-
Notifications
You must be signed in to change notification settings - Fork 1
/
readoc_parser.py
186 lines (155 loc) · 6.68 KB
/
readoc_parser.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
from typing import List, Tuple, Optional
import json
from abc import ABC, abstractmethod
from readoc import *
class ReaDocParser(ABC):
"""
Parser abstract class, 'parse' method must be implemented.
"""
def __init__(self, readoc: ReaDoc, **kwargs):
self.output = self.parse(readoc, **kwargs)
@abstractmethod
def parse(self, readoc: ReaDoc, **kwargs) -> str:
"""
Use to set the 'output' attribute, must implement in child class
"""
pass
def export(self, output_file: str):
text_file = open(output_file, "w")
text_file.write(self.output)
text_file.close()
class VscParser(ReaDocParser):
"""
VisualStudio Code Parser
"""
def __init__(self, readoc: ReaDoc, **kwargs):
super().__init__(readoc, **kwargs)
self.pretty: bool = True
self.opti_lang: bool = True
def parse(self, readoc: ReaDoc, **kwargs) -> str:
if kwargs is not None:
if 'pretty' in kwargs:
self.pretty = kwargs['pretty']
if 'opti_lang' in kwargs:
self.opti_lang = kwargs['opti_lang']
doc: dict = dict()
for func in readoc.functions:
func_prop = self.get_function_dict(func)
if func_prop is not None:
doc[func.name.upper() + ' ' + func.lang] = func_prop
for func in readoc.functions:
func_prop = self.get_function_dict(func, True)
if func_prop is not None:
doc[func.name.upper() + '_WR ' + func.lang] = func_prop
for key, keyword in readoc.keywords.items():
if self.opti_lang and keyword.languages == list(languages.values()):
keyword_prop: dict = dict()
keyword_prop['prefix'] = keyword.name
keyword_prop['body'] = keyword.name
keyword_prop['description'] = keyword.desc + "\n"
doc[keyword.name.upper()] = keyword_prop
else:
for lang in keyword.languages:
keyword_prop: dict = dict()
keyword_prop['prefix'] = keyword.name
keyword_prop['scope'] = lang
keyword_prop['body'] = keyword.name
keyword_prop['description'] = keyword.desc + "\n"
doc[keyword.name.upper() + ' ' + lang] = keyword_prop
for key, alias in readoc.aliases.items():
alias_prop: dict = dict()
alias_prop['prefix'] = alias.alias
alias_prop['body'] = alias.name
alias_prop['description'] = alias.desc + "\n"
doc[alias.alias] = alias_prop
if self.pretty:
return json.dumps(doc, indent=4, separators=(',', ': '))
else:
return json.dumps(doc)
@staticmethod
def get_function_dict(func: FunctionDoc, retval: bool = False) -> Optional[dict]:
"""Convert a FunctionDoc to a dict
:param func: FunctionDoc to parse
:param retval: True if 'with return' function, false otherwise
:return: Function dict
"""
if len(func.returns) == 0 and retval: # Skip if useless
return None
func_prop: dict = dict()
func_prop['prefix'] = func.name
if func.lang == 'lua':
func_prop['prefix'] = func.name
if retval:
func_prop['prefix'] = func_prop['prefix'].replace('reaper.', 'reaperwr.')
func_prop['prefix'] = func_prop['prefix'].replace('ultraschall.', 'ultraschallwr.')
elif retval:
func_prop['prefix'] = 'WR_' + func_prop['prefix']
func_prop['scope'] = func.lang
func_prop['description'] = func.get_full_desc() + "\n"
body: str = ""
i: int = 1
if retval:
if func.lang == 'lua':
body += '${' + str(i) + ':local }'
i += 1
body_core, i = VscParser.get_body_core(func.returns, i)
body += body_core
body += ' = '
body += func.name + '('
body_core, i = VscParser.get_body_core(func.params, i)
body += body_core
body += ')$0'
func_prop['body'] = body
return func_prop
@staticmethod
def get_body_core(variables: List[VariableDoc], i: int) -> (str, int):
body_core: str = ""
for variable in variables:
body_core += '${' + str(i)
if len(variable.values) > 0:
body_core += '|' + variable.type + (' ' + variable.name if variable.name != '' else '') + ','
for value in variable.values:
body_core += '"' + value + '"' + ','
body_core = body_core[:-1] # Remove last comma
body_core += '|'
else:
body_core += ':' + variable.type + (' ' + variable.name if variable.name != '' else '')
body_core += '}'
body_core += ',' if variable != variables[-1] else ''
i += 1
return body_core, i
class RawParser(ReaDocParser):
"""
Raw parser
"""
def __init__(self, readoc: ReaDoc, **kwargs):
super().__init__(readoc, **kwargs)
def parse(self, readoc: ReaDoc, **kwargs) -> str:
output: str = "FUNCTIONS:\n" if len(readoc.functions) > 0 else ''
for func in readoc.functions:
output += "name:" + func.name + "\n"
output += "language:" + func.lang + "\n"
output += "return:"
for ret in func.returns:
output += "(" + ret.type + ":" + ret.name + ")"
output += "," if ret != func.returns[-1] else ""
output += "\nparams:"
for param in func.params:
output += "(" + param.type + ":" + param.name + ")"
output += ", " if param != func.params[-1] else ""
for param in func.params:
if len(param.values) > 0:
output += "\nValid input for " + param.name + ": "
for value in param.values:
output += value
output += ", " if value != param.values[-1] else ""
output += "\n"
output += "\ndescription:\n" + func.get_full_desc()
output += "\n------\n"
output += "KEYWORDS:\n" if len(readoc.keywords) > 0 else ''
for key, keyword in readoc.keywords.items():
output += keyword.name + ":" + keyword.desc + " / languages:" + ','.join(keyword.languages) + "\n"
output += "ALIASES:\n" if len(readoc.aliases) > 0 else ''
for key, alias in readoc.aliases.items():
output += alias.alias + "->" + alias.name + ":" + alias.desc + "\n"
return output