Skip to content

Commit 03453ff

Browse files
committed
Merge branch 'master' of github.com:libvips/pyvips
2 parents 1228ec7 + bcaa50e commit 03453ff

File tree

2 files changed

+888
-11
lines changed

2 files changed

+888
-11
lines changed

examples/gen-enums.py

+43-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import xml.etree.ElementTree as ET
25

36
from pyvips import ffi, values_for_enum, vips_lib, \
47
type_map, type_name, type_from_name
58

69
# This file generates enums.py -- the set of classes giving the permissible
710
# values for the pyvips enums. Run with something like:
811
#
9-
# ./gen-enums.py > enums.py
12+
# ./gen-enums.py ~/GIT/libvips/libvips/Vips-8.0.gir > enums.py
1013
# mv enums.py ../pyvips
1114

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+
1226

1327
def remove_prefix(enum_str):
1428
prefix = 'Vips'
@@ -40,16 +54,37 @@ def add_enum(gtype, a, b):
4054
for name in all_enums:
4155
gtype = type_from_name(name)
4256
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'')
4582

4683
for value in values_for_enum(gtype):
4784
python_name = value.replace('-', '_').upper()
48-
print(' {0} = \'{1}\''.format(python_name, value))
49-
50-
print('')
51-
print('')
85+
print(f' {python_name} = \'{value}\'')
5286

5387

5488
if __name__ == "__main__":
89+
print(f'# libvips enums -- this file is generated automatically')
5590
generate_enums()

0 commit comments

Comments
 (0)