-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure_builder.py
277 lines (243 loc) · 14.2 KB
/
structure_builder.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
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
import os, re
import info
# Set project directory to current directory
output_instructions = True
if not os.path.exists(info.data["requirements_folder"]):
os.mkdir(info.data["requirements_folder"])
if not os.path.exists(info.data["storage_folder"]):
os.mkdir(info.data["storage_folder"])
output = ""
documentation = ""
# Iterate through instructions folder
for file in sorted(os.listdir("instructions")):
with open(f"instructions/{file}", "r") as f:
output += "=" * 30 + "\n\n"
output += "-=[" + file.replace(".txt", "").replace("_", " ").upper() + "]=-\n\n"
output += f.read() + "\n\n"
if output_instructions:
def format_for_readme(text, document_path=""):
global documentation
if document_path != "":
documentation += f"## {document_path.split('/')[-1].replace('-', ' ')} ##\n\n"
new_text = ""
for text_line in text.split("\n"):
if text_line.startswith("-=["):
new_text += f"## {text_line[3:-3]} ##\n" + "\n"
if document_path != "":
documentation += f"[{text_line[3:-3]}](/{document_path}#{text_line[3:-3].lower().replace(' ', '-').replace('.', '')})\n\n"
elif len(text_line) > 0 and text_line[0] in "1234567890":
new_text += f"### {text_line} ###\n" + "\n"
# new_text += line + "\n" + "\n"
elif text_line.startswith("=="):
new_text += "\n"
elif len(text_line) == 0:
new_text += "\n"
else:
new_text += text_line + "\n" + "\n"
return new_text
keys = info.data.keys()
for key in keys:
search_item2 = "!<" + key + ">"
if search_item2 in output:
# Replace with the value
output = output.replace(search_item2, info.data[key])
turn_to_readme = format_for_readme(output, document_path="docs/INSTRUCTIONS.md")
with open("README.md", "w") as f:
# Add all lines from ABOUT.md to README.md
with open("ABOUT.md", "r") as about:
for line in about.readlines():
for key, value in info.data.items():
line = line.replace("<" + key + ">", str(value))
f.write(line)
f.write("\n\n")
f.write("""## DOCUMENTATION\n\n""")
f.write("[For Documentation, Click Here](docs/DOCS.md)\n\n")
with open(info.data["docs_folder"] + "/INSTRUCTIONS.md", "w") as f:
# Provide link to go back to DOCS.md
f.write(f"[Back to DOCS.md](DOCS.md)\n\n")
f.write(turn_to_readme)
documentation += f"# API #\n\n"
# walk through every .py file in the toolbox folder
for root, dirs, files in os.walk(info.data["project_dir"]):
for file in files:
if file[0] == ".":
continue
if file.endswith(".py"):
# open the file
with open(os.path.join(root, file), "r") as f:
# Get path between project directory and file
file_path = os.path.join(root, file).replace(info.data["project_dir"], "")
if file_path.startswith("/"):
file_path = file_path[1:]
folders = str(file_path).split("/")
last_part = folders[-1].split(".")[0]
file_document_path = info.data["docs_folder_dir"] + "/" + (str(file_path).strip().replace("/", "-").replace("\\", "-").upper().split(".PY")[0]) + ".md"
file_document_path = file_document_path.strip()
if len(file_document_path.split("/")) > 1 and file_document_path.split("/")[-1].startswith("-"):
file_document_path = file_document_path.split("/")[0] + "/" + file_document_path.split("/")[1][1:]
print(file_document_path)
file2 = open(file_document_path, "w")
# Provide link to go back to DOCS.md
file2.write(f"[Back to DOCS.md](DOCS.md)\n\n")
# Turn into a "from ... import ..." statement
join_stuff = ".".join(folders[:-1])
if len(join_stuff) == 0:
import_statement = "import " + last_part
else:
import_statement = "from " + ".".join(folders[:-1]) + " import " + last_part
file2.write(f"Import Statement: `{import_statement}`\n\n")
# Turn into a "from ... import *" statement
# Remove extension first
folders[-1] = folders[-1].split(".")[0]
import_statement = "from " + ".".join(folders) + " import *"
file2.write(f"Alternative Import Statement: `{import_statement}`\n\n")
# read the file as a list
lines = f.readlines()
def get_function_name(text_line):
return text_line.strip().split("def ")[1].split("(")[0]
def get_class_name(text_line):
return text_line.strip().split("class ")[1].split("(")[0].split(":")[0]
def count_spaces_at_beginning(text_line):
count = 0
for c in text_line:
if c == " ":
count += 1
else:
break
return count
def get_function_documentation(k, offset=0):
if len(lines) <= k or lines[k].strip().startswith("def "):
return ""
if lines[k].strip() == "\"\"\"":
docs = ""
j = k + 1
while j < len(lines) and lines[j].strip() != "\"\"\"":
docs += lines[j]
j += 1
return docs
else:
if offset > 10:
return ""
else:
return get_function_documentation(k + 1, offset=offset + 1)
def get_class_documentation(k, offset=0):
if len(lines) <= k or lines[k].strip().startswith("class "):
return ""
if lines[k].strip() == "\"\"\"":
docs = ""
j = k + 1
while j < len(lines) and lines[j].strip() != "\"\"\"":
docs += lines[j]
j += 1
return docs
else:
if offset > 10:
return ""
else:
return get_class_documentation(k + 1, offset=offset + 1)
def document_data(i, name, line, docs, parent_string="", obj_type="function", spaces = "> "):
# Remove underscores from front and back only, not in the middle
name = name.strip("_")
name = parent_string + name
file2.write(f"# {spaces} {obj_type + ' ' + name} #\n\n")
class_declaration = line
file2.write(f"### [{class_declaration.strip()}](./../{file_path}#L{i + 1}) \n\n")
file_documentation = f"/{file_document_path}#{obj_type}-{name.lower().replace(' ', '-').replace('.', '')}"
writing_header = f"### {spaces}[{obj_type + ' ' + name}]({file_documentation}) \n\n"
documents = get_class_documentation(i + 1)
sections = ["Note", "Param", "Return", "Example", "Reference", "Desc"]
new_documentation = ""
for section in sections:
if section in documents:
file2.write(section + "\n\n")
new_documentation += section + "\n\n"
sect_back = documents.find(section) + len(section) + 1
while documents[sect_back] == "\n" or documents[sect_back] == "-":
sect_back += 1
sect_front = 9999999
for sect2 in sections:
if sect2 == section:
continue
sect_front2 = documents.find(sect2)
if sect_front2 != -1:
if sect_front > sect_front2 > sect_back:
sect_front = sect_front2
section_combined = documents[sect_back:sect_front].strip()
# remove first line if its first character is a dash
if section_combined[0] == "-":
section_combined = section_combined.split("\n", 1)[1]
file2.write("```python\n" + section_combined + "\n```\n\n")
new_documentation += "\n```python\n" + section_combined + "\n```\n\n"
class_declaration_reference = f"[{class_declaration.strip()}](./../{file_path}#L{i + 1}) \n\n"
# Add dropdown to docs with the documentation
docs += f"\n <details>\n<summary>\n\n{writing_header}\n\n</summary>\n\n{class_declaration_reference + new_documentation}\n\n"
# Identify level of tabbing for any statements after the class
tab_level2 = 0
for j in range(i+1, len(lines)):
# If there is text, then set the tab level
if len(lines[j].strip()) > 0:
tab_level2 = count_spaces_at_beginning(lines[j])
break
# Locate functions and classes within the class, if their tab level is equal to tab_level2
# Once the tab level is less than tab_level2, then we know we have reached the end of the class
identified_functions_or_classes = False
for j in range(i+1, len(lines)):
if count_spaces_at_beginning(lines[j]) == tab_level2:
if lines[j].strip().startswith("def ") or lines[j].strip().startswith("class "):
identified_functions_or_classes = True
break
if count_spaces_at_beginning(lines[j]) < tab_level2:
# if not empty
if len(lines[j].strip()) > 0:
break
if identified_functions_or_classes:
file2.write(f"\n <details>\n<summary>\n\n#### Functions and Classes\n\n</summary>\n\n")
for j in range(i+1, len(lines)):
if count_spaces_at_beginning(lines[j]) == tab_level2:
if lines[j].strip().startswith("def "):
name2 = get_function_name(lines[j])
docs = document_data(j, name2, lines[j], docs, parent_string=name+".", obj_type="function", spaces=spaces + " " + spaces.split(" ")[0] + " ")
elif lines[j].strip().startswith("class "):
name2 = get_class_name(lines[j])
docs = document_data(j, name2, lines[j], docs, parent_string=name+".", obj_type="class", spaces=spaces + " " + spaces.split(" ")[0] + " ")
if count_spaces_at_beginning(lines[j]) < tab_level2:
# if not empty
if len(lines[j].strip()) > 0:
break
file2.write(f"</details>\n\n")
docs += "</details>\n\n"
return docs
found = False
other_docs = ""
for i, line in enumerate(lines):
# If we find a class definition
if line.startswith("class"):
found = True
name = get_class_name(line)
other_docs = document_data(i, name, line, other_docs, obj_type="class")
# If we find a function definition
elif line.startswith("def"):
found = True
name = get_function_name(line)
other_docs = document_data(i, name, line, other_docs, obj_type="function")
if found:
# Provide link to md file
documentation += f"\n<details>\n<summary>\n\n## Documentation For [{file_path}](/{file_document_path})\n\n</summary>\n\n{other_docs}<br></details>\n\n"
file2.close()
with open(info.data["docs_folder"] + "/DOCS.md", "w") as f:
# Add the capability to go back to README.md
f.write(f"[Back to README.md](/README.md)\n\n")
f.write("# DOCUMENTATION TABLE OF CONTENTS #\n\n")
f.write(f"This is the documentation for the project {info.data['project_name']}.\n\n")
f.write(documentation)
keys = info.data.keys()
for key in keys:
search_item = "<" + key + ">"
if search_item in output:
# Replace with the value
output = output.replace(search_item, info.data[key])
# Remove blank lines from output
output = "\n".join([s for s in output.splitlines() if s.strip() != ""])
# Color the lines (like === and ---) and numbers like 1. 2. 3. etc. red
output = re.sub(r"^(=+)$", r"\033[91m\1\033[0m", output, flags=re.MULTILINE)
print(output)