This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathdocs_api.py
151 lines (137 loc) · 3.75 KB
/
docs_api.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
# The following code ensures everything has a page under docs/api/
import inspect
import pathlib
from typing import Optional, Callable
def modules(
root: pathlib.Path,
package_name: str,
*,
skip: Optional[Callable[[str, pathlib.Path], bool]] = None,
):
for path in root.rglob("*.py"):
# Figure out name
import_name = pathlib.Path(str(path)[len(str(root)) :]).parts[1:]
import_name = (
package_name
+ "."
+ ".".join(
list(import_name[:-1]) + [import_name[-1].replace(".py", "")]
)
)
# Check if we should skip importing this file
if skip and skip(import_name, path):
continue
# Import module
yield import_name, path
package_name = "dffml"
root = pathlib.Path(__file__).parent.parent / package_name
skel = root / "skel"
# Skip any files in skel and __main__.py and __init__.py
skip = lambda _import_name, path: skel in path.parents or path.name.startswith(
"__"
)
def gen_toctree(
root: pathlib.Path,
*,
skip: Optional[Callable[[str, pathlib.Path], bool]] = None,
):
names = []
for path in root.glob("*.py"):
# Figure out name
import_name = pathlib.Path(str(path)[len(str(root)) :]).parts[1:]
import_name = (
package_name
+ "."
+ ".".join(
list(import_name[:-1]) + [import_name[-1].replace(".py", "")]
)
)
# Check if we should skip importing this file
if skip and skip(import_name, path):
continue
names.append(path.stem)
for path in root.rglob("__init__.py"):
if path.parent.parent == root:
names.append(path.parent.name + "/index")
return (
inspect.cleandoc(
f"""
.. toctree::
:glob:
:maxdepth: 2
:caption: Contents:
"""
)
+ "\n\n "
+ "\n ".join(names)
)
for import_name, import_path in modules(root, package_name, skip=skip):
import_name_no_package = import_name[len(package_name) + 1 :]
path = (
root.parent
/ "docs"
/ "api"
/ pathlib.Path(*import_name_no_package.split("."))
).with_suffix(".rst")
index = path.parent / "index.rst"
# Use parent.parts[1:] to remove docs/ from the path
index_template = (
root.parent
/ "scripts"
/ "docs"
/ "templates"
/ pathlib.Path(*path.parent.parts[1:])
/ "index.rst"
)
page_template = (
root.parent
/ "scripts"
/ "docs"
/ "templates"
/ pathlib.Path(*path.parts[1:])
)
title = " ".join(import_name_no_package.split(".")).title()
page = (
inspect.cleandoc(
f"""
{title}
{"=" * len(title)}
.. automodule:: {import_name}
:members:
"""
).lstrip()
+ "\n"
)
index_title = " ".join(import_name_no_package.split(".")[:-1]).title()
index_content = (
inspect.cleandoc(
f"""
{index_title}
{"=" * len(index_title)}
"""
).lstrip()
+ "\n"
)
if index_template.is_file():
index_content = index_template.read_text()
index_content += gen_toctree(import_path.parent, skip=skip) + "\n"
"""
print()
print()
print()
print(index)
print()
print(index_content)
print()
print(path)
print()
print(page)
print()
print("-" * 40)
"""
if page_template.is_file():
page = page_template.read_text()
if not index.parent.is_dir():
index.parent.mkdir(parents=True)
index.write_text(index_content)
path.write_text(page)