This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autogen_apidoc.py
65 lines (48 loc) · 1.51 KB
/
autogen_apidoc.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
import os
import re
import subprocess
re_ext = re.compile('(/?__init__\.py|\.py)$')
re_separator = re.compile('/')
src_path = 'passerine'
index_tmpl = """
API Reference
#############
.. toctree::
:maxdepth: 1
:glob:
*
"""
module_tmpl = """
{module_path}
{underline}
.. note::
This page is automatically generated. If you don't see anything, this means
this sub-module is not meant to be used. If you really want to know what it
is, please check out the source code at :file:`{relative_path}`.
.. automodule:: {module_path}
:members:
"""
def traverse(path):
for name in os.listdir(path):
if '__init__' in name:
continue
relative_path = os.path.join(path, name)
if os.path.isdir(relative_path):
traverse(relative_path)
continue
module_path = re_separator.sub('.', re_ext.sub('', relative_path))
#print(name, relative_path, module_path)
with open('docs/source/api/{}.rst'.format(module_path), 'w') as f:
f.write(module_tmpl.format(
module_path = module_path,
relative_path = relative_path,
underline = "#" * len(module_path)
))
if __name__ == '__main__':
# Remove all files.
subprocess.call('rm docs/source/api/*', shell=True)
# Generate the index page.
with open('docs/source/api/index.rst', 'w') as f:
f.write(index_tmpl)
# Recursively generate API pages for the library.
traverse(src_path)