<commit_before>#
# ico2py.py
# Copyright 2012 d10n
# License: BSD 2-Clause
#
"""
Combine images representing icon layers to a wxPython Icon

Usage:
    ico2py.py image_files... [python_file]

    If python_file is omitted, the output is written to stdout
    python_file must end with .py

"""
import os
import sys
import StringIO
import base64
import textwrap


def file_to_bytes(file_name):
    return open(file_name, 'rb').read()


def chunks(splittable, chunk_length):
    # Need to use this because Python 2.7 doesn't have textwrap.indent()
    # http://stackoverflow.com/questions/312443
    for i in xrange(0, len(splittable), chunk_length):
        yield splittable[i:i + chunk_length]


def sanitize_variable(variable_name):
    return ''.join(x if x.isalnum() else '_' for x in variable_name)


def main(args=None):
    if not args:
        args = sys.argv[1:]
    if (
        (not args) or
        (len(args) < 2)
    ):
        print __doc__
        return

    real_output_file = args[-1].endswith('.py')

    if real_output_file:
        input_files = args[:-1]
        output_file = open(args[-1], 'w')
    else:
        input_files = args
        output_file = StringIO.StringIO()

    def write(line=None):
        if line:
            output_file.write(line + os.linesep)
        else:
            output_file.write(os.linesep)

    input_b64 = {f: base64.b64encode(file_to_bytes(f)) for f in input_files}
    output_b64 = {}
    max_b64_line_len = 79 - len("    ''")  # PEP8 length
    for name, b64 in input_b64.iteritems():
        name = sanitize_variable(name)
        output_b64[name] = []
        for chunk in chunks(b64, max_b64_line_len):
            output_b64[name].append("'" + chunk + "'")

    header = textwrap.dedent("""
        # file generated by ico2py.py
        import wx
        import base64
        import StringIO

        """).lstrip()
    write(header)

    for name, b64_parts in output_b64.iteritems():
        write('b64_' + sanitize_variable(name) + ' = (')
        for b64 in b64_parts:
            write(' ' * 4 + b64)
        write(')')
        write()
    write()
    footer = textwrap.dedent("""
        def _get_icon(b64):
            image_bytes = base64.b64decode(b64)
            image_stream = StringIO.StringIO(image_bytes)
            image = wx.ImageFromStream(image_stream)
            bitmap = wx.BitmapFromImage(image)
            icon = wx.EmptyIcon()
            icon.CopyFromBitmap(bitmap)
            return icon
        """).strip()
    write(footer)
    write()
    write()
    write(textwrap.dedent("""
        def get_icon_bundle():
            icon_bundle = wx.IconBundle()
        """).strip())
    write()
    for name in output_b64.keys():
        write('    {0} = _get_icon(b64_{0})'.format(name))
    for name in output_b64.keys():
        write('    icon_bundle.AddIcon({0})'.format(name))
    write('    return icon_bundle')

    if not real_output_file:
        print(output_file.getvalue())

    output_file.close()


if __name__ == '__main__':
    exit_code = main(sys.argv[1:])
    sys.exit(exit_code)

<commit_msg>Windows newline fix<commit_after>#
# ico2py.py
# Copyright 2012 d10n
# License: BSD 2-Clause
#
"""
Combine images representing icon layers to a wxPython Icon

Usage:
    ico2py.py image_files... [python_file]

    If python_file is omitted, the output is written to stdout
    python_file must end with .py

"""
import os
import sys
import StringIO
import base64
import textwrap


def file_to_bytes(file_name):
    return open(file_name, 'rb').read()


def chunks(splittable, chunk_length):
    # Need to use this because Python 2.7 doesn't have textwrap.indent()
    # http://stackoverflow.com/questions/312443
    for i in xrange(0, len(splittable), chunk_length):
        yield splittable[i:i + chunk_length]


def sanitize_variable(variable_name):
    return ''.join(x if x.isalnum() else '_' for x in variable_name)


def main(args=None):
    if not args:
        args = sys.argv[1:]
    if (
        (not args) or
        (len(args) < 2)
    ):
        print __doc__
        return

    real_output_file = args[-1].endswith('.py')

    if real_output_file:
        input_files = args[:-1]
        output_file = open(args[-1], 'wb')
    else:
        input_files = args
        output_file = StringIO.StringIO()

    def write(line=None):
        if line:
            line = os.linesep.join(line.splitlines())  # Windows fix
            output_file.write(line + os.linesep)
        else:
            output_file.write(os.linesep)

    input_b64 = {f: base64.b64encode(file_to_bytes(f)) for f in input_files}
    output_b64 = {}
    max_b64_line_len = 79 - len("    ''")  # PEP8 length
    for name, b64 in input_b64.iteritems():
        name = sanitize_variable(name)
        output_b64[name] = []
        for chunk in chunks(b64, max_b64_line_len):
            output_b64[name].append("'" + chunk + "'")

    header = textwrap.dedent("""
        # file generated by ico2py.py
        import wx
        import base64
        import StringIO

        """).lstrip()
    write(header)

    for name, b64_parts in output_b64.iteritems():
        write('b64_' + sanitize_variable(name) + ' = (')
        for b64 in b64_parts:
            write(' ' * 4 + b64)
        write(')')
        write()
    write()
    footer = textwrap.dedent("""
        def _get_icon(b64):
            image_bytes = base64.b64decode(b64)
            image_stream = StringIO.StringIO(image_bytes)
            image = wx.ImageFromStream(image_stream)
            bitmap = wx.BitmapFromImage(image)
            icon = wx.EmptyIcon()
            icon.CopyFromBitmap(bitmap)
            return icon
        """).strip()
    write(footer)
    write()
    write()
    write(textwrap.dedent("""
        def get_icon_bundle():
            icon_bundle = wx.IconBundle()
        """).strip())
    write()
    for name in output_b64.keys():
        write('    {0} = _get_icon(b64_{0})'.format(name))
    for name in output_b64.keys():
        write('    icon_bundle.AddIcon({0})'.format(name))
    write('    return icon_bundle')

    if not real_output_file:
        print(output_file.getvalue())

    output_file.close()


if __name__ == '__main__':
    exit_code = main(sys.argv[1:])
    sys.exit(exit_code)


{'fraction_non_alphanumeric': 0.12089837527875119, 'fraction_numerical': 0.0267601146862058, 'mean_word_length': 2.373992477162816, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2512768', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2052, 'n_words': 480}