-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate
executable file
·204 lines (178 loc) · 7.95 KB
/
generate
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
#!/usr/bin/env python
import importlib
import pkgutil
import sys
import os
from collections import OrderedDict
import coalib.bearlib.aspects
def text_wrap(*args, delimiter=' ', end='\n', limit=80):
"""
Breaks it wordwise when going further than ``limit`` chars.
:param args:
The stuff you want to have printed.
:param delimiter:
Will be placed between all the args.
:param limit:
Char limit.
:param end:
An ending, will be put as last character and doesn't count into the
char limit.
:return:
A list of strings, each element to be written on its own line.
"""
output = delimiter.join(args)
lines = output.splitlines(keepends=True)
results = []
for line in lines:
curr_print = line
while len(curr_print.rstrip('\n')) > limit:
splitpos = curr_print[:limit].rfind(' ')
if splitpos < 0:
# Word too long, search for a space left from limit at least
splitpos = curr_print.find(' ')
if splitpos < 0:
break # Break out and add the long thing in the next line
results.append(curr_print[:splitpos])
curr_print = curr_print[splitpos+1:]
results.append(curr_print)
return results
def import_all_subaspects(package_name):
package = sys.modules[package_name]
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
importlib.import_module(package_name + '.' + name)
def recurse(aspect, level=0):
readme = ""
for subaspect_name, subaspect in sorted(aspect.subaspects.items()):
subaspect_dir = subaspect.__qualname__.replace(".", "/")
os.makedirs(subaspect_dir, exist_ok=True)
output = ""
cols = [
"``" + subaspect.__qualname__ + "``",
"`Parent <../README.rst>`_ ",
"`Index <//github.com/coala/aspect-docs/blob/master/README.rst>`_",
]
if "." not in subaspect.__qualname__:
cols[1] = "`Parent <//github.com/coala/aspect-docs/blob/master/README.rst>`_"
output += "+"
for col in cols:
output += "-" * (len(col) + 2)
output += "+"
output += "\n"
output += "|"
for col in cols:
output += " " + col + " |"
output += "\n"
output += "+"
for col in cols:
output += "-" * (len(col) + 2)
output += "+"
output += "\n\n"
if len(subaspect.parent.subaspects) > 0:
cols = ["**Sibling aspects**"]
cols += ["`" + subsub_name + " <../" + subsub_name + "/README.rst>`_"
for subsub_name in sorted(subaspect.parent.subaspects)
if subsub_name != subaspect.__name__]
parent = "Root"
if subaspect.parent.__qualname__:
parent = subaspect.parent.__qualname__
output += "+"
for col in cols:
output += "-" * (len(col) + 2)
output += "+"
output += "\n"
output += "|"
for col in cols:
output += " " + col + " |"
output += "\n"
output += "+"
for col in cols:
output += "-" * (len(col) + 2)
output += "+"
output += "\n\n"
output += subaspect_name + "\n"
output += "=" * len(subaspect_name) + "\n"
output += subaspect.docs.definition + "\n\n"
if subaspect.tastes:
output += "Tastes\n"
output += "========\n\n"
par = OrderedDict()
lmax = -1
cmax = -1
rmax = -1
par = {}
var = {}
for taste_name, taste in subaspect.tastes.items():
key = "``" + taste_name + "``"
par[key] = text_wrap(taste.description, limit=60)
par[key] = [g.strip() for g in par[key]]
var[key] = "**" + str(taste.default) + "**"
if len(taste.suggested_values) > 1:
var[key] += ", "
var[key] += ", ".join([str(x) for x in taste.suggested_values if x != taste.default])
lmax = max(lmax, len(key))
cmax = max(cmax, max(len(g) for g in par[key]))
rmax = max(rmax, len(var[key]))
if len(par):
lmax = max(lmax, len(" Taste"))
cmax = max(cmax, len("Meaning"))
rmax = max(cmax, len("Values"))
header = False
for key in sorted(par):
if not header:
output += "+" + "-" * (lmax) + "-+-" + "-" * (cmax) + "-+-" + "-" * (1 + rmax) + "+\n"
output += "| Taste" + " " * (lmax - len(" Taste")) + " | Meaning" + " " * (cmax - 1 - len("Meaning")) + " | Values" + " " * (cmax - len("Values")) + "|\n"
output += "+" + "=" * lmax + "=+=" + "=" * (cmax) + "=+=" + "=" * (1 + rmax) + "+\n"
header = True
output += "| " + " " * lmax + "| " + " " * (1 + cmax) + "| " + " " * (1 + rmax) + "|\n"
output += "|" + key + " " + " " * (lmax - len(key)) + "| " + par[key][0] + " " * (1 + cmax - len(par[key][0])) + "| " + var[key] + " " * (1 + rmax - len(var[key]))
if len(par[key]) > 1:
output += "|\n"
else:
output += "+\n"
for text in par[key][1:]:
output += "|" + " " * (lmax + 1) + "| " + text + " " * (1 + cmax - len(text)) + "| " + " " * (1 + rmax) + "|\n"
output += "| " + " " * lmax + "| " + " " * (1 + cmax) + "| " + " " * (1 + rmax) + "|\n"
output += "+" + "-" * lmax + "-+-" + "-" * (cmax) + "-+-" + "-" * (1 + rmax) + "+\n"
output += "\n\n\* bold denotes default value\n\n"
output += "Subaspects\n"
output += "==========\n\n"
if subaspect.subaspects.items():
for subsub_name, subsub in sorted(subaspect.subaspects.items()):
output += "* `" + subsub_name + " <" + subsub_name + "/README.rst>`_\n"
else:
output += "This aspect does not have any sub aspects.\n\n"
if subaspect.docs.example:
output += "Example\n"
output += "=======\n\n"
output += ".. code-block:: " + subaspect.docs.example_language + "\n\n"
for line in subaspect.docs.example.splitlines():
output += " " + line + "\n"
output += "\n\n"
if subaspect.docs.importance_reason:
output += "Importance\n"
output += "==========\n\n"
output += subaspect.docs.importance_reason + "\n\n"
if subaspect.docs.fix_suggestions:
output += "How to fix this\n"
output += "==========\n\n"
output += subaspect.docs.fix_suggestions + "\n\n"
with open(os.path.join(subaspect_dir, "README.rst"), "w") as subfile:
subfile.write(output)
readme += (" " * (2*level))
readme += "- " + "`" + subaspect_name + " <" + subaspect.__qualname__.replace(".", "/") + "/README.rst>`_ \n"
readme += (" " * (2*level + 2))
readme += "\n\n"
readme += recurse(subaspect, level=level+1)
return readme
if __name__ == "__main__":
import_all_subaspects("coalib.bearlib.aspects")
readme = recurse(coalib.bearlib.aspects.Root, level=0)
with open("README.rst", "w") as f:
f.write("Documentation for aspects\n")
f.write("-------------------------\n\n")
f.write("This repository is automatically generated. To do so, run "
"``./generate``. This will automatically push the changes too.\n\n")
f.write("The aspect tree is given below:\n\n")
f.write(readme)
if len(sys.argv) == 1:
os.system("git add -A && git commit -m 'Docs Update' && git push")