-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_api_docs.py
92 lines (72 loc) · 2.16 KB
/
generate_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
import ast
import pathlib
import textwrap
import yaml
api_docs = pathlib.Path("docs/api")
api_docs.mkdir(exist_ok=True)
src = pathlib.Path("pypythia")
api_files = {
"msa": None,
"raxmlng": None,
"prediction": None,
"predictor": None,
"custom_types": None,
"config": None,
}
for file_name in api_files:
file = src / f"{file_name}.py"
api_file = api_docs / (file_name + ".md")
api_files[file_name] = api_file
node = ast.parse(file.read_text())
classes = []
methods = []
for item in node.body:
if isinstance(item, ast.FunctionDef):
fn_name = item.name
if fn_name.startswith("_"):
continue
methods.append(item.name)
elif isinstance(item, ast.ClassDef):
classes.append(item.name)
with api_file.open("w") as f:
for cls in classes:
f.write(
textwrap.dedent(f"""
::: pypythia.{file.stem}.{cls}\n
options:
show_root_heading: true
merge_init_into_class: false
group_by_category: true
""")
)
for mtd in methods:
f.write(
textwrap.dedent(f"""
::: pypythia.{file.stem}.{mtd}\n
options:
show_root_heading: true
""")
)
if file_name == "config":
with api_file.open("a") as f:
f.write(
textwrap.dedent(f"""
::: pypythia.{file.stem}.DEFAULT_MODEL_FILE\n
options:
show_root_heading: true
::: pypythia.{file.stem}.DEFAULT_RAXMLNG_EXE\n
options:
show_root_heading: true
""")
)
mkdocs_cfg_file = pathlib.Path("mkdocs.yml")
mkdocs_cfg = yaml.safe_load(mkdocs_cfg_file.read_text())
api_nav = {"API Reference": [{name: f"api/{f.name}"} for name, f in api_files.items()]}
nav = []
for el in mkdocs_cfg["nav"]:
if "API Reference" in el:
continue
nav.append(el)
nav.append(api_nav)
mkdocs_cfg["nav"] = nav
yaml.dump(mkdocs_cfg, mkdocs_cfg_file.open("w"))