1212https://mkdocstrings.github.io/recipes/#automatic-code-reference-pages
1313"""
1414
15+ import subprocess
16+ import tempfile
1517from pathlib import Path
1618from typing import Tuple
1719
@@ -34,7 +36,9 @@ def with_underscore_not_init(part: str) -> bool:
3436 return any (p for p in path_parts if with_underscore_not_init (p ))
3537
3638
37- def generate_api_pages (src_path : str = "src" , dst_path : str = "reference" ) -> None :
39+ def generate_python_api_pages (
40+ src_path : str = "src" , dst_path : str = "python-reference"
41+ ) -> None :
3842 """Generate API documentation pages for the code.
3943
4044 Internal modules (those starting with an underscore except from `__init__`) are
@@ -74,3 +78,66 @@ def generate_api_pages(src_path: str = "src", dst_path: str = "reference") -> No
7478
7579 with mkdocs_gen_files .open (Path (dst_path ) / "SUMMARY.md" , "w" ) as nav_file :
7680 nav_file .writelines (nav .build_literate_nav ())
81+
82+
83+ def generate_protobuf_api_pages (
84+ src_path : str = "proto" , dst_path : str = "protobuf-reference"
85+ ) -> None :
86+ """Generate API documentation pages for the code.
87+
88+ Internal modules (those starting with an underscore except from `__init__`) are
89+ not included.
90+
91+ A summary page is generated as `SUMMARY.md` which is compatible with the
92+ `mkdocs-literary-nav` plugin.
93+
94+ Args:
95+ src_path: Path where the code is located.
96+ dst_path: Path where the documentation should be generated. This is relative
97+ to the output directory of mkdocs.
98+ """
99+ # type ignore because mkdocs_gen_files uses a very weird module-level
100+ # __getattr__() which messes up the type system
101+ nav = mkdocs_gen_files .Nav () # type: ignore
102+
103+ cwd = Path .cwd ()
104+
105+ with tempfile .TemporaryDirectory (prefix = "mkdocs-protobuf-reference-" ) as tmp_path :
106+ for path in sorted (Path (src_path ).rglob ("*.proto" )):
107+ doc_path = path .relative_to (src_path ).with_suffix (".md" )
108+ full_doc_path = Path (dst_path , doc_path )
109+ parts = tuple (path .relative_to (src_path ).parts )
110+ nav [parts ] = doc_path .as_posix ()
111+ doc_tmp_path = tmp_path / doc_path
112+ doc_tmp_path .parent .mkdir (parents = True , exist_ok = True )
113+ try :
114+ # TODO: Get arguments from setuptools.grpc
115+ subprocess .run (
116+ [
117+ "docker" ,
118+ "run" ,
119+ "--rm" ,
120+ f"-v{ cwd } :{ cwd } " ,
121+ f"-v{ tmp_path } :{ tmp_path } " ,
122+ "pseudomuto/protoc-gen-doc" ,
123+ f"-I{ cwd / src_path } " ,
124+ f"-I{ cwd } /submodules/api-common-protos" ,
125+ f"-I{ cwd } /submodules/frequenz-api-common/proto" ,
126+ f"--doc_opt=markdown,{ doc_path .name } " ,
127+ f"--doc_out={ tmp_path / doc_path .parent } " ,
128+ str (cwd / path ),
129+ ],
130+ check = True ,
131+ )
132+ except subprocess .CalledProcessError as error :
133+ print (f"Error generating protobuf reference page: { error } " )
134+
135+ with doc_tmp_path .open () as input_file , mkdocs_gen_files .open (
136+ full_doc_path , "w"
137+ ) as output_file :
138+ output_file .write (input_file .read ())
139+
140+ mkdocs_gen_files .set_edit_path (full_doc_path , Path (".." ) / path )
141+
142+ with mkdocs_gen_files .open (Path (dst_path ) / "SUMMARY.md" , "w" ) as nav_file :
143+ nav_file .writelines (nav .build_literate_nav ())
0 commit comments