-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs_extractor.py
More file actions
71 lines (53 loc) · 2.27 KB
/
Copy pathdocs_extractor.py
File metadata and controls
71 lines (53 loc) · 2.27 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Docstring extractor for sph_client - generates markdown documentation from code.
Run this to extract docs that can be loaded into AI context.
"""
import ast
import os
from pathlib import Path
from typing import Any, Dict, List, Optional
def get_class_methods(filepath: Path) -> Dict[str, Any]:
"""Extract class methods and their docstrings from a file."""
with open(filepath, "r", encoding="utf-8") as f:
source = f.read()
tree = ast.parse(source)
methods = {}
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
docstring = ast.get_docstring(node)
if docstring:
methods[node.name] = {
"docstring": docstring,
"args": [arg.arg for arg in node.args.args],
"defaults": node.args.defaults,
}
return methods
def extract_api_docs(project_root: Path) -> str:
"""Extract all API docs and format as markdown."""
modules = {
"base": project_root / "schulportal_hessen" / "base.py",
"login": project_root / "schulportal_hessen" / "applets" / "login" / "api.py",
"kalender": project_root / "schulportal_hessen" / "applets" / "kalender" / "api.py",
"dsb": project_root / "schulportal_hessen" / "external" / "dsb" / "api.py",
}
docs = ["## sph_client API Documentation\n"]
for name, path in modules.items():
if path.exists():
methods = get_class_methods(path)
docs.append(f"\n### {name.upper()}\n")
for method_name, info in methods.items():
if info["docstring"]:
docs.append(f"#### {method_name}\n")
docs.append(f"{info['docstring']}\n")
return "\n".join(docs)
def save_docs(project_root: Path, output_path: Optional[Path] = None):
"""Generate and save documentation."""
if output_path is None:
output_path = project_root / "docs" / "API.md"
output_path.parent.mkdir(parents=True, exist_ok=True)
docs = extract_api_docs(project_root)
output_path.write_text(docs, encoding="utf-8")
print(f"Documentation saved to {output_path}")
if __name__ == "__main__":
project_root = Path(__file__).parent
save_docs(project_root)