-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathgen-enums.py
executable file
·209 lines (161 loc) · 5.85 KB
/
gen-enums.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
import sys
import xml.etree.ElementTree as ET
from pyvips import ffi, enum_dict, flags_dict, \
vips_lib, type_map, type_name, type_from_name
# This file generates enums.py -- the set of classes giving the permissible
# values for the pyvips enums/flags. Run with something like:
#
# ./gen-enums.py ~/GIT/libvips/build/libvips/Vips-8.0.gir > enums.py
# mv enums.py ../pyvips
# The GIR file
root = ET.parse(sys.argv[1]).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
def remove_prefix(enum_str):
prefix = 'Vips'
if enum_str.startswith(prefix):
return enum_str[len(prefix):]
return enum_str
def rewrite_references(string):
"""Rewrite a gi-docgen references to RST style.
gi-docgen references look like this:
[func@version]
[class@Image]
[func@Image.bandjoin]
[meth@Image.add]
[ctor@Image.new_from_file]
[enum@SdfShape]
[enum@Vips.SdfShape.CIRCLE]
we look for the approximate patterns and rewrite in RST style, so:
:meth:`.version`
:class:`.Image`
:meth:`.Image.bandjoin`
:meth:`.Image.new_from_file`
:class:`.enums.SdfShape`
:class:`.enums.SdfShape.CIRCLE`
"""
import re
while True:
match = re.search(r"\[(.*?)@(.*?)\]", string)
if not match:
break
before = string[0:match.span(0)[0]]
type = match[1]
target = match[2]
after = string[match.span(0)[1]:]
if type in ["ctor", "meth", "method", "func"]:
python_type = "meth"
elif type in ["class", "enum", "flags"]:
python_type = "class"
else:
raise Exception(f'type "{type}" is unknown')
match = re.match("Vips.(.*)", target)
if match:
target = match[1]
if type == "enum":
target = f"enums.{target}"
string = f"{before}:{python_type}:`.{target}`{after}"
return string
def generate_enums():
all_nicknames = []
def add_nickname(gtype, a, b):
nickname = type_name(gtype)
all_nicknames.append(nickname)
type_map(gtype, add_nickname)
return ffi.NULL
type_map(type_from_name('GEnum'), add_nickname)
# Filter internal enums
blacklist = ['VipsImageType', 'VipsToken']
all_nicknames = [name for name in all_nicknames if name not in blacklist]
for name in all_nicknames:
gtype = type_from_name(name)
python_name = remove_prefix(name)
if python_name not in xml_enums:
continue
node = xml_enums[python_name]
values = enum_dict(gtype)
enum_doc = node.find("goi:doc", namespace)
print('')
print('')
print(f'class {python_name}(object):')
print(f' """{python_name}.')
if enum_doc is not None:
print('')
print(f'{rewrite_references(enum_doc.text)}')
print('')
print('Attributes:')
print('')
for key, value in values.items():
python_name = key.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 = rewrite_references(member_doc.text)
print(f' {python_name.upper()} (str): {text}')
print('')
print(' """')
print('')
for key, value in values.items():
python_name = key.replace('-', '_').upper()
print(f' {python_name} = \'{key}\'')
def generate_flags():
all_nicknames = []
def add_nickname(gtype, a, b):
nickname = type_name(gtype)
all_nicknames.append(nickname)
type_map(gtype, add_nickname)
return ffi.NULL
type_map(type_from_name('GFlags'), add_nickname)
# Filter internal flags
blacklist = ['VipsForeignFlags']
all_nicknames = [name for name in all_nicknames if name not in blacklist]
for name in all_nicknames:
gtype = type_from_name(name)
python_name = remove_prefix(name)
if python_name not in xml_flags:
continue
node = xml_flags[python_name]
values = flags_dict(gtype)
enum_doc = node.find("goi:doc", namespace)
print('')
print('')
print(f'class {python_name}(object):')
print(f' """{python_name}.')
if enum_doc is not None:
print('')
print(f'{rewrite_references(enum_doc.text)}')
print('')
print('Attributes:')
print('')
for key, value in values.items():
python_name = key.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()} (int): '
f'{rewrite_references(text)}')
print('')
print(' """')
print('')
for key, value in values.items():
python_name = key.replace('-', '_').upper()
print(f' {python_name} = {value}')
if __name__ == "__main__":
# 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()
print('# libvips enums -- this file is generated automatically')
print('# flake8: noqa: E501') # ignore line too long error
generate_enums()
generate_flags()