Repository navigation
Expand file tree
/
Copy pathgenerate-api-docs.py
More file actions
123 lines (94 loc) · 2.97 KB
/
Copy pathgenerate-api-docs.py
File metadata and controls
123 lines (94 loc) · 2.97 KB
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
import web
import os
import re
import shutil
from collections import defaultdict
template = """\
$def with (mod, submodules)
$ name = mod.split(".")[-1]
$name
$("=" * len(name))
$if submodules:
Submodules
----------
.. toctree::
:maxdepth: 1
$for m in submodules: $m
Documentation
-------------
.. automodule:: $mod
$else:
.. automodule:: $mod
"""
t = web.template.Template(template)
def docpath(path):
return "docs/api/" + path.replace(".py", ".rst").replace("__init__", "index")
def modname(path):
return path.replace(".py", "").replace("/__init__", "").replace("/", ".")
def write(path, text):
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
print "writing", path
f = open(path, "w")
f.write(text)
f.close()
def find_python_sources(dir):
ignores = [
"openlibrary/catalog.*",
"openlibrary/solr.*",
"openlibrary/plugins/recaptcha.*",
"openlibrary/plugins/akismet.*",
"openlibrary/plugins/bookrev.*",
"openlibrary/plugins/copyright.*",
"openlibrary/plugins/heartbeat.*",
".*tests",
"infogami/plugins.*",
"infogami.utils.markdown",
]
re_ignore = re.compile("|".join(ignores))
for dirpath, dirnames, filenames in os.walk(dir):
if re_ignore.match(dirpath):
print "ignoring", dirpath
continue
for f in filenames:
if f.endswith(".py"):
yield os.path.join(dirpath, f)
def generate_docs(dir):
shutil.rmtree(docpath(dir), ignore_errors=True)
paths = list(find_python_sources(dir))
submodule_dict = defaultdict(list)
for path in paths:
dir = os.path.dirname(path)
if path.endswith("__init__.py"):
dir = os.path.dirname(dir)
submodule_dict[dir].append(path)
for path in paths:
dirname = os.path.dirname(path)
if path.endswith("__init__.py"):
submodules = [web.lstrips(docpath(s), docpath(dirname) + "/") for s in submodule_dict[dirname]]
else:
submodules = []
submodules.sort()
mod = modname(path)
text = str(t(mod, submodules))
write(docpath(path), text)
# set the modification time same as the source file
mtime = os.stat(path).st_mtime
os.utime(docpath(path), (mtime, mtime))
def generate_index():
filenames = sorted(os.listdir("docs/api"))
f = open("docs/api/index.rst", "w")
f.write("API Documentation\n")
f.write("=================\n")
f.write("\n")
f.write(".. toctree::\n")
f.write(" :maxdepth: 1\n")
f.write("\n")
f.write("\n".join(" " + filename for filename in filenames))
def main():
generate_docs("openlibrary")
generate_docs("infogami")
#generate_index()
if __name__ == "__main__":
main()