forked from airtai/faststream
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_api_docs.py
355 lines (271 loc) · 10.7 KB
/
create_api_docs.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Create API documentation for a module."""
import itertools
import shutil
from importlib import import_module
from inspect import getmembers, isclass, isfunction
from pathlib import Path
from pkgutil import walk_packages
from types import FunctionType, ModuleType
from typing import Any, List, Optional, Tuple, Type, Union
API_META = (
"# 0.5 - API\n"
"# 2 - Release\n"
"# 3 - Contributing\n"
"# 5 - Template Page\n"
"# 10 - Default\n"
"search:\n"
" boost: 0.5"
)
MD_API_META = "---\n" + API_META + "\n---\n\n"
PUBLIC_API_FILES = [
"faststream/opentelemetry/__init__.py",
"faststream/asgi/__init__.py",
"faststream/asyncapi/__init__.py",
"faststream/__init__.py",
"faststream/nats/__init__.py",
"faststream/rabbit/__init__.py",
"faststream/confluent/__init__.py",
"faststream/kafka/__init__.py",
"faststream/redis/__init__.py",
]
def _get_submodules(package_name: str) -> List[str]:
"""Get all submodules of a package.
Args:
package_name: The name of the package.
Returns:
A list of submodules.
!!! note
The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen)
"""
try:
# nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
m = import_module(package_name)
except ModuleNotFoundError as e:
raise e
submodules = [
info.name for info in walk_packages(m.__path__, prefix=f"{package_name}.")
]
submodules = [
x for x in submodules if not any(name.startswith("_") for name in x.split("."))
]
return [package_name, *submodules]
def _import_submodules(
module_name: str, include_public_api_only: bool = False
) -> Optional[List[ModuleType]]:
def _import_module(name: str) -> Optional[ModuleType]:
try:
# nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
return import_module(name)
except Exception:
return None
package_names = _get_submodules(module_name)
modules = [_import_module(n) for n in package_names]
if include_public_api_only:
repo_path = Path.cwd().parent
# Extract only faststream/__init__.py or faststream/<something>/__init__.py
public_api_modules = [
m
for m in modules
if m and m.__file__.replace(str(repo_path) + "/", "") in PUBLIC_API_FILES
]
return public_api_modules
return [m for m in modules if m is not None]
def _import_functions_and_classes(
m: ModuleType, include_public_api_only: bool = False
) -> List[Tuple[str, Union[FunctionType, Type[Any]]]]:
funcs_and_classes = []
if not include_public_api_only:
funcs_and_classes = [
(x, y) for x, y in getmembers(m) if isfunction(y) or isclass(y)
]
if hasattr(m, "__all__"):
for t in m.__all__:
obj = getattr(m, t)
if isfunction(obj) or isclass(obj):
funcs_and_classes.append((t, m.__name__ + "." + t))
return funcs_and_classes
def _is_private(name: str) -> bool:
parts = name.split(".")
return any(part.startswith("_") for part in parts)
def _import_all_members(
module_name: str, include_public_api_only: bool = False
) -> List[str]:
submodules = _import_submodules(
module_name, include_public_api_only=include_public_api_only
)
members: List[Tuple[str, Union[FunctionType, Type[Any]]]] = list(
itertools.chain(
*[
_import_functions_and_classes(
m, include_public_api_only=include_public_api_only
)
for m in submodules
]
)
)
names = [
y if isinstance(y, str) else f"{y.__module__}.{y.__name__}" for x, y in members
]
names = [
name for name in names if not _is_private(name) and name.startswith(module_name)
]
return names
def _merge_lists(members: List[str], submodules: List[str]) -> List[str]:
members_copy = members[:]
for sm in submodules:
for i, el in enumerate(members_copy):
if el.startswith(sm):
members_copy.insert(i, sm)
break
return members_copy
def _add_all_submodules(members: List[str]) -> List[str]:
def _f(x: str) -> List[str]:
xs = x.split(".")
return [".".join(xs[:i]) + "." for i in range(1, len(xs))]
def _get_sorting_key(item):
y = item.split(".")
z = [f"~{a}" for a in y[:-1]] + [y[-1]]
return ".".join(z)
submodules = list(set(itertools.chain(*[_f(x) for x in members])))
members = _merge_lists(members, submodules)
members = list(dict.fromkeys(members))
return sorted(members, key=_get_sorting_key)
def _get_api_summary_item(x: str) -> str:
xs = x.split(".")
if x.endswith("."):
indent = " " * (4 * (len(xs) - 1 + 1))
return f"{indent}- {xs[-2]}"
else:
indent = " " * (4 * (len(xs) + 1))
return f"{indent}- [{xs[-1]}](api/{'/'.join(xs)}.md)"
def _get_api_summary(members: List[str]) -> str:
return "\n".join([_get_api_summary_item(x) for x in members])
def _generate_api_doc(name: str, docs_path: Path) -> Path:
xs = name.split(".")
module_name = ".".join(xs[:-1])
member_name = xs[-1]
path = docs_path / f"{('/').join(xs)}.md"
content = f"::: {module_name}.{member_name}\n"
path.parent.mkdir(exist_ok=True, parents=True)
path.write_text(MD_API_META + content)
return path
def _generate_api_docs(members: List[str], docs_path: Path) -> List[Path]:
return [_generate_api_doc(x, docs_path) for x in members if not x.endswith(".")]
def _get_submodule_members(module_name: str) -> List[str]:
"""Get a list of all submodules contained within the module.
Args:
module_name: The name of the module to retrieve submodules from
Returns:
A list of submodule names within the module
"""
members = _import_all_members(module_name)
members_with_submodules = _add_all_submodules(members)
members_with_submodules_str: List[str] = [
x[:-1] if x.endswith(".") else x for x in members_with_submodules
]
return members_with_submodules_str
def _load_submodules(
module_name: str,
members_with_submodules: List[str],
) -> List[Union[FunctionType, Type[Any]]]:
"""Load the given submodules from the module.
Args:
module_name: The name of the module whose submodules to load
members_with_submodules: A list of submodule names to load
Returns:
A list of imported submodule objects.
"""
submodules = _import_submodules(module_name)
members = itertools.chain(*map(_import_functions_and_classes, submodules))
names = [
y
for _, y in members
if (isinstance(y, str) and y in members_with_submodules)
or (f"{y.__module__}.{y.__name__}" in members_with_submodules)
]
return names
def _update_single_api_doc(
symbol: Union[FunctionType, Type[Any]], docs_path: Path, module_name: str
) -> None:
en_docs_path = docs_path / "docs" / "en"
if isinstance(symbol, str):
class_name = symbol.split(".")[-1]
module_name = ".".join(symbol.split(".")[:-1])
# nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
obj = getattr(import_module(module_name), class_name)
if obj.__module__.startswith(module_name):
obj = symbol
filename = symbol
else:
obj = symbol
filename = f"{symbol.__module__}.{symbol.__name__}"
content = "::: %s\n" % (
obj if isinstance(obj, str) else f"{obj.__module__}.{obj.__qualname__}"
)
target_file_path = "/".join(filename.split(".")) + ".md"
(en_docs_path / "api" / target_file_path).write_text(MD_API_META + content)
def _update_api_docs(
symbols: List[Union[FunctionType, Type[Any]]], docs_path: Path, module_name: str
) -> None:
for symbol in symbols:
_update_single_api_doc(
symbol=symbol, docs_path=docs_path, module_name=module_name
)
def _generate_api_docs_for_module(root_path: Path, module_name: str) -> Tuple[str, str]:
"""Generate API documentation for a module.
Args:
root_path: The root path of the project.
module_name: The name of the module.
Returns:
A string containing the API documentation for the module.
"""
public_api_summary = _get_api_summary(
_add_all_submodules(
_import_all_members(module_name, include_public_api_only=True)
)
)
# Using public_api/ symlink pointing to api/ because of the issue
# https://github.com/mkdocs/mkdocs/issues/1974
public_api_summary = public_api_summary.replace("(api/", "(public_api/")
members = _import_all_members(module_name)
members_with_submodules = _add_all_submodules(members)
api_summary = _get_api_summary(members_with_submodules)
api_root = root_path / "docs" / "en" / "api"
shutil.rmtree(api_root / module_name, ignore_errors=True)
api_root.mkdir(parents=True, exist_ok=True)
(api_root / ".meta.yml").write_text(API_META)
_generate_api_docs(members_with_submodules, api_root)
members_with_submodules = _get_submodule_members(module_name)
symbols = _load_submodules(module_name, members_with_submodules)
_update_api_docs(symbols, root_path, module_name)
# todo: fix the problem and remove this
src = """ - [ContactDict](api/faststream/asyncapi/schema/info/ContactDict.md)
"""
dst = """ - [ContactDict](api/faststream/asyncapi/schema/info/ContactDict.md)
- [EmailStr](api/faststream/asyncapi/schema/info/EmailStr.md)
"""
api_summary = api_summary.replace(src, dst)
return api_summary, public_api_summary
def create_api_docs(
root_path: Path,
module: str,
) -> None:
"""Generate API documentation for a module.
Args:
root_path: The root path of the project.
module: The name of the module.
"""
api, public_api = _generate_api_docs_for_module(root_path, module)
docs_dir = root_path / "docs"
# read summary template from file
navigation_template = (docs_dir / "navigation_template.txt").read_text()
summary = navigation_template.format(api=api, public_api=public_api)
summary = "\n".join(filter(bool, (x.rstrip() for x in summary.split("\n"))))
(docs_dir / "SUMMARY.md").write_text(summary)
def on_page_markdown(markdown, *, page, config, files):
"""Mkdocs hook to update the edit URL for the public API pages."""
if page.edit_url and "public_api" in page.edit_url:
page.edit_url = page.edit_url.replace("public_api", "api")
if __name__ == "__main__":
root = Path(__file__).resolve().parent
create_api_docs(root, "faststream")