|
5 | 5 |
|
6 | 6 | HERE = Path(__file__).parent
|
7 | 7 | PACKAGE_SRC = HERE.parent.parent.parent / "src"
|
8 |
| -PUBLIC_API_REFERENCE_FILE = HERE.parent / "api-reference.rst" |
9 |
| -PRIVATE_API_REFERENCE_FILE = HERE.parent / "developer-apis.rst" |
10 | 8 |
|
| 9 | +AUTO_DIR = HERE.parent / "auto" |
| 10 | +AUTO_DIR.mkdir(exist_ok=True) |
11 | 11 |
|
12 |
| -PUBLIC_TITLE = """API Reference |
13 |
| -============= |
14 |
| -""" |
| 12 | +PUBLIC_API_REFERENCE_FILE = AUTO_DIR / "api-reference.rst" |
| 13 | +PRIVATE_API_REFERENCE_FILE = AUTO_DIR / "developer-apis.rst" |
15 | 14 |
|
16 |
| -PRIVATE_TITLE = """Developer APIs |
17 |
| -============== |
18 |
| -""" |
19 | 15 |
|
20 |
| -AUTODOC_TEMPLATE = """ |
21 |
| -.. automodule:: {module} |
22 |
| - :members: |
23 |
| -""" |
| 16 | +PUBLIC_TITLE = "API Reference\n=============\n" |
| 17 | +PUBLIC_MISC_TITLE = "Misc Modules\n------------\n" |
| 18 | +PRIVATE_TITLE = "Developer APIs\n==============\n" |
| 19 | +PRIVATE_MISC_TITLE = "Misc Dev Modules\n----------------\n" |
24 | 20 |
|
25 |
| - |
26 |
| -def make_autodoc_section(module_name: str) -> str: |
27 |
| - return AUTODOC_TEMPLATE.format(module=module_name, underline="-" * len(module_name)) |
| 21 | +AUTODOC_TEMPLATE = ".. automodule:: {module}\n :members:\n" |
28 | 22 |
|
29 | 23 |
|
30 | 24 | def generate_api_docs():
|
31 |
| - public_docs = [PUBLIC_TITLE] |
32 |
| - private_docs = [PRIVATE_TITLE] |
| 25 | + docs = { |
| 26 | + "public.main": [PUBLIC_TITLE], |
| 27 | + "public.misc": [PUBLIC_MISC_TITLE], |
| 28 | + "private.main": [PRIVATE_TITLE], |
| 29 | + "private.misc": [PRIVATE_MISC_TITLE], |
| 30 | + } |
33 | 31 |
|
34 | 32 | for file in sorted(PACKAGE_SRC.glob("**/*.py")):
|
35 | 33 | if file.stem.startswith("__"):
|
36 | 34 | # skip __init__ and __main__
|
37 | 35 | continue
|
38 |
| - rel_path = file.relative_to(PACKAGE_SRC) |
39 |
| - module_name = ".".join(rel_path.with_suffix("").parts) |
40 |
| - api_docs = make_autodoc_section(module_name) |
41 |
| - (private_docs if "._" in module_name else public_docs).append(api_docs) |
| 36 | + public_vs_private = "private" if is_private_module(file) else "public" |
| 37 | + main_vs_misc = "main" if file_starts_with_docstring(file) else "misc" |
| 38 | + key = f"{public_vs_private}.{main_vs_misc}" |
| 39 | + docs[key].append(make_autodoc_section(file, public_vs_private == "private")) |
| 40 | + |
| 41 | + public_content = docs["public.main"] |
| 42 | + if len(docs["public.misc"]) > 1: |
| 43 | + public_content += docs["public.misc"] |
| 44 | + |
| 45 | + private_content = docs["private.main"] |
| 46 | + if len(docs["private.misc"]) > 1: |
| 47 | + private_content += docs["private.misc"] |
| 48 | + |
| 49 | + PUBLIC_API_REFERENCE_FILE.write_text("\n".join(public_content)) |
| 50 | + PRIVATE_API_REFERENCE_FILE.write_text("\n".join(private_content)) |
| 51 | + |
42 | 52 |
|
43 |
| - PUBLIC_API_REFERENCE_FILE.write_text("\n".join(public_docs)) |
44 |
| - PRIVATE_API_REFERENCE_FILE.write_text("\n".join(private_docs)) |
| 53 | +def is_private_module(path: Path) -> bool: |
| 54 | + return any(p.startswith("_") for p in path.parts) |
| 55 | + |
| 56 | + |
| 57 | +def make_autodoc_section(path: Path, is_public) -> str: |
| 58 | + rel_path = path.relative_to(PACKAGE_SRC) |
| 59 | + module_name = ".".join(rel_path.with_suffix("").parts) |
| 60 | + return AUTODOC_TEMPLATE.format(module=module_name, underline="-" * len(module_name)) |
| 61 | + |
| 62 | + |
| 63 | +def file_starts_with_docstring(path: Path) -> bool: |
| 64 | + for line in path.read_text().split("\n"): |
| 65 | + if line.startswith("#"): |
| 66 | + continue |
| 67 | + if line.startswith('"""') or line.startswith("'''"): |
| 68 | + return True |
| 69 | + else: |
| 70 | + break |
| 71 | + return False |
45 | 72 |
|
46 | 73 |
|
47 | 74 | def setup(app: Sphinx) -> None:
|
|
0 commit comments