-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-docs.py
More file actions
executable file
·83 lines (68 loc) · 2.77 KB
/
build-docs.py
File metadata and controls
executable file
·83 lines (68 loc) · 2.77 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
#!/usr/bin/env python3
import os
import shutil
from typing import List, Tuple
from io import TextIOWrapper
src_dir = os.path.join(os.path.dirname(__file__), "src", "basic_bot")
docs_dir = os.path.join(os.path.dirname(__file__), "docs")
api_docs_dir = os.path.join(docs_dir, "Api Docs")
shutil.rmtree(api_docs_dir, ignore_errors=True)
os.makedirs(api_docs_dir, exist_ok=True)
FILE_BANNER = """
#
# WARNING: this file is added by build-docs.py in the project root.
#
# YOUR CHANGES HERE WILL BE OVERWRITTEN
"""
with open("./README.md", "r") as f_readme:
with open(os.path.join(docs_dir, "index.md"), "w") as f_docs:
f_docs.write(f"---\ntitle: Getting Started\n\n{FILE_BANNER}\n---\n")
f_docs.write(f_readme.read())
def write_banner(f: TextIOWrapper, title: str):
# without the title, the mkdocs TOC entry will be stripped of
# underscores and capitalized
f.write(f"---\ntitle: {title}")
f.write(FILE_BANNER)
f.write("\n---\n")
# (src_dir, docs_dir)
doc_dirs: List[Tuple[str, str]] = [
("", "scripts"),
("services", "services"),
("commons", "commons"),
("debug", "debug"),
("test_helpers", "test_helpers"),
]
for src, dest in doc_dirs:
this_src_dir = os.path.join(src_dir, src)
dest_dir = os.path.join(api_docs_dir, dest)
os.makedirs(dest_dir, exist_ok=True)
readme_file = os.path.join(this_src_dir, "README.md")
if os.path.isfile(readme_file):
os.link(readme_file, os.path.join(dest_dir, "README.md"))
for file in os.listdir(this_src_dir):
if file.endswith(".py") and not file.startswith("__"):
file_name = file[:-3]
doc_file = f"{dest_dir}/{file_name}.md"
with open(doc_file, "w") as f:
write_banner(f, file_name)
dot_parts = "" if src == "" else f".{src}"
cmd = f'pydoc-markdown -m basic_bot{dot_parts}.{file_name} >> "{doc_file}"'
print(f"running: {cmd}")
os.system(cmd)
config_src_file = os.path.join(src_dir, "commons", "config_file_schema.py")
config_doc_file = os.path.join(docs_dir, "Configuration", "Config File Schema.md")
env_src_file = os.path.join(src_dir, "commons", "constants.py")
env_doc_file = os.path.join(docs_dir, "Configuration", "Environment Variables.md")
for src_file, doc_file in (
(config_src_file, config_doc_file),
(env_src_file, env_doc_file),
):
with open(src_file, "r") as f_in, open(doc_file, "w") as f_out:
contents = f_in.read()
# skip over the module docstring
start = contents.find('"""', contents.find('"""') + 3) + 3
write_banner(f_out, os.path.splitext(os.path.basename(doc_file))[0])
f_out.write("```python\n")
f_out.write(contents[start:])
f_out.write("\n```")
print("Docs built successfully")