Repository navigation
Expand file tree
/
Copy pathgen_docs.py
More file actions
51 lines (41 loc) · 2.07 KB
/
Copy pathgen_docs.py
File metadata and controls
51 lines (41 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import ast
import shutil
from pathlib import Path
from pydoctor import driver, model
base_dir = Path(__file__).parent.parent
class ProtobufSystem(model.System):
"""Expose dynamic protobuf types as names without documenting their internals.
Pydoctor discovers .py files, not adjacent .pyi stubs. The generated .py
files create types through runtime builder calls that its static analysis
cannot resolve. Read public top-level class names from the stubs and register
them as attributes so links resolve without generating full class pages.
"""
def addModule(self, path: Path, parent: model.Package | None) -> None:
super().addModule(path, parent)
stub = path.with_suffix(".pyi")
if path.name.endswith("_pb2.py") and stub.is_file():
name = f"{parent.fullName()}.{path.stem}" if parent else path.stem
module = self.allobjects[name]
assert isinstance(module, model.Module)
for node in ast.parse(stub.read_text(encoding="utf-8")).body:
if isinstance(node, ast.ClassDef) and not node.name.startswith("_"):
attribute = self.Attribute(self, node.name, module, stub)
attribute.kind = model.DocumentableKind.VARIABLE
attribute.parentMod = module
attribute.setLineNumber(node.lineno)
# The declaration is in the stub, not the runtime module.
if source_href := self._formatSourceHref(stub):
attribute.source_href = self.options.htmlsourcetemplate.format(
mod_source_href=source_href, lineno=str(node.lineno)
)
self.addObject(attribute)
if __name__ == "__main__":
print("Generating documentation...")
result = driver.main(["--quiet", "--system-class=__main__.ProtobufSystem"])
if result:
raise SystemExit(result)
# Copy favicon
shutil.copyfile(
base_dir / "scripts" / "_img" / "favicon.ico",
base_dir / "build" / "apidocs" / "favicon.ico",
)