forked from Soju06/python-kis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_api_reference.py
More file actions
130 lines (99 loc) ยท 4.1 KB
/
generate_api_reference.py
File metadata and controls
130 lines (99 loc) ยท 4.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Generate API reference documentation from source code.
This script extracts docstrings and type hints from pykis modules
and generates markdown documentation.
"""
import ast
import inspect
import os
from pathlib import Path
from typing import Any, List, Dict
def extract_module_info(module_path: Path) -> Dict[str, Any]:
"""Extract classes, functions, and their docstrings from a Python module."""
with open(module_path, "r", encoding="utf-8") as f:
tree = ast.parse(f.read())
classes = []
functions = []
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
docstring = ast.get_docstring(node) or "(No docstring)"
methods = []
for item in node.body:
if isinstance(item, ast.FunctionDef):
if not item.name.startswith("_"): # Public methods only
method_doc = ast.get_docstring(item) or ""
methods.append({
"name": item.name,
"docstring": method_doc.split("\n")[0] if method_doc else ""
})
classes.append({
"name": node.name,
"docstring": docstring,
"methods": methods
})
elif isinstance(node, ast.FunctionDef):
if not node.name.startswith("_"): # Public functions only
docstring = ast.get_docstring(node) or "(No docstring)"
functions.append({
"name": node.name,
"docstring": docstring
})
return {"classes": classes, "functions": functions}
def generate_markdown(modules: Dict[str, Dict[str, Any]]) -> str:
"""Generate markdown documentation from extracted module info."""
md = ["# API Reference\n\n"]
md.append("์๋ ์์ฑ๋ API ๋ ํผ๋ฐ์ค ๋ฌธ์์
๋๋ค.\n\n")
md.append("---\n\n")
md.append("## ๋ชฉ์ฐจ\n\n")
# Table of contents
for module_name in sorted(modules.keys()):
md.append(f"- [{module_name}](#{module_name.replace('.', '-')})\n")
md.append("\n---\n\n")
# Module details
for module_name, info in sorted(modules.items()):
md.append(f"## {module_name}\n\n")
if info["classes"]:
md.append("### Classes\n\n")
for cls in info["classes"]:
md.append(f"#### `{cls['name']}`\n\n")
md.append(f"{cls['docstring']}\n\n")
if cls["methods"]:
md.append("**Methods:**\n\n")
for method in cls["methods"]:
md.append(f"- `{method['name']}()`: {method['docstring']}\n")
md.append("\n")
if info["functions"]:
md.append("### Functions\n\n")
for func in info["functions"]:
md.append(f"#### `{func['name']}()`\n\n")
md.append(f"{func['docstring']}\n\n")
md.append("---\n\n")
return "".join(md)
def main():
"""Main entry point for API reference generation."""
repo_root = Path(__file__).parent.parent
pykis_dir = repo_root / "pykis"
# Target modules for API reference (public API only)
target_files = [
"kis.py",
"simple.py",
"helpers.py",
"public_types.py",
"client/auth.py",
]
modules = {}
for file_path in target_files:
full_path = pykis_dir / file_path
if full_path.exists():
module_name = f"pykis.{file_path.replace('.py', '').replace('/', '.')}"
modules[module_name] = extract_module_info(full_path)
# Generate markdown
md_content = generate_markdown(modules)
# Write to file
output_path = repo_root / "docs" / "generated" / "API_REFERENCE.md"
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
f.write(md_content)
print(f"โ
API Reference generated: {output_path}")
if __name__ == "__main__":
main()