|
| 1 | +"""Generate docs.""" |
| 2 | + |
| 3 | +# standard |
| 4 | +from shutil import copy, move, rmtree |
| 5 | +from yaml import safe_load, safe_dump |
| 6 | +from ast import parse, ImportFrom |
| 7 | +from typing import Dict, List |
| 8 | +from os.path import getsize |
| 9 | +from subprocess import run |
| 10 | +from pathlib import Path |
| 11 | +from sys import argv |
| 12 | + |
| 13 | + |
| 14 | +def _write_ref_content(source: Path, module_name: str, func_name: str): |
| 15 | + """Write content.""" |
| 16 | + with open(source, "at") as ref: |
| 17 | + ref.write( |
| 18 | + (f"# {module_name}\n\n" if getsize(source) == 0 else "") |
| 19 | + + f"::: validators.{module_name}.{func_name}\n" |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def generate_reference(source: Path, destination: Path): |
| 24 | + """Generate reference.""" |
| 25 | + nav_items: Dict[str, List[str]] = {"Code Reference": []} |
| 26 | + # clean destination |
| 27 | + if destination.exists() and destination.is_dir(): |
| 28 | + rmtree(destination) |
| 29 | + destination.mkdir(exist_ok=True) |
| 30 | + # parse source |
| 31 | + v_ast = parse(source.read_text(), source) |
| 32 | + # generate reference content |
| 33 | + for namespace in (node for node in v_ast.body if isinstance(node, ImportFrom)): |
| 34 | + if not namespace.module: |
| 35 | + continue |
| 36 | + for alias in namespace.names: |
| 37 | + ref_module = destination / f"{namespace.module}.md" |
| 38 | + _write_ref_content(ref_module, namespace.module, alias.name) |
| 39 | + nav_items["Code Reference"].append(f"reference/{namespace.module}.md") |
| 40 | + return nav_items |
| 41 | + |
| 42 | + |
| 43 | +def update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, List[str]]): |
| 44 | + """Temporary update to mkdocs config.""" |
| 45 | + copy(source, destination) |
| 46 | + with open(source, "rt") as mkf: |
| 47 | + mkdocs_conf = safe_load(mkf) |
| 48 | + mkdocs_conf["nav"] += [nav_items] |
| 49 | + with open(source, "wt") as mkf: |
| 50 | + safe_dump(mkdocs_conf, mkf, sort_keys=False) |
| 51 | + |
| 52 | + |
| 53 | +def generate_documentation(source: Path): |
| 54 | + """Generate documentation.""" |
| 55 | + # copy readme as docs index file |
| 56 | + copy(source / "README.md", source / "docs/index.md") |
| 57 | + # generate reference documentation |
| 58 | + nav_items = generate_reference(source / "validators/__init__.py", source / "docs/reference") |
| 59 | + # backup mkdocs config |
| 60 | + update_mkdocs_config(source / "mkdocs.yml", source / "mkdocs.bak.yml", nav_items) |
| 61 | + # build docs as subprocess |
| 62 | + print(run(("mkdocs", "build"), capture_output=True).stderr.decode()) |
| 63 | + # restore mkdocs config |
| 64 | + move(source / "mkdocs.bak.yml", source / "mkdocs.yml") |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + project_dir = Path(__file__).parent.parent |
| 69 | + generate_documentation(project_dir) |
| 70 | + # use this option before building package |
| 71 | + # with `poetry build` to include refs |
| 72 | + if len(argv) > 1 and argv[1] == "--keep": |
| 73 | + quit() |
| 74 | + rmtree(project_dir / "docs/reference") |
0 commit comments