Skip to content

Commit 80aef25

Browse files
Meade Command Wiki Parser (#116)
* Updated meade header parser for wiki * Added Example as new sub header, swapped place on command and info
1 parent 79cdca3 commit 80aef25

File tree

3 files changed

+957
-400
lines changed

3 files changed

+957
-400
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ CMakeLists.txt
77
CMakeListsPrivate.txt
88

99
Configuration_local*
10+
MeadeToWikiOutput.txt

scripts/MeadeCommandParser.py

Lines changed: 254 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
# Helper script for parsingt the header file of MEADE
2-
# Can be used to populate WIKI
1+
"""
2+
Helper script for parsingt the header file of MEADE
3+
4+
USAGE:
5+
- Ensure you have a Python interperter working for VSCode
6+
- Execute this file by pressing the little green "Play" button in the top-right corner
7+
- Output will be written to "./scripts/MeadeToWikiOutput.txt" directory
8+
- Copy entire content of the file and paste it on the wiki page
9+
"""
310

411
import os
12+
import re
513

614
MEADE_HPP = "..\\src\\MeadeCommandProcessor.cpp"
715
MODULE_PATH = os.path.dirname(os.path.realpath(__file__))
816
START_LINE = 0
917
END_LINE = 0
1018

19+
1120
class Family:
1221
def __init__(self):
1322
self.name = None
@@ -16,15 +25,104 @@ def __init__(self):
1625

1726
class Command:
1827
def __init__(self):
19-
self.command = None
20-
self.short = str()
21-
self.long = str()
22-
self.returns = str()
28+
self.__command = None
29+
self.__description = str()
30+
self.__information = list()
31+
self.__returns = list()
32+
self.__parameters = list()
33+
self.__remarks = list()
34+
self.__example = list()
35+
36+
def set_data(self, attribute, data):
37+
setattr(self, attribute, data)
38+
39+
@property
40+
def command(self):
41+
return self.__command
42+
43+
@command.setter
44+
def command(self, val):
45+
self.__command = val
46+
47+
@property
48+
def description(self):
49+
return self.__description
50+
51+
@description.setter
52+
def description(self, val):
53+
self.__description = val
54+
55+
@property
56+
def information(self):
57+
return self.__information
58+
59+
@information.setter
60+
def information(self, val):
61+
if val not in self.__information:
62+
self.__information.append(val)
63+
64+
@property
65+
def returns(self):
66+
return self.__returns
67+
68+
@returns.setter
69+
def returns(self, val):
70+
if val not in self.__returns:
71+
self.__returns.append(val)
72+
73+
@property
74+
def parameters(self):
75+
return self.__parameters
76+
77+
@parameters.setter
78+
def parameters(self, val):
79+
if val not in self.__parameters:
80+
self.__parameters.append(val)
81+
82+
@property
83+
def remarks(self):
84+
return self.__remarks
85+
86+
@remarks.setter
87+
def remarks(self, val):
88+
if val not in self.__remarks:
89+
self.__remarks.append(val)
90+
91+
@property
92+
def example(self):
93+
return self.__example
94+
95+
@example.setter
96+
def example(self, val):
97+
if val not in self.__example:
98+
self.__example.append(val)
99+
100+
101+
command_sepparators = ["Description::",
102+
"Information:",
103+
"Returns:",
104+
"Parameters:",
105+
"Remarks:",
106+
"Remarks:",
107+
"Example:",
108+
"//"]
109+
110+
111+
def remove_line_prefix(line):
112+
striped_line = line.replace("//", "").lstrip()
113+
striped_line = striped_line.replace("\"", "`")
114+
return striped_line
23115

24-
def removeLinePrefix(line):
25-
return line.replace("// ", "").strip()
116+
def check_command_sepparator(line):
117+
striped_line = line.replace("//", "").lstrip()
118+
if line in command_sepparators:
119+
return True
120+
elif line.startswith("//") and striped_line in command_sepparators:
121+
return True
122+
else:
123+
return False
26124

27-
#Meade hpp File
125+
# Meade hpp File
28126
with open(os.path.join(MODULE_PATH, MEADE_HPP)) as f:
29127
content = f.readlines()
30128
content = [x.strip() for x in content]
@@ -40,75 +138,165 @@ def removeLinePrefix(line):
40138
raise Exception("Could not locate start and stop of comment block.")
41139

42140
START_LINE = startStop[0]
43-
END_LINE = startStop[1]
141+
END_LINE = startStop[1]
44142

45143
print(("Start and end of block: {0}, {1} ".format(START_LINE, END_LINE)))
46144

47-
familyDividers = []
145+
family_dividers = []
48146
for i in range(START_LINE, END_LINE):
49147
for div in ["//------------------------------------------------------------------", "// --"]:
50148
if div in content[i]:
51-
familyDividers.append(i)
149+
family_dividers.append(i)
52150

53-
print(("Found {0} family groups ".format(len(familyDividers))))
151+
print(("Found {0} family groups ".format(len(family_dividers))))
54152

55-
allCommands = []
56-
for i in range(len(familyDividers) - 1):
57-
start = familyDividers[i]
58-
end = familyDividers[i + 1]
153+
all_commands = []
154+
for i in range(len(family_dividers) - 1):
155+
#for i in range(0, 6):
156+
start = family_dividers[i] + 1
157+
end = family_dividers[i + 1]
158+
159+
new_family = Family()
160+
new_family.name = remove_line_prefix(content[start])
161+
162+
# Find command groups
163+
sub_offsets = list()
164+
sub_offsets.append(start+2)
165+
for j in range(start+2, end):
166+
if content[j] == "//":
167+
sub_offsets.append(j)
59168

60-
newFamily = Family()
61-
if "//------------------------------------------------------------------" in content[start]:
62-
newFamily.name = removeLinePrefix(content[start + 1])
63-
elif "// --" in content[start]:
64-
nameCleanup = content[start].replace("// -- ", "")
65-
nameCleanup = nameCleanup.replace(" --", "")
66-
newFamily.name = nameCleanup
67-
68-
for y in range(start + 1, end - 1):
69-
newCommand = Command()
70-
71-
# Command
72-
if content[y].startswith("// :"):
73-
newCommand.command = removeLinePrefix(content[y])
74-
75-
# Short Description
76-
newCommand.short = removeLinePrefix(content[y + 1])
77-
y+=2
78-
79-
k = y
80-
while not content[k].startswith("// Returns:"):
81-
newCommand.long += removeLinePrefix(content[k])
82-
k += 1
83-
y = k
169+
for k in range(0, len(sub_offsets)-1):
170+
command = Command()
171+
sub_start = sub_offsets[k]
172+
sub_end = sub_offsets[k+1]
84173

174+
for l in range(sub_start, sub_end):
175+
if content[l] == "//":
176+
continue
177+
178+
# Command
179+
if content[l].startswith("// :"):
180+
command.command = remove_line_prefix(content[l])
181+
182+
# Description
183+
if content[l].startswith("//") and "Description:" in content[l]:
184+
command.description = remove_line_prefix(content[l+1])
185+
186+
# Information
187+
if content[l].startswith("//") and "Information:" in content[l]:
188+
m = l+1
189+
while not check_command_sepparator(content[m]):
190+
command.information = remove_line_prefix(content[m])
191+
m += 1
192+
l = m
193+
194+
# Returns
195+
if content[l].startswith("//") and "Returns:" in content[l]:
196+
m = l+1
197+
while not check_command_sepparator(content[m]):
198+
command.returns = remove_line_prefix(content[m])
199+
m += 1
200+
l = m
201+
202+
# Remarks
203+
if content[l].startswith("//") and "Remarks:" in content[l]:
204+
m = l+1
205+
while not check_command_sepparator(content[m]):
206+
command.remarks = remove_line_prefix(content[m])
207+
m += 1
208+
l = m
209+
210+
# Parameters
211+
if content[l].startswith("//") and "Parameters:" in content[l]:
212+
m = l+1
213+
while not check_command_sepparator(content[m]):
214+
command.parameters = remove_line_prefix(content[m])
215+
m += 1
216+
l = m
217+
218+
# Example
219+
if content[l].startswith("//") and "Example:" in content[l]:
220+
m = l+1
221+
while not check_command_sepparator(content[m]):
222+
command.example = remove_line_prefix(content[m])
223+
m += 1
224+
l = m
85225

86-
if content[y].startswith("// Returns:"):
87-
newCommand.returns += content[y].replace("// Returns: ", "")
88-
k = y+1
89-
while content[k] != "//":
90-
newCommand.returns += content[k].replace("// ", " ").strip()
91-
k += 1
92-
y = k
93-
94-
if newCommand.command:
95-
newFamily.commands.append(newCommand)
226+
new_family.commands.append(command)
227+
all_commands.append(new_family)
228+
229+
def output_wiki():
230+
"""
231+
Writes content to a MeadeToWikiOutput.txt file
232+
"""
96233

97-
allCommands.append(newFamily)
234+
f = open("./scripts/MeadeToWikiOutput.txt", "w")
235+
236+
for fam in all_commands:
237+
f.write(f"## {fam.name}\n")
238+
f.write("<br>\n\n")
239+
240+
for cmd in fam.commands:
241+
f.write(f"### {cmd.description}\n")
242+
243+
if cmd.information:
244+
#f.write("**Information:**\n")
245+
for line in cmd.information:
246+
f.write(f"{line}")
247+
f.write("\n\n")
248+
249+
f.write(f"**Command:**\n")
250+
f.write(f"`{cmd.command}`\n")
251+
f.write("\n")
252+
253+
f.write("**Returns:**\n")
254+
for line in cmd.returns:
255+
f.write(f"- {line}\n")
256+
f.write("\n")
257+
258+
if cmd.parameters:
259+
f.write("**Parameters:**\n")
260+
for param in cmd.parameters:
261+
f.write(f"- {param}\n")
262+
f.write("\n")
98263

264+
if cmd.remarks:
265+
f.write("**Remarks:**\n")
266+
for param in cmd.remarks:
267+
f.write(f"{param}\n")
268+
f.write("\n")
99269

100-
# Example of printing output
101-
for fam in allCommands:
102-
print("***** {0} *****".format(fam.name))
103-
print("Command Count: {0}".format(len(fam.commands)))
270+
if cmd.example:
271+
f.write("**Example:**\n")
272+
for param in cmd.example:
273+
f.write(f"- {param}\n")
274+
f.write("\n")
275+
276+
f.write("<br>")
277+
f.write("\n")
278+
f.write("\n")
279+
280+
f.write("\n\n")
281+
282+
f.close()
283+
print("File written to: ./scripts/MeadeToWikiOutput.txt")
284+
285+
if __name__ == "__main__":
286+
output_wiki()
287+
288+
"""
289+
# Output Excample
290+
for fam in all_commands:
291+
print("-----")
292+
print(fam.name)
104293
for cmd in fam.commands:
105-
print("\tCommand: {0}".format(cmd.command))
106-
print("\tShort: {0}".format(cmd.short))
107-
print("\tLong Description: {0}".format(cmd.long))
108-
print("\tReturns: {0}".format(cmd.returns))
109-
print("\r")
110-
111-
print("Family Count: {0}".format(len(allCommands)))
112-
for fam in allCommands:
113-
print("{0}".format(fam.name))
114-
print("\t{0}".format(len(fam.commands)))
294+
print("############################")
295+
print(f"Command: {cmd.command}")
296+
print(f"Description: {cmd.description}")
297+
print(f"Information: {cmd.information}")
298+
print(f"Returns: {cmd.returns}")
299+
print(f"Parameters: {cmd.parameters}")
300+
print(f"Remarks: {cmd.remarks}")
301+
302+
"""

0 commit comments

Comments
 (0)