-
Notifications
You must be signed in to change notification settings - Fork 8
/
rev_eng_params.py
80 lines (66 loc) · 2.89 KB
/
rev_eng_params.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
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
curDir = Path(__file__).absolute().parent
zmqRemoteApiToolsDir = curDir.parent / 'programming' / 'zmqRemoteApi' / 'tools'
if zmqRemoteApiToolsDir.is_dir():
sys.path.append(str(zmqRemoteApiToolsDir))
else:
sys.stderr.write('zmqRemoteApi/tools directory missing\n')
sys.exit(1)
import lark
from calltip import FuncDef, Arg, ArgDef
tree = ET.parse(curDir / 'regularApi.xml')
root = tree.getroot()
for func in root:
name = func.find('api-function-name').text.strip()
calltip = func.find('api-calltip')
api_in = func.find('api-input-python-lua')
api_out = func.find('api-output-python-lua')
if api_in is None or api_out is None:
print(f'warning: function "{name}" lacks <api-input-python-lua> or <api-output-python-lua>')
continue
li_in, li_out = {}, {}
for li in api_in.findall('li'):
s = li.find('strong')
if s is None:
print(f'warning: function "{name}" <api-input...> <li> lacks a <strong> element')
n = s.text.strip()
li_in[n] = li
for li in api_out.findall('li'):
s = li.find('strong')
if s is None:
print(f'warning: function "{name}" <api-output...> <li> lacks a <strong> element')
n = s.text.strip()
li_out[n] = li
if calltip is not None:
calltip = calltip.text.strip()
for i, c in enumerate(calltip.splitlines()):
if not c: break
try:
fd = FuncDef.from_calltip(c)
in_node = ET.SubElement(func, 'api-params')
for arg in fd.in_args:
p_node = ET.SubElement(in_node, 'param')
p_node.set('name', arg.name)
p_node.set('type', arg.type)
if isinstance(arg, ArgDef):
p_node.set('default', str(arg.default))
if arg.name not in li_in:
print(f'warning: function "{name}" calltip param "{arg.name}" not present in <api-input...>')
out_node = ET.SubElement(func, 'api-returns')
for arg in fd.out_args:
p_node = ET.SubElement(out_node, 'param')
p_node.set('name', arg.name)
p_node.set('type', arg.type)
if isinstance(arg, ArgDef):
p_node.set('default', str(arg.default))
if arg.name not in li_out:
print(f'warning: function "{name}" calltip param "{arg.name}" not present in <api-output...>')
ET.indent(in_node, ' ' * 4, 2)
ET.indent(out_node, ' ' * 4, 2)
except lark.exceptions.LarkError as e:
print(f'error: function "{name}": cannot parse calltip {i} ({c!r}): {e}')
else:
print(f'warning: no calltip for function "{name}"')
#ET.dump(tree)