Coverage for scripts/build_docs.py: 100%
55 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Prepare the docs site (V6.6.5, #346): one API reference page per `aether_quant` facade, the nav that lists them, and the
2tutorial notebooks copied into `docs/tutorials/` (gitignored there; `notebooks/` is the source).
4 python scripts/build_docs.py # write docs/reference/*.md, the mkdocs.yml nav block, docs/tutorials/*.ipynb
5 python scripts/build_docs.py --check # fail if a reference page or the nav is missing or stale (no writes)
6 mkdocs build --strict # then the site, into site/
7"""
9from __future__ import annotations
11import argparse
12import importlib
13import shutil
14import sys
15from pathlib import Path
17ROOT = Path(__file__).resolve().parents[1]
18sys.path.insert(0, str(ROOT))
19DOCS = ROOT / "docs"
20REFERENCE = DOCS / "reference"
21TUTORIALS = DOCS / "tutorials"
22MKDOCS = ROOT / "mkdocs.yml"
23NAV_START, NAV_END = " # AQ:REFERENCE_NAV_START (scripts/build_docs.py)\n", " # AQ:REFERENCE_NAV_END\n"
26def facades() -> list[str]:
27 return sorted(path.stem for path in (ROOT / "aether_quant").glob("*.py") if not path.stem.startswith("_"))
30def reference_page(name: str) -> str:
31 module = importlib.import_module(f"aether_quant.{name}")
32 summary = (module.__doc__ or "").strip().split("\n\n")[0].replace("\n", " ")
33 members = "\n".join(f" - {member}" for member in sorted(module.__all__))
34 return (
35 f"# aether_quant.{name}\n\n{summary}\n\n"
36 f"::: aether_quant.{name}\n options:\n members:\n{members}\n"
37 )
40def nav_block() -> str:
41 return NAV_START + "".join(f" - aether_quant.{name}: reference/{name}.md\n" for name in facades()) + NAV_END
44def expected_files() -> dict[Path, str]:
45 return {REFERENCE / f"{name}.md": reference_page(name) for name in facades()}
48def main(argv: list[str] | None = None) -> int:
49 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
50 parser.add_argument("--check", action="store_true")
51 args = parser.parse_args(argv)
52 problems = []
53 config = MKDOCS.read_text(encoding="utf-8")
54 start, end = config.index(NAV_START), config.index(NAV_END) + len(NAV_END)
55 if config[start:end] != nav_block():
56 problems.append("mkdocs.yml: the API reference nav is stale")
57 if not args.check:
58 MKDOCS.write_text(config[:start] + nav_block() + config[end:], encoding="utf-8", newline="\n")
59 for path, text in expected_files().items():
60 if not path.exists() or path.read_text(encoding="utf-8") != text:
61 problems.append(f"{path.relative_to(ROOT).as_posix()} is missing or stale")
62 if not args.check:
63 path.parent.mkdir(parents=True, exist_ok=True)
64 path.write_text(text, encoding="utf-8", newline="\n")
65 stray = sorted(set(REFERENCE.glob("*.md")) - set(expected_files()) - {REFERENCE / "index.md", REFERENCE / "cli.md"}) # cli.md: aq docs cli
66 problems += [f"{path.relative_to(ROOT).as_posix()} documents no facade" for path in stray]
67 if not args.check:
68 TUTORIALS.mkdir(parents=True, exist_ok=True)
69 for notebook in sorted((ROOT / "notebooks").glob("*.ipynb")):
70 shutil.copyfile(notebook, TUTORIALS / notebook.name)
71 if args.check:
72 for problem in problems:
73 print(problem, file=sys.stderr)
74 return 1 if problems else 0
75 print(f"docs ready: {len(expected_files())} reference pages, {len(list(TUTORIALS.glob('*.ipynb')))} tutorials")
76 return 0
79if __name__ == "__main__":
80 raise SystemExit(main())