-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
fetch_default_values.py
299 lines (250 loc) · 10.6 KB
/
fetch_default_values.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Fetch from the official WebGPU specification the default values for
descriptors.
Copyright (c) 2022-2023 Élie Michel
"""
import argparse
from os.path import dirname, isfile, join
import dataclasses
from dataclasses import dataclass, field
import logging
import re
import json
try:
from lxml import html
except ImportError:
print("[ERROR] You need to install the lxml module! (Run `pip install lxml`)")
exit(1)
#------------------------------------------------
# Command line arguments
parser = argparse.ArgumentParser(prog="fetch_default_values", description=__doc__)
parser.add_argument("-u", "--url", type=str, default="https://www.w3.org/TR/webgpu",
help="URL of the specification document")
parser.add_argument('-v', '--log-level', type=str, default='INFO',
help="level of verbosity: DEBUG, INFO, WARNING ou ERROR")
parser.add_argument('-s', '--output-spec', type=str, default='spec.json',
help="output JSON file containing the scraped API")
parser.add_argument('-d', '--output-defaults', type=str, default='defaults.txt',
help="output file containing the default values")
#------------------------------------------------
# Main outline
def main(args):
configureLogging(args)
spec = download(args.url, label="WebGPU specification")
api = parseAllDefinitions(spec)
exportApi(api, args.output_spec)
exportDefaults(api, args.output_defaults)
logging.info(f"Done.")
#------------------------------------------------
# Structures
@dataclass
class FieldApi:
name: str
type: str
default: str|None = None
required: bool = False
@dataclass
class DictionaryApi:
name: str
parent: str|None
fields: list[FieldApi] = field(default_factory=list)
@dataclass
class WebGpuApi:
dictionaries: dict[str,DictionaryApi] = field(default_factory=dict)
#------------------------------------------------
# Steps
def configureLogging(args):
logging.basicConfig(format='[%(levelname)s] %(message)s', level=args.log_level)
def parseAllDefinitions(spec):
root = html.fromstring(spec)
defs = root.xpath("//pre[@class='idl highlight def']")
api = WebGpuApi()
for d in defs:
parseDefinition(api, d)
return api
def exportApi(api, filename):
logging.info(f"Exporting API specification to {filename}...")
with open(filename, 'w', encoding="utf-8") as f:
json.dump(api, f, indent=2, cls=EnhancedJSONEncoder)
def exportDefaults(api, filename):
logging.info(f"Exporting default values to {filename}...")
# Some nested structures from the JavaScript APIs are unfolded in
# the C header (or the other way around) so we remap the default
# values to their parent structure.
remap_to_parent = {
# "JavaScript dictionary": "C Structure",
"GPUBufferBinding": "WGPUBindGroupEntry",
"GPURenderPassLayout": "WGPURenderBundleEncoderDescriptor",
"GPUOrigin3DDict": "WGPUOrigin3D",
"GPUExtent3DDict": "WGPUExtent3D",
# ("JavaScript dictionary", "JavaScript property"): ("C Structure", "C field"),
("GPUPrimitiveState", "unclippedDepth"): ("WGPUPrimitiveDepthClipControl", "unclippedDepth"),
}
# Some structure are JavaScript-specific and have not been ported in
# the native C header.
ignored = [
"GPUExternalTextureDescriptor",
"GPUImageDataLayout",
"GPUImageCopyTextureTagged",
"GPUImageCopyExternalImage",
"GPUCanvasConfiguration",
"GPUOrigin2DDict",
]
with open(filename, 'w', encoding="utf-8") as f:
for d in api.dictionaries.values():
if d.name in ignored:
continue
fixed_name = remap_to_parent.get(d.name, f"W{d.name}")
wrote_any = False
for field in d.fields:
if field.default is not None:
dict_name, field_name = remap_to_parent.get((d.name, field.name), (fixed_name, field.name))
f.write(f"{dict_name}::{field_name} = {field.default};\n")
wrote_any = True
if wrote_any:
f.write(f"\n")
#------------------------------------------------
# Parser
def parseDefinition(api, body):
it = iter(body.text_content().split("\n"))
start_dict_re = re.compile(r"^dictionary (\w+)( : (\w+))? {$")
split_start_dict_A_re = re.compile(r"^dictionary (\w+)$")
split_start_dict_B_re = re.compile(r"^\s*: (\w+)? {$")
start_enum_re = re.compile(r"^enum (\w+) {$")
end_enum_re = re.compile(r"^};$")
start_interface_re = re.compile(r"^interface (\w+)( : (\w+))? {$")
split_start_interface_A_re = re.compile(r"^interface (\w+)$")
split_start_interface_B_re = re.compile(r"^\s*: (\w+)? {$")
start_partial_interface_re = re.compile(r"^partial interface (\w+) {$")
start_interface_mixin_re = re.compile(r"^interface mixin (\w+) {$")
end_interface_re = re.compile(r"^};$")
start_namespace_re = re.compile(r"^namespace (\w+) {$")
end_namespace_re = re.compile(r"^};$")
interface_attribs_re = re.compile(r"^\[Exposed=")
typedef_re = re.compile(r"^typedef( (\[\w+\]))?( (\w+))+;")
typedef2_re = re.compile(r"^typedef \([\w<>]+( or [\w<>]+)+\) (\w+);")
start_typedef_re = re.compile(r"^typedef \(\w+ or$")
end_typedef_re = re.compile(r"^\s*\w+\) (\w+);")
includes_re = re.compile(r"^(\w+) includes (\w+);")
state = 'NONE'
dict_name = None
while (x := next(it, None)) is not None:
if state == 'NONE':
if (match := start_dict_re.search(x)):
dict_name = match.group(1)
parent_name = match.group(3)
api.dictionaries[dict_name] = (
parseDictionary(DictionaryApi(dict_name, parent_name), it)
)
elif (match := split_start_dict_A_re.search(x)):
dict_name = match.group(1)
state = 'SPLIT_START_DICT'
elif (match := start_enum_re.search(x)):
while (x := next(it, None)) is not None:
if end_enum_re.search(x):
break
elif (match := start_partial_interface_re.search(x)):
while (x := next(it, None)) is not None:
if end_interface_re.search(x):
break
elif (match := start_interface_re.search(x)):
while (x := next(it, None)) is not None:
if end_interface_re.search(x):
break
elif (match := start_interface_mixin_re.search(x)):
while (x := next(it, None)) is not None:
if end_interface_re.search(x):
break
elif (match := split_start_interface_A_re.search(x)):
dict_name = match.group(1)
state = 'SPLIT_START_INTERFACE'
elif (match := start_namespace_re.search(x)):
while (x := next(it, None)) is not None:
if end_namespace_re.search(x):
break
elif (match := start_typedef_re.search(x)):
while (x := next(it, None)) is not None:
if end_typedef_re.search(x):
break
elif (match := interface_attribs_re.search(x)):
continue
elif (match := typedef_re.search(x)):
continue
elif (match := typedef2_re.search(x)):
continue
elif (match := includes_re.search(x)):
continue
elif x.strip() == "":
continue
else:
logging.warning(f"Unable to parse line: {x}")
elif state == 'SPLIT_START_DICT':
if (match := split_start_dict_B_re.search(x)):
parent_name = match.group(1)
api.dictionaries[dict_name] = (
parseDictionary(DictionaryApi(dict_name, parent_name), it)
)
state = 'NONE'
else:
logging.warning(f"Unable to parse line: {x}")
elif state == 'SPLIT_START_INTERFACE':
if (match := split_start_interface_B_re.search(x)):
parent_name = match.group(1)
while (x := next(it, None)) is not None:
if end_interface_re.search(x):
break
state = 'NONE'
else:
logging.warning(f"Unable to parse line: {x}")
return api
def parseDictionary(api, it):
end_dict_re = re.compile(r"^};$")
field_re = re.compile(r"""^\s*(\w+) (\w+) = ("?[\w-]+"?);$""")
required_field_re = re.compile(r"^\s*required (\w+) (\w+);$")
logging.debug(f"Starting dict {api.name}")
while (x := next(it, None)) is not None:
if (match := end_dict_re.search(x)):
break
elif (match := field_re.search(x)):
field_type = match.group(1)
field_name = match.group(2)
field_default = match.group(3)
api.fields.append(FieldApi(field_name, field_type, default=field_default))
elif (match := required_field_re.search(x)):
field_type = match.group(1)
field_name = match.group(2)
api.fields.append(FieldApi(field_name, field_type, required=True))
logging.debug(f"Ending dict {api.name}")
return api
#------------------------------------------------
# Utils (should be shared with generator.py)
def resolveFilepath(path):
for p in [ join(dirname(__file__), path), path ]:
if isfile(p):
return p
logging.error(f"Invalid template path: {path}")
raise ValueError("Invalid template path")
def download(url, label=None):
"""Get a file's content either from a remote URL or from a local file"""
label = label + " " if label is not None else ""
if url.startswith("https://") or url.startswith("http://"):
logging.info(f"Downloading {label}from {url}...")
import urllib.request
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode("utf-8")
return text
else:
resolved = resolveFilepath(url)
logging.info(f"Loading {label}from {resolved}...")
with open(resolved, encoding="utf-8") as f:
return f.read()
class EnhancedJSONEncoder(json.JSONEncoder):
"""From https://stackoverflow.com/a/51286749"""
def default(self, o):
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
return super().default(o)
#------------------------------------------------
if __name__ == "__main__":
main(parser.parse_args())