<commit_before>#!/usr/bin/env python3

"""Extract a .blend file from a Windows executable (.exe).
Website: https://gitlab.com/ricvelozo/scripts
"""

__version__ = '2.2.0'
__author__ = '<NAME>'

import click
import re
from io import DEFAULT_BUFFER_SIZE
from os import path

@click.command(context_settings=dict(help_option_names=['-h', '--help']))
@click.version_option(version=__version__, message='%(prog)s %(version)s')
@click.argument('executables', metavar='INPUT', nargs=-1)
def extract(executables: list[str]):
    """Extract a .blend file from a Windows executable (.exe)."""
    prog = re.compile(b'BLENDER[_|-][v|V][0-9]{3}')
    if not executables:
        executable = input('Enter the URL of the executable file, ' \
                           'or ENTER to exit:\n>>> ').strip()
        if executable:
            executables = [executable]
    for i, executable in enumerate(executables):
        dir, filename = path.split(executable)
        blend = filename[:-4] if filename.endswith('.exe') else filename
        blend += '.blend'
        if i > 0:
            print()
        try:
            with open(executable, 'rb') as exe:
                print(f'{[i]} Processing `{filename}`...')
                bigger_chunk = b''
                while chunk := exe.read(DEFAULT_BUFFER_SIZE):
                    bigger_chunk += chunk
                    magic_number = prog.search(bigger_chunk)
                    if magic_number is None:
                        # Include the last 12 bytes in the next search
                        bigger_chunk = bigger_chunk[-12:]
                    else:
                        print(f'{[i]} Extracting `{blend}`...')
                        try:
                            with open(path.join(dir, blend), 'wb') as output:
                                # The .blend file's start
                                start = magic_number.start()
                                output.write(bigger_chunk[start:])

                                while chunk := exe.read(DEFAULT_BUFFER_SIZE):
                                    output.write(chunk)
                        except IOError:
                            print(f'{[i]} Could not write to `{blend}`.')
                        break

                # EOF
                if magic_number is None:
                    print(f'{[i]} The file `{filename}` does not have a ' \
                           '.blend file.')
        except IOError:
            print(f'{[i]} Could not open the file `{filename}`.')

if __name__ == '__main__':<commit_msg>Simplify the code, and add more comments<commit_after>#!/usr/bin/env python3

"""Extract a .blend file from a Windows executable (.exe).
Website: https://gitlab.com/ricvelozo/scripts
"""

__version__ = '2.2.1'
__author__ = '<NAME>'

import click
import re
from io import DEFAULT_BUFFER_SIZE
from os import path

@click.command(context_settings=dict(help_option_names=['-h', '--help']))
@click.version_option(version=__version__, message='%(prog)s %(version)s')
@click.argument('executables', metavar='INPUT', nargs=-1)
def extract(executables: list[str]):
    """Extract a .blend file from a Windows executable (.exe)."""
    prog = re.compile(b'BLENDER[_|-][v|V][0-9]{3}')
    if not executables:
        executable = input('Enter the URL of the executable file, ' \
                           'or ENTER to exit:\n>>> ').strip()
        if executable:
            executables = [executable]
    for i, executable in enumerate(executables):
        dir, filename = path.split(executable)
        blend = filename[:-4] if filename.endswith('.exe') else filename
        blend += '.blend'
        if i > 0:
            print()
        try:
            with open(executable, 'rb') as exe:
                print(f'{[i]} Processing `{filename}`...')
                bigger_chunk = b''
                while chunk := exe.read(DEFAULT_BUFFER_SIZE):
                    bigger_chunk += chunk
                    if magic_number := prog.search(bigger_chunk):
                        print(f'{[i]} Extracting `{blend}`...')
                        try:
                            with open(path.join(dir, blend), 'wb') as output:
                                # Copy the .blend file's start
                                start = magic_number.start()
                                output.write(bigger_chunk[start:])

                                # Copy the rest of the .blend file
                                while chunk := exe.read(DEFAULT_BUFFER_SIZE):
                                    output.write(chunk)
                        except IOError:
                            print(f'{[i]} Could not write to `{blend}`.')
                        break
                    else:
                        # Keep only the last 12 bytes for the next search
                        bigger_chunk = bigger_chunk[-12:]

                # EOF
                if magic_number is None:
                    print(f'{[i]} The file `{filename}` does not have a ' \
                           '.blend file.')
        except IOError:
            print(f'{[i]} Could not open the file `{filename}`.')

if __name__ == '__main__':
{'fraction_non_alphanumeric': 0.1218568665377176, 'fraction_numerical': 0.005415860735009671, 'mean_word_length': 1.568802781917536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '458681', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1388, 'n_words': 375}