|
1 |
| -#!/usr/bin/env python |
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import xml.etree.ElementTree as ET |
2 | 5 |
|
3 | 6 | from pyvips import ffi, values_for_enum, vips_lib, \
|
4 | 7 | type_map, type_name, type_from_name
|
5 | 8 |
|
6 | 9 | # This file generates enums.py -- the set of classes giving the permissible
|
7 | 10 | # values for the pyvips enums. Run with something like:
|
8 | 11 | #
|
9 |
| -# ./gen-enums.py > enums.py |
| 12 | +# ./gen-enums.py ~/GIT/libvips/libvips/Vips-8.0.gir > enums.py |
10 | 13 | # mv enums.py ../pyvips
|
11 | 14 |
|
| 15 | +# The GIR file |
| 16 | +root = ET.parse(sys.argv[1]).getroot() |
| 17 | +namespace = { |
| 18 | + "goi": "http://www.gtk.org/introspection/core/1.0" |
| 19 | +} |
| 20 | + |
| 21 | +# find all the enumerations and make a dict for them |
| 22 | +xml_enums = {} |
| 23 | +for node in root.findall("goi:namespace/goi:enumeration", namespace): |
| 24 | + xml_enums[node.get('name')] = node |
| 25 | + |
12 | 26 |
|
13 | 27 | def remove_prefix(enum_str):
|
14 | 28 | prefix = 'Vips'
|
@@ -40,16 +54,37 @@ def add_enum(gtype, a, b):
|
40 | 54 | for name in all_enums:
|
41 | 55 | gtype = type_from_name(name)
|
42 | 56 | python_name = remove_prefix(name)
|
43 |
| - |
44 |
| - print('class {0}(object):'.format(python_name)) |
| 57 | + if python_name not in xml_enums: |
| 58 | + continue |
| 59 | + |
| 60 | + node = xml_enums[python_name] |
| 61 | + enum_doc = node.find("goi:doc", namespace) |
| 62 | + |
| 63 | + print(f'') |
| 64 | + print(f'') |
| 65 | + print(f'class {python_name}(object):') |
| 66 | + print(f' """{python_name}.') |
| 67 | + if enum_doc is not None: |
| 68 | + print(f'') |
| 69 | + print(f' {enum_doc.text}') |
| 70 | + print(f'') |
| 71 | + print(f' Attributes:') |
| 72 | + for value in values_for_enum(gtype): |
| 73 | + python_name = value.replace('-', '_') |
| 74 | + member = node.find(f"goi:member[@name='{python_name}']", namespace) |
| 75 | + member_doc = member.find("goi:doc", namespace) |
| 76 | + if member_doc is not None: |
| 77 | + text = member_doc.text |
| 78 | + print(f' {python_name.upper()} (str): {text}') |
| 79 | + print(f'') |
| 80 | + print(f' """') |
| 81 | + print(f'') |
45 | 82 |
|
46 | 83 | for value in values_for_enum(gtype):
|
47 | 84 | python_name = value.replace('-', '_').upper()
|
48 |
| - print(' {0} = \'{1}\''.format(python_name, value)) |
49 |
| - |
50 |
| - print('') |
51 |
| - print('') |
| 85 | + print(f' {python_name} = \'{value}\'') |
52 | 86 |
|
53 | 87 |
|
54 | 88 | if __name__ == "__main__":
|
| 89 | + print(f'# libvips enums -- this file is generated automatically') |
55 | 90 | generate_enums()
|
0 commit comments