-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
makedoc.py
209 lines (172 loc) · 4.55 KB
/
makedoc.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
"""
Outputs web.py docs as html
version 2.0: documents all code, and indents nicely.
By Colin Rothwell (TheBoff)
"""
import inspect
import sys
import markdown
from web.net import websafe
sys.path.insert(0, "..")
ALL_MODULES = [
"web.application",
"web.contrib.template",
"web.db",
"web.debugerror",
"web.form",
"web.http",
"web.httpserver",
"web.net",
"web.session",
"web.template",
"web.utils",
"web.webapi",
"web.wsgi",
]
item_start = '<code class="%s">'
item_end = "</code>"
indent_amount = 30
doc_these = ( # These are the types of object that should be docced
"module",
"classobj",
"instancemethod",
"function",
"type",
"property",
)
not_these_names = ( # Any particular object names that shouldn't be doced
"fget",
"fset",
"fdel",
"storage", # These stop the lower case versions getting docced
"memoize",
"iterbetter",
"capturesstdout",
"profile",
"threadeddict",
"d", # Don't know what this is, but only only conclude it shouldn't be doc'd
)
css = """
<style type="text/css">
.module {
font-size: 130%;
font-weight: bold;
}
.function, .class, .type {
font-size: 120%;
font-weight: bold;
}
.method, .property {
font-size: 115%;
font-weight: bold;
}
.ts {
font-size: small;
font-weight: lighter;
color: grey;
}
#contents_link {
position: fixed;
top: 0;
right: 0;
padding: 5px;
background: rgba(255, 255, 255, 0.5);
}
#contents_link a:hover {
font-weight: bold;
}
</style>
"""
indent_start = '<div style="margin-left:%dpx">'
indent_end = "</div>"
header = """
<div id="contents_link">
<a href="#top">Back to contents</a>
</div>
"""
def type_string(ob):
return str(type(ob)).split("'")[1]
def ts_css(text):
"""applies nice css to the type string"""
return '<span class="ts">%s</span>' % text
def arg_string(func):
"""Returns a nice argstring for a function or method"""
return inspect.formatargspec(*inspect.getargspec(func))
def recurse_over(ob, name, indent_level=0):
ts = type_string(ob)
if ts not in doc_these:
return # stos what shouldn't be docced getting docced
if indent_level > 0 and ts == "module":
return # Stops it getting into the stdlib
if name in not_these_names:
return # Stops things we don't want getting docced
indent = indent_level * indent_amount # Indents nicely
ds_indent = indent + (indent_amount / 2)
if indent_level > 0:
print(indent_start % indent)
argstr = ""
if ts.endswith(("function", "method")):
argstr = arg_string(ob)
elif ts in {"classobj", "type"}:
if ts == "classobj":
ts = "class"
if hasattr(ob, "__init__"):
if type_string(ob.__init__) == "instancemethod":
argstr = arg_string(ob.__init__)
else:
argstr = "(self)"
if ts == "instancemethod":
ts = "method" # looks much nicer
ds = inspect.getdoc(ob)
if ds is None:
ds = ""
ds = markdown.Markdown(ds)
mlink = '<a name="%s">' % name if ts == "module" else ""
mend = "</a>" if ts == "module" else ""
print(
"".join(
(
"<p>",
ts_css(ts),
item_start % ts,
" ",
mlink,
name,
websafe(argstr),
mend,
item_end,
"<br />",
)
)
)
print("".join((indent_start % ds_indent, ds, indent_end, "</p>")))
# Although ''.join looks weird, it's a lot faster is string addition
members = ""
if hasattr(ob, "__all__"):
members = ob.__all__
else:
members = [item for item in dir(ob) if not item.startswith("_")]
if "im_class" not in members:
for name in members:
recurse_over(getattr(ob, name), name, indent_level + 1)
if indent_level > 0:
print(indent_end)
def main(modules=None):
modules = modules or ALL_MODULES
print("<div>") # Stops markdown vandalising my html.
print(css)
print(header)
print("<ul>")
for name in modules:
print('<li><a href="#{name}">{name}</a></li>'.format(**dict(name=name)))
print("</ul>")
for name in modules:
try:
mod = __import__(name, {}, {}, "x")
recurse_over(mod, name)
except ImportError as e:
print(f"Unable to import module {name} (Error: {e})", file=sys.stderr)
pass
print("</div>")
if __name__ == "__main__":
main(sys.argv[1:])