-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdoxy_parse.py
More file actions
406 lines (331 loc) · 13.2 KB
/
doxy_parse.py
File metadata and controls
406 lines (331 loc) · 13.2 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from xml.etree import ElementTree
from os import listdir
from os.path import isfile, join
import json
class DescriptionMetadata:
text = ''
def __init__(self):
self.attributes = {}
def parse(self, n: ElementTree.Element):
if n is None:
return
for para in n.findall('para'):
text = self.do_parse_text(para)
if len(text) > 0:
if text.startswith('<'):
try:
ele = ElementTree.fromstring('<root>' + text + '</root>')
for tag in list(ele):
desc = {'text': tag.text}
for k,v in tag.attrib.items():
desc[k] = v
self.attributes[tag.tag] = desc
except ElementTree.ParseError:
print('Warning: Failed to parse annotation: ' + text)
self.text += self.parse_text(n)
def tag_wrap(self, text: str, tag: str):
if tag == 'computeroutput':
return '`' + text + '`'
if tag == 'sp':
return ' ' + text
if tag == 'listitem':
return " - " + text
if tag == 'programlisting':
return "\r\n```lua\r\n" + text + "```"
return text
def parse_text(self, n: ElementTree.Element):
if n.tag == 'para' and n.text is not None and n.text.startswith('<'):
return ''
return self.do_parse_text(n)
def do_parse_text(self, n: ElementTree.Element):
text = ''
if n.text is not None and n.tag != 'briefdescription' and n.tag != 'detaileddescription' and n.tag != 'parameterdescription':
text += n.text
for ele in list(n):
if ele.tag != 'parameterlist' and ele.tag != 'simplesect':
text += self.parse_text(ele)
if ele.tail is not None:
text += ele.tail
return self.tag_wrap(text, n.tag)
def parse_description_metadata(n: ElementTree.Element) -> DescriptionMetadata:
meta = DescriptionMetadata()
meta.parse(n.find('briefdescription'))
meta.parse(n.find('detaileddescription'))
meta.parse(n.find('parameterdescription'))
return meta
class FunctionParamDefn:
type = None
name = ''
description = ''
is_internal = False
def from_xml(self, n: ElementTree.Element):
self.type = n.find('type').text
self.name = n.find('declname').text
self.description = ''
self.is_internal = (self.name == 'L')
def to_json(self):
return {
"name": self.name,
"type": self.type,
"description": self.description
}
class FunctionDefn:
inspected = False
name = None
namespace = None
exported = False
export_name = None
qualified_name = None
description = None
params = None
definition = None
implementation_file = None
implementation_line = None
def from_xml(self, n: ElementTree.Element, ns):
self.namespace = ns
desc = parse_description_metadata(n)
if 'lua_export' in desc.attributes:
self.exported = True
self.description = desc.text
self.parse_definition(n)
if self.exported:
self.export_name = desc.attributes['lua_export']['text'] or self.name
else:
self.export_name = self.name
def dump(self):
print('Exported function: ' + self.namespace.name + '::' + self.name + ' -> ' + (self.namespace.export_name or self.namespace.name) + '.' + self.export_name)
print('Description: ' + self.description.strip())
print('')
for param in list(self.params):
if not param.is_internal:
print(' - Param ' + param.name + ': ' + (param.description or '').strip())
def find_param_list(self, n: ElementTree.Element):
for para in n.find('detaileddescription').findall('para'):
params = para.find('parameterlist')
if params is not None:
return params
return None
def parse_definition(self, n: ElementTree.Element):
self.inspected = True
# FIXME - parse ref?
# <type><ref refid="structdse_1_1esv_1_1_surface_action" kindref="compound">SurfaceAction</ref> *</type>
self.type = n.find('type').text
self.name = n.find('name').text
self.definition = n.find('definition').text + n.find('argsstring').text
self.qualified_name = n.find('qualifiedname').text
loc = n.find('location')
if 'bodyfile' in loc.attrib:
self.implementation_file = loc.attrib['bodyfile']
self.implementation_line = int(loc.attrib['bodystart'])
self.params = []
for param in n.findall('param'):
name = param.find('declname')
# Lua context is an internal parameter only
if name is not None:
paramDesc = FunctionParamDefn()
paramDesc.from_xml(param)
self.params.append(paramDesc)
paramsByName = {p.name: p for p in self.params}
params = self.find_param_list(n)
if params is not None:
for param in params.findall('parameteritem'):
name = param.find('parameternamelist').find('parametername').text
paramDesc = paramsByName.get(name)
if paramDesc is None:
paramDesc = FunctionParamDefn()
paramDesc.name = name
self.params.append(paramDesc)
description = parse_description_metadata(param)
paramDesc.description = description.text
def to_json(self):
defn = {
"name": self.export_name or self.name,
"qualified_name": self.qualified_name,
"description": self.description,
"definition": self.definition,
"implementation_file": self.implementation_file,
"implementation_line": self.implementation_line,
"params": []
}
for param in self.params:
if not param.is_internal:
defn["params"].append(param.to_json())
return defn
def has_any_annotations(self) -> bool:
return self.exported or (self.description is not None and len(self.description) > 0) or (self.params is not None and len(self.params) > 0)
class PropertyDefn:
name = None
type = None
description = None
def from_xml(self, n: ElementTree.Element):
self.type = n.find('type').text
self.name = n.find('name').text
desc = parse_description_metadata(n)
self.description = desc.text
def to_json(self):
return {
"name": self.name,
"type": self.type,
"description": self.description
}
def has_any_annotations(self) -> bool:
return self.description is not None and len(self.description) > 0
class NamespaceDefn:
name = ''
exported = False
export_name = None
lua_name = None
def __init__(self):
self.functions = {}
self.classes = {}
self.class_names = []
def from_xml(self, n: ElementTree.Element):
self.name = n.find('compoundname').text
desc = parse_description_metadata(n)
if 'lua_module' in desc.attributes:
self.exported = True
self.export_name = desc.attributes['lua_module']['text']
if self.name[0:8] == "dse::ecl":
self.lua_name = "Client" + self.export_name
elif self.name[0:8] == "dse::esv":
self.lua_name = "Server" + self.export_name
else:
self.lua_name = self.export_name
self.parse_definition(n)
def parse_definition(self, n: ElementTree.Element):
for section in list(n):
if section.tag == 'sectiondef' and section.attrib['kind'] == 'func':
self.parse_function_section(section)
if section.tag == 'innerclass':
self.class_names.append(section.attrib['refid'])
def parse_function_section(self, n: ElementTree.Element):
for memberdef in list(n):
if memberdef.tag == 'memberdef' and memberdef.attrib['kind'] == 'function':
fun = FunctionDefn()
fun.from_xml(memberdef, self)
if fun.exported or self.exported:
self.functions[fun.name] = fun
def to_json(self):
defns = {
"name": self.name,
"functions": {},
"classes": {}
}
for fun in self.functions.values():
if fun.exported or self.exported:
defns["functions"][fun.export_name] = fun.to_json()
for cls in self.classes.values():
if cls.has_any_annotations():
defns["classes"][cls.name] = cls.to_json()
return defns
class ClassDefn:
name = ''
export_name = ''
exported = False
def __init__(self):
self.properties = {}
self.methods = {}
def from_xml(self, n: ElementTree.Element):
self.name = n.find('compoundname').text
desc = parse_description_metadata(n)
# FIXME - filter for only exported classes!
self.exported = True
if 'lua_export' in desc.attributes:
self.exported = True
if self.exported:
self.parse_definition(n)
def dump(self):
print('Exported class: ' + self.name)
for n,method in self.methods.items():
print(method)
for n,prop in self.properties.items():
print(prop.name, prop.type, prop.description)
def parse_definition(self, n: ElementTree.Element):
for section in list(n):
if section.tag == 'sectiondef' and section.attrib['kind'] == 'public-attrib':
self.parse_property_section(section)
if section.tag == 'sectiondef' and section.attrib['kind'] == 'public-func':
self.parse_function_section(section)
def parse_function_section(self, n: ElementTree.Element):
for memberdef in list(n):
if memberdef.tag == 'memberdef' and memberdef.attrib['kind'] == 'function':
fun = FunctionDefn()
fun.from_xml(memberdef, self)
if fun.has_any_annotations():
self.methods[fun.name] = fun
def parse_property_section(self, n: ElementTree.Element):
for memberdef in list(n):
if memberdef.tag == 'memberdef' and memberdef.attrib['kind'] == 'variable':
prop = PropertyDefn()
prop.from_xml(memberdef)
self.properties[prop.name] = prop
def has_any_annotations(self):
for prop in self.properties.values():
if prop.has_any_annotations():
return True
for fun in self.methods.values():
if fun.has_any_annotations():
return True
return False
def to_json(self):
defns = {
"name": self.export_name or self.name,
"methods": {},
"properties": {}
}
for fun in self.methods.values():
defns["methods"][fun.export_name] = fun.to_json()
for prop in self.properties.values():
if prop.description is not None and len(prop.description) > 0:
defns["properties"][prop.name] = prop.to_json()
return defns
class DoxyMetadata:
def __init__(self):
self.namespaces = {}
self.classes = {}
def parse_namespace(self, n: ElementTree.Element):
name = n.find('compoundname').text
ns = self.namespaces.get(name) or NamespaceDefn()
ns.from_xml(n)
if ns.exported:
self.namespaces[name] = ns
def parse_struct(self, n: ElementTree.Element):
name = n.find('compoundname').text
ns = self.classes.get(name) or ClassDefn()
ns.from_xml(n)
if ns.exported:
self.classes[name] = ns
def parse_doxygen_xml(self, path):
tree = ElementTree.parse(path)
root = tree.getroot()
for doxydef in root:
if doxydef.tag == 'compounddef':
if doxydef.attrib['kind'] == 'namespace':
self.parse_namespace(doxydef)
if doxydef.attrib['kind'] == 'struct' or doxydef.attrib['kind'] == 'class':
self.parse_struct(doxydef)
def parse_doxygen_dir(self, path):
for f in listdir(path):
f = join(path, f)
if isfile(f):
self.parse_doxygen_xml(f)
def to_json(self):
defns = {
"modules": {},
"classes": {}
}
for ns in self.namespaces.values():
if ns.exported:
defns["modules"][ns.lua_name or ns.name] = ns.to_json()
for cls in self.classes.values():
if cls.has_any_annotations():
defns["classes"][cls.name] = cls.to_json()
return defns
def lua_export(self, path: str):
lua = json.dumps(self.to_json())
lua = "return [[" + lua + "]]"
with open(path, 'w') as f:
f.write(lua)
meta = DoxyMetadata()
meta.parse_doxygen_dir('ScriptExtenderTypeGen/Doxygen/xml')
meta.lua_export('ScriptExtender/LuaScripts/Libs/IdeHelpersNativeData.lua')