forked from tflearn/tflearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodoc.py
321 lines (266 loc) · 11.1 KB
/
autodoc.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from __future__ import division, print_function, absolute_import
import inspect
import os
import os.path
from inspect import getmembers, isfunction
import re
import ast
import tflearn
from tflearn import activations
from tflearn import callbacks
from tflearn import collections
import tflearn.config
from tflearn import initializations
from tflearn import metrics
from tflearn import objectives
from tflearn import optimizers
from tflearn import data_utils
from tflearn import losses
from tflearn import summaries
from tflearn import utils
from tflearn import variables
from tflearn import data_flow
from tflearn import data_preprocessing
from tflearn import data_augmentation
from tflearn.layers import conv
from tflearn.layers import core
from tflearn.layers import embedding_ops
from tflearn.layers import estimator
from tflearn.layers import merge_ops
from tflearn.layers import normalization
from tflearn.layers import recurrent
from tflearn.models import dnn, generator
from tflearn.helpers import evaluator
from tflearn.helpers import regularizer
from tflearn.helpers import summarizer
from tflearn.helpers import trainer
ROOT = 'http://tflearn.org/'
MODULES = [(activations, 'tflearn.activations'),
(callbacks, 'tflearn.callbacks'),
(collections, 'tflearn.collections'),
(tflearn.config, 'tflearn.config'),
(initializations, 'tflearn.initializations'),
(metrics, 'tflearn.metrics'),
(objectives, 'tflearn.objectives'),
(optimizers, 'tflearn.optimizers'),
(data_utils, 'tflearn.data_utils'),
(losses, 'tflearn.losses'),
(summaries, 'tflearn.summaries'),
(variables, 'tflearn.variables'),
(utils, 'tflearn.utils'),
(data_flow, 'tflearn.data_flow'),
(data_preprocessing, 'tflearn.data_preprocessing'),
(data_augmentation, 'tflearn.data_augmentation'),
(conv, 'tflearn.layers.conv'),
(core, 'tflearn.layers.core'),
(embedding_ops, 'tflearn.layers.embedding_ops'),
(estimator, 'tflearn.layers.estimator'),
(merge_ops, 'tflearn.layers.merge_ops'),
(normalization, 'tflearn.layers.normalization'),
(recurrent, 'tflearn.layers.recurrent'),
(dnn, 'tflearn.models.dnn'),
(generator, 'tflearn.models.generator'),
(evaluator, 'tflearn.helpers.evaluator'),
(regularizer, 'tflearn.helpers.regularizer'),
(summarizer, 'tflearn.helpers.summarizer'),
(trainer, 'tflearn.helpers.trainer')]
KEYWORDS = ['Input', 'Output', 'Examples', 'Arguments', 'Attributes',
'Returns', 'Raises', 'References', 'Links', 'Yields']
SKIP = ['get_from_module', 'leakyrelu', 'RNNCell', 'resize_image']
def top_level_functions(body):
return (f for f in body if isinstance(f, ast.FunctionDef))
def top_level_classes(body):
return (f for f in body if isinstance(f, ast.ClassDef))
def parse_ast(filename):
with open(filename, "rt") as file:
return ast.parse(file.read(), filename=filename)
def format_func_doc(docstring, header):
rev_docstring = ''
if docstring:
# Erase 2nd lines
docstring = docstring.replace('\n' + ' ' * 3, '')
docstring = docstring.replace(' ' * 2, '')
name = docstring.split('\n')[0]
docstring = docstring[len(name):]
if name[-1] == '.':
name = name[:-1]
docstring = '\n\n' + header_style(header) + docstring
docstring = "# " + name + docstring
# format arguments
for o in ['Arguments', 'Attributes']:
if docstring.find(o + ':') > -1:
args = docstring[docstring.find(o + ':'):].split('\n\n')[0]
args = args.replace(' ', ' - ')
args = re.sub(r' - ([A-Za-z0-9_]+):', r' - **\1**:', args)
if rev_docstring == '':
rev_docstring = docstring[:docstring.find(o + ':')] + args
else:
rev_docstring += '\n\n' + args
for o in ['Returns', 'References', 'Links']:
if docstring.find(o + ':') > -1:
desc = docstring[docstring.find(o + ':'):].split('\n\n')[0]
desc = desc.replace('\n-', '\n\n-')
desc = desc.replace(' ', '')
if rev_docstring == '':
rev_docstring = docstring[:docstring.find(o + ':')] + desc
else:
rev_docstring += '\n\n' + desc
rev_docstring = rev_docstring.replace(' ', '')
rev_docstring = rev_docstring.replace(']\n(http', '](http')
for keyword in KEYWORDS:
rev_docstring = rev_docstring.replace(keyword + ':', '<h3>'
+ keyword + '</h3>\n\n')
else:
rev_docstring = ""
return rev_docstring
def format_method_doc(docstring, header):
rev_docstring = ''
if docstring:
docstring = docstring.replace('\n' + ' ' * 4, '')
docstring = docstring.replace('\n' + ' ' * 3, '')
docstring = docstring.replace(' ' * 2, '')
name = docstring.split('\n')[0]
docstring = docstring[len(name):]
if name[-1] == '.':
name = name[:-1]
docstring = '\n\n' + method_header_style(header) + docstring
#docstring = "\n\n <h3>" + name + "</h3>" + docstring
# format arguments
for o in ['Arguments', 'Attributes']:
if docstring.find(o + ':') > -1:
args = docstring[docstring.find(o + ':'):].split('\n\n')[0]
args = args.replace(' ', ' - ')
args = re.sub(r' - ([A-Za-z0-9_]+):', r' - **\1**:', args)
if rev_docstring == '':
rev_docstring = docstring[:docstring.find(o + ':')] + args
else:
rev_docstring += '\n\n' + args
for o in ['Returns', 'References', 'Links']:
if docstring.find(o + ':') > -1:
desc = docstring[docstring.find(o + ':'):].split('\n\n')[0]
desc = desc.replace('\n-', '\n\n-')
desc = desc.replace(' ', '')
if rev_docstring == '':
rev_docstring = docstring[:docstring.find(o + ':')] + desc
else:
rev_docstring += '\n\n' + desc
rev_docstring = rev_docstring.replace(' ', '')
rev_docstring = rev_docstring.replace(']\n(http', '](http')
for keyword in KEYWORDS:
rev_docstring = rev_docstring.replace(keyword + ':', '<h5>'
+ keyword + '</h5>\n\n')
else:
rev_docstring = ""
return rev_docstring
def classesinmodule(module):
classes = []
tree = parse_ast(os.path.abspath(module.__file__).replace('.pyc', '.py'))
for c in top_level_classes(tree.body):
classes.append(eval(module.__name__ + '.' + c.name))
return classes
def functionsinmodule(module):
fn = []
tree = parse_ast(os.path.abspath(module.__file__).replace('.pyc', '.py'))
for c in top_level_functions(tree.body):
fn.append(eval(module.__name__ + '.' + c.name))
return fn
def enlarge_span(str):
return '<span style="font-size:115%">' + str + '</span>'
def header_style(header):
name = header.split('(')[0]
bold_name = '<span style="color:black;"><b>' + name + '</b></span>'
header = header.replace('self, ', '').replace('(', ' (').replace(' ', ' ')
header = header.replace(name, bold_name)
# return '<span style="display: inline-block;margin: 6px 0;font-size: ' \
# '90%;line-height: 140%;background: #e7f2fa;color: #2980B9;' \
# 'border-top: solid 3px #6ab0de;padding: 6px;position: relative;' \
# 'font-weight:600">' + header + '</span>'
return '<span class="extra_h1">' + header + '</span>'
def method_header_style(header):
name = header.split('(')[0]
bold_name = '<span style="color:black"><b>' + name + '</b></span>'
header = header.replace('self, ', '').replace('(', ' (').replace(' ', ' ')
header = header.replace(name, bold_name)
return '<span class="extra_h2">' + header + '</span>'
print('Starting...')
classes_and_functions = set()
def get_func_doc(name, func):
doc_source = ''
if name in SKIP:
return ''
if name[0] == '_':
return ''
if func in classes_and_functions:
return ''
classes_and_functions.add(func)
header = name + inspect.formatargspec(*inspect.getargspec(func))
docstring = format_func_doc(inspect.getdoc(func), module_name + '.' +
header)
if docstring != '':
doc_source += docstring
doc_source += '\n\n ---------- \n\n'
return doc_source
def get_method_doc(name, func):
doc_source = ''
if name in SKIP:
return ''
if name[0] == '_':
return ''
if func in classes_and_functions:
return ''
classes_and_functions.add(func)
header = name + inspect.formatargspec(*inspect.getargspec(func))
docstring = format_method_doc(inspect.getdoc(func), header)
if docstring != '':
doc_source += '\n\n <span class="hr_large"></span> \n\n'
doc_source += docstring
return doc_source
def get_class_doc(c):
doc_source = ''
if c.__name__ in SKIP:
return ''
if c.__name__[0] == '_':
return ''
if c in classes_and_functions:
return ''
classes_and_functions.add(c)
header = c.__name__ + inspect.formatargspec(*inspect.getargspec(
c.__init__))
docstring = format_func_doc(inspect.getdoc(c), module_name + '.' +
header)
method_doc = ''
if docstring != '':
methods = inspect.getmembers(c, predicate=inspect.ismethod)
if len(methods) > 0:
method_doc += '\n\n<h2>Methods</h2>'
for name, func in methods:
method_doc += get_method_doc(name, func)
if method_doc == '\n\n<h2>Methods</h2>':
method_doc = ''
doc_source += docstring + method_doc
doc_source += '\n\n --------- \n\n'
return doc_source
for module, module_name in MODULES:
# Handle Classes
md_source = ""
for c in classesinmodule(module):
md_source += get_class_doc(c)
# Handle Functions
for func in functionsinmodule(module):
md_source += get_func_doc(func.__name__, func)
# save module page.
# Either insert content into existing page,
# or create page otherwise
path = 'templates/' + module_name.replace('.', '/')[8:] + '.md'
if False: #os.path.exists(path):
template = open(path).read()
assert '{{autogenerated}}' in template, ('Template found for ' + path +
' but missing {{autogenerated}} tag.')
md_source = template.replace('{{autogenerated}}', md_source)
print('...inserting autogenerated content into template:', path)
else:
print('...creating new page with autogenerated content:', path)
subdir = os.path.dirname(path)
if not os.path.exists(subdir):
os.makedirs(subdir)
open(path, 'w').write(md_source)