<commit_before>from __future__ import print_function

import sys
import binascii
import argparse
import re


# raw_input doesn't exist in Python 3
try:
    raw_input
except NameError:
    raw_input = input


PROMPT_TEXT = (
    "I agree to be careful"
)

WARNING_TEXT = """
USE THIS SOFTWARE AT YOUR OWN DISCRETION. THIS SOFTWARE HAS *NOT* BEEN 
EXTENSIVELY TESTED AND MAY NOT WORK WITH YOUR GRAPHICS CARD.

If you want to save the created vBIOS file, type the following phrase
EXACTLY as it is written below:

%s
""" % PROMPT_TEXT


class VBIOSROM(object):
    def __init__(self, f):
        """
        Load a VBIOS and convert it into a hex-ascii format
        for easier editing
        """
        content = f.read()
        self.offsets = {
            "header": None,
            "footer": None
        }
        self.content = binascii.hexlify(content)

    def detect_offsets(self):
        """
        Search the ROM for known sections of data and raise an AssertionError
        if any of the checks fails
        """
        content = self.content

        # Search for the header that starts the file
        # Examples of this header:
        #
        # U.y.K7400.L.w.VIDEO
        # U.x.K7400.L.w.VIDEO
        #
        HEADER_REGEX = (
            b'55aa(([a-z]|[0-9]){2})(eb)(([a-z]|[0-9]){20})(564944454f)'
        )
        result = re.compile(HEADER_REGEX).search(self.content)
        assert len(result.groups()) == 6, "Couldn't find the ROM header!"
        self.offsets["header"] = result.start(0)
        
        # Search for the footer, which are shortly followed by
        # 'NPDS' and 'NPDE' strings. 'NPDS' and 'NPDE' markers are separated by
        # 28 ASCII characters
        FOOTER_REGEX = (
            b'564e(([a-z]|[0-9]){348})(4e504453)(([a-z]|[0-9]){56})(4e504445)'
        )
        result = re.compile(FOOTER_REGEX).search(self.content)
        assert len(result.groups()) == 6, "Couldn't find the ROM footer!"
        self.offsets["footer"] = result.start(0)

    def run_sanity_tests(self, ignore_check=False):
        """
        Run a few sanity tests on the ROM to be a little more sure we are
        working with a valid ROM
        """
        try:
            # There should be one 'NPDS' marker and three 'NPDE' markers
            # before the footer we've already found
            #
            # The 'NPDS' marker should be followed by two 'NPDE' markers
            npds_count = self.content.count(
                b"4e504453", self.offsets["header"], self.offsets["footer"])
            assert npds_count == 1, \
                str("Expected only one 'NPDS' marker between header and "
                    "footer, found %d" % npds_count)

            npde_count = self.content.count(
                b"4e504445", self.offsets["header"], self.offsets["footer"])
            assert npde_count == 3, \
                str("Expected only three 'NPDE' markers between header and "
                    "footer, found %d" % npde_count)

            npde_after_npds_count = self.content.count(
                b"4e504445", self.content.find(b"4e504453"),
                self.offsets["footer"])

            assert npde_after_npds_count == 2, \
                "Expected two 'NPDE' markers after the 'NPDS' marker"
        except AssertionError as e:
            if ignore_check:
                print("Encountered error during sanity check: %s" % str(e))
                print("Ignoring...")
            else:
                raise

        print("No problems found.")

    def get_spliced_rom(self):
        """
        Convert the internal hex-ascii representation of the ROM
        into binary data for saving
        """
        start = self.offsets["header"]
        end = self.offsets["footer"]
        spliced = self.content[start:end]

        return binascii.unhexlify(spliced)


def main():
    parser = argparse.ArgumentParser(
        description=(<commit_msg>Don't print 'No problems found' if problems were found<commit_after>from __future__ import print_function

import sys
import binascii
import argparse
import re


# raw_input doesn't exist in Python 3
try:
    raw_input
except NameError:
    raw_input = input


PROMPT_TEXT = (
    "I agree to be careful"
)

WARNING_TEXT = """
USE THIS SOFTWARE AT YOUR OWN DISCRETION. THIS SOFTWARE HAS *NOT* BEEN 
EXTENSIVELY TESTED AND MAY NOT WORK WITH YOUR GRAPHICS CARD.

If you want to save the created vBIOS file, type the following phrase
EXACTLY as it is written below:

%s
""" % PROMPT_TEXT


class VBIOSROM(object):
    def __init__(self, f):
        """
        Load a VBIOS and convert it into a hex-ascii format
        for easier editing
        """
        content = f.read()
        self.offsets = {
            "header": None,
            "footer": None
        }
        self.content = binascii.hexlify(content)

    def detect_offsets(self):
        """
        Search the ROM for known sections of data and raise an AssertionError
        if any of the checks fails
        """
        content = self.content

        # Search for the header that starts the file
        # Examples of this header:
        #
        # U.y.K7400.L.w.VIDEO
        # U.x.K7400.L.w.VIDEO
        #
        HEADER_REGEX = (
            b'55aa(([a-z]|[0-9]){2})(eb)(([a-z]|[0-9]){20})(564944454f)'
        )
        result = re.compile(HEADER_REGEX).search(self.content)
        assert len(result.groups()) == 6, "Couldn't find the ROM header!"
        self.offsets["header"] = result.start(0)
        
        # Search for the footer, which are shortly followed by
        # 'NPDS' and 'NPDE' strings. 'NPDS' and 'NPDE' markers are separated by
        # 28 ASCII characters
        FOOTER_REGEX = (
            b'564e(([a-z]|[0-9]){348})(4e504453)(([a-z]|[0-9]){56})(4e504445)'
        )
        result = re.compile(FOOTER_REGEX).search(self.content)
        assert len(result.groups()) == 6, "Couldn't find the ROM footer!"
        self.offsets["footer"] = result.start(0)

    def run_sanity_tests(self, ignore_check=False):
        """
        Run a few sanity tests on the ROM to be a little more sure we are
        working with a valid ROM
        """
        try:
            # There should be one 'NPDS' marker and three 'NPDE' markers
            # before the footer we've already found
            #
            # The 'NPDS' marker should be followed by two 'NPDE' markers
            npds_count = self.content.count(
                b"4e504453", self.offsets["header"], self.offsets["footer"])
            assert npds_count == 1, \
                str("Expected only one 'NPDS' marker between header and "
                    "footer, found %d" % npds_count)

            npde_count = self.content.count(
                b"4e504445", self.offsets["header"], self.offsets["footer"])
            assert npde_count == 3, \
                str("Expected only three 'NPDE' markers between header and "
                    "footer, found %d" % npde_count)

            npde_after_npds_count = self.content.count(
                b"4e504445", self.content.find(b"4e504453"),
                self.offsets["footer"])

            assert npde_after_npds_count == 2, \
                "Expected two 'NPDE' markers after the 'NPDS' marker"
        except AssertionError as e:
            if ignore_check:
                print("Encountered error during sanity check: %s" % str(e))
                print("Ignoring...")
                return
            else:
                raise

        print("No problems found.")

    def get_spliced_rom(self):
        """
        Convert the internal hex-ascii representation of the ROM
        into binary data for saving
        """
        start = self.offsets["header"]
        end = self.offsets["footer"]
        spliced = self.content[start:end]

        return binascii.unhexlify(spliced)


def main():
    parser = argparse.ArgumentParser(
        description=(
{'fraction_non_alphanumeric': 0.10537607269056032, 'fraction_numerical': 0.02271580010095911, 'mean_word_length': 1.9928247734138973, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5497763', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2340, 'n_words': 692}