-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments_extractor.py
121 lines (94 loc) · 3.21 KB
/
arguments_extractor.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
import re
import os
import sys
decompilesPath = os.path.abspath("decompiles")
generatedPath = os.path.abspath("generated")
if not os.path.isdir(decompilesPath):
os.makedirs(decompilesPath)
if not os.path.isdir(generatedPath):
os.makedirs(generatedPath)
def extractOptionsToMarkdown(name: str):
source = os.path.join(decompilesPath, name + ".java")
destination = os.path.join(generatedPath, name + ".md")
print(f"extracting {source} to {destination}")
with open(source, 'r') as file:
content = file.read()
options = extractOptionsFromJavaCode(content)
markdown = generateMarkdownDocument(name, options)
with open(destination, 'w') as file:
file.write(markdown)
def extractOptionsFromJavaCode(source: str) -> list:
options = []
matches = re.findall(r'(accepts.*);', source)
for specs in matches:
if not specs:
continue
option = extractOptionSpec(specs)
options.append(option)
return options
def extractOptionSpec(specs: str) -> dict:
result = {}
state = "methodName"
methodName = ""
argument = ""
bracketLevel = 0
for char in specs:
if char == ' ' or char == '\n' or char == '\r':
pass
elif char == '(':
bracketLevel += 1
if bracketLevel == 1:
state = "argument"
continue
elif char == ')':
bracketLevel -= 1
if bracketLevel == 0:
if not argument:
argument = True
result[methodName] = argument
state = "methodName"
methodName = ""
argument = ""
continue
elif char == '.':
if bracketLevel == 0:
continue
if state == "methodName":
methodName += char
elif state == "argument":
argument += char
return result
def generateMarkdownDocument(name: str, options: list) -> str:
table = optionsToMarkdownTable(options)
return f'''
# {name}
{table}
generated by arguments_extractor.py
'''
def optionsToMarkdownTable(options: list) -> str:
result = ['''
| name | type | default | isRequired | others | description |
|------|------|---------|------------|--------|-------------|
''']
for opt in options:
name = opt.get('accepts').strip('"')
isRequired = opt.get('withRequiredArg') or opt.get('required')
if not isRequired:
isRequired = False
subs = opt.keys() - ["accepts", "ofType", "defaultsTo", "withRequiredArg", "required"]
result.append(f"| {name} | {opt.get('ofType')} | {opt.get('defaultsTo')} | {isRequired} | {', '.join(subs)} | ~ |\n")
return ''.join(result)
if __name__ == "__main__":
if not len(sys.argv) == 2 or not sys.argv[1]:
print("specify name")
exit()
#name = "all"
name = sys.argv[1]
if name == "all":
for filename in os.listdir(decompilesPath):
if not os.path.isfile(os.path.join(decompilesPath, filename)):
continue
extractOptionsToMarkdown(os.path.splitext(filename)[0])
else:
extractOptionsToMarkdown(name)
print("done")