Ejemplo n.º 1
0
def generate_enums():
    # otherwise we're missing some enums
    vips_lib.vips_token_get_type()
    vips_lib.vips_saveable_get_type()
    vips_lib.vips_image_type_get_type()

    all_enums = []

    def add_enum(gtype, a, b):
        nickname = type_name(gtype)
        all_enums.append(nickname)

        type_map(gtype, add_enum)

        return ffi.NULL

    type_map(type_from_name('GEnum'), add_enum)

    for name in all_enums:
        gtype = type_from_name(name)
        python_name = remove_prefix(name)
        if python_name not in xml_enums:
            continue

        node = xml_enums[python_name]
        enum_doc = node.find("goi:doc", namespace)

        print(f'')
        print(f'')
        print(f'class {python_name}(object):')
        print(f'    """{python_name}.')
        if enum_doc is not None:
            print(f'')
            print(f'{enum_doc.text}')
        print(f'')
        print(f'Attributes:')
        print(f'')
        for value in values_for_enum(gtype):
            python_name = value.replace('-', '_')
            member = node.find(f"goi:member[@name='{python_name}']", namespace)
            member_doc = member.find("goi:doc", namespace)
            if member_doc is not None:
                text = member_doc.text
                print(f'    {python_name.upper()} (str): {text}')
                print(f'')
        print(f'    """')
        print(f'')

        for value in values_for_enum(gtype):
            python_name = value.replace('-', '_').upper()
            print(f'    {python_name} = \'{value}\'')
Ejemplo n.º 2
0
def generate_enums_flags(file):
    all_enums = []
    all_flags = []

    def add_enum(gtype, a, b):
        nickname = type_name(gtype)
        all_enums.append(nickname)

        type_map(gtype, add_enum)

        return ffi.NULL

    # Enums
    type_map(type_from_name('GEnum'), add_enum)

    # Flags
    all_flags.append('VipsForeignPngFilter')

    print(f'Generating {file}...')

    with open(file, 'w') as f:
        f.write('    // Auto-generated enums\n')

        for name in all_enums:
            gtype = type_from_name(name)

            f.write(f'    enum_<{name}>("{remove_prefix(name)}")')

            for value in values_for_enum(gtype):
                js_value = cppize(value)
                prefix = to_snake_case(name).upper()
                if prefix == 'VIPS_BAND_FORMAT':
                    prefix = 'VIPS_FORMAT'
                elif prefix == 'VIPS_IMAGE_TYPE':
                    prefix = 'VIPS_IMAGE'
                cpp_value = prefix + '_' + js_value.upper()
                if cpp_value == 'VIPS_INTERPRETATION_SRGB':
                    cpp_value = 'VIPS_INTERPRETATION_sRGB'
                elif cpp_value == 'VIPS_INTERPRETATION_SCRGB':
                    cpp_value = 'VIPS_INTERPRETATION_scRGB'

                f.write(f'\n        .value("{js_value}", {cpp_value})')

            f.write(';\n\n')

        for name in all_flags:
            gtype = type_from_name(name)

            f.write(f'    enum_<{name}>("{remove_prefix(name)}")')

            for value in values_for_flag(gtype):
                js_value = cppize(value)
                prefix = to_snake_case(name).upper()
                cpp_value = prefix + '_' + js_value.upper()

                f.write(f'\n        .value("{js_value}", {cpp_value})')

            f.write(';\n\n')
Ejemplo n.º 3
0
def generate_enums():
    # otherwise we're missing some enums
    vips_lib.vips_token_get_type()
    vips_lib.vips_saveable_get_type()
    vips_lib.vips_image_type_get_type()

    all_enums = []

    def add_enum(gtype, a, b):
        nickname = type_name(gtype)
        all_enums.append(nickname)

        type_map(gtype, add_enum)

        return ffi.NULL

    type_map(type_from_name('GEnum'), add_enum)

    for name in all_enums:
        gtype = type_from_name(name)
        php_name = remove_prefix(name)

        print('Generating {0}.php ...'.format(php_name))

        with open('{0}.php'.format(php_name), 'w') as f:
            f.write(preamble)
            f.write('\n')
            f.write('namespace Jcupitt\\Vips;\n')
            f.write('\n')
            f.write('/**\n')
            f.write(' * The {0} enum.\n'.format(php_name))
            f.write(class_header)
            f.write(' */\n')
            f.write('abstract class {0}\n'.format(php_name))
            f.write('{\n')

            for value in values_for_enum(gtype):
                php_name = value.replace('-', '_').upper()
                if php_name in reserved_php_names:
                    php_name = reserved_php_names[php_name]
                f.write('    const {0} = \'{1}\';\n'.format(php_name, value))

            f.write('}\n')
Ejemplo n.º 4
0
def generate_enums_flags(gir_file, out_file):
    root = ET.parse(gir_file).getroot()
    namespace = {'goi': 'http://www.gtk.org/introspection/core/1.0'}

    # find all the enumerations/flags and make a dict for them
    xml_enums = {}
    for node in root.findall('goi:namespace/goi:enumeration', namespace):
        xml_enums[node.get('name')] = node

    xml_flags = {}
    for node in root.findall('goi:namespace/goi:bitfield', namespace):
        xml_flags[node.get('name')] = node

    all_nicknames = []

    def add_enum(gtype, a, b):
        nickname = type_name(gtype)
        all_nicknames.append(nickname)
        gtype_to_js_param[gtype] = f'{remove_prefix(nickname)} | Enum'

        type_map(gtype, add_enum)

        return ffi.NULL

    # Enums
    type_map(type_from_name('GEnum'), add_enum)

    # Flags
    all_nicknames.append('VipsForeignPngFilter')
    gtype_to_js_param[
        type_from_name('VipsForeignPngFilter'
                       )] = f'{remove_prefix("VipsForeignPngFilter")} | Flag'

    with open('preamble_vips.d.ts', 'r') as f:
        preamble = f.read()

    with open(out_file, 'w') as f:
        f.write(preamble)

        for name in all_nicknames:
            gtype = type_from_name(name)
            name = remove_prefix(name)
            if name in xml_enums:
                is_enum = True
                node = xml_enums[name]
            elif name in xml_flags:
                is_enum = False
                node = xml_flags[name]
            else:
                continue

            enum_doc = node.find('goi:doc', namespace)

            if enum_doc is not None:
                text = enum_doc.text.replace('\n', '\n     * ')
                f.write('    /**\n')
                f.write(f"     * {text}\n")
                f.write('     */\n')

            f.write(f'    export enum {name} {{\n')

            values = values_for_enum(gtype) if is_enum else values_for_flag(
                gtype)
            for i, value in enumerate(values):
                js_value = value.replace('-', '_')
                if i == 0 and (js_value == 'error' or js_value == 'notset'):
                    continue

                member = node.find(f"goi:member[@name='{js_value}']",
                                   namespace)
                member_doc = member.find('goi:doc', namespace)
                if member_doc is not None:
                    text = member_doc.text[:1].upper() + member_doc.text[1:]
                    f.write('        /**\n')
                    f.write(f'         * {text}\n')
                    f.write('         */\n')

                f.write(f"        {js_value} = '{value}'")

                if i != len(values) - 1:
                    f.write(',\n')

            f.write('\n    }\n\n')