88from os .path import getsize
99from subprocess import run
1010from pathlib import Path
11- from sys import argv
1211
1312# external
1413from yaml import safe_load , safe_dump
1514
15+ __all__ = ("generate_documentation" ,)
16+
1617
1718def _write_ref_content (source : Path , module_name : str , func_name : str ):
1819 """Write content."""
@@ -23,7 +24,7 @@ def _write_ref_content(source: Path, module_name: str, func_name: str):
2324 )
2425
2526
26- def generate_reference (source : Path , destination : Path ):
27+ def _generate_reference (source : Path , destination : Path ):
2728 """Generate reference."""
2829 nav_items : Dict [str , List [str ]] = {"Code Reference" : []}
2930 # clean destination
@@ -43,7 +44,7 @@ def generate_reference(source: Path, destination: Path):
4344 return nav_items
4445
4546
46- def update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
47+ def _update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
4748 """Temporary update to mkdocs config."""
4849 copy (source , destination )
4950 with open (source , "rt" ) as mkf :
@@ -53,25 +54,27 @@ def update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, L
5354 safe_dump (mkdocs_conf , mkf , sort_keys = False )
5455
5556
56- def generate_documentation (source : Path ):
57+ def generate_documentation (source : Path , discard_refs : bool = True ):
5758 """Generate documentation."""
5859 # copy readme as docs index file
5960 copy (source / "README.md" , source / "docs/index.md" )
6061 # generate reference documentation
61- nav_items = generate_reference (source / "validators/__init__.py" , source / "docs/reference" )
62+ nav_items = _generate_reference (source / "validators/__init__.py" , source / "docs/reference" )
6263 # backup mkdocs config
63- update_mkdocs_config (source / "mkdocs.yml" , source / "mkdocs.bak.yml" , nav_items )
64+ _update_mkdocs_config (source / "mkdocs.yml" , source / "mkdocs.bak.yml" , nav_items )
6465 # build docs as subprocess
6566 print (run (("mkdocs" , "build" ), capture_output = True ).stderr .decode ())
6667 # restore mkdocs config
6768 move (str (source / "mkdocs.bak.yml" ), source / "mkdocs.yml" )
69+ # optionally discard reference folder
70+ if discard_refs :
71+ rmtree (source / "docs/reference" )
6872
6973
7074if __name__ == "__main__" :
71- project_dir = Path (__file__ ).parent .parent
72- generate_documentation (project_dir )
73- # use this option before building package
74- # with `poetry build` to include refs
75- if len (argv ) > 1 and argv [1 ] == "--keep" :
76- quit ()
77- rmtree (project_dir / "docs/reference" )
75+ project_root = Path (__file__ ).parent .parent
76+ generate_documentation (project_root )
77+ # NOTE: use following lines only for testing/debugging
78+ # generate_documentation(project_root, discard_refs=False)
79+ # from sys import argv
80+ # generate_documentation(project_root, len(argv) > 1 and argv[1] == "--keep")
0 commit comments