-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_api_docs.py
More file actions
executable file
·320 lines (252 loc) · 9.79 KB
/
generate_api_docs.py
File metadata and controls
executable file
·320 lines (252 loc) · 9.79 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
"""
Generate API documentation for NetGraph
This script should be run from the project root directory.
By default, outputs documentation to stdout.
Use --write-file to write to docs/reference/api-full.md instead.
"""
import argparse
import dataclasses
import glob
import importlib
import inspect
import os
import sys
from datetime import datetime
from pathlib import Path
# Add the current directory to Python path for development installs
if os.path.exists("ngraph"):
sys.path.insert(0, ".")
def discover_modules():
"""Automatically discover all documentable Python modules in the ngraph package."""
modules = []
# Find all .py files in ngraph/
for py_file in glob.glob("ngraph/**/*.py", recursive=True):
# Skip files that shouldn't be documented
filename = os.path.basename(py_file)
if filename in ["__init__.py", "__main__.py"]:
continue
# Convert file path to module name
module_path = py_file.replace("/", ".").replace(".py", "")
modules.append(module_path)
# Sort modules in logical order for documentation
def module_sort_key(module_name):
"""Sort key to organize modules logically."""
parts = module_name.split(".")
# Main ngraph modules first
if len(parts) == 2: # ngraph.xxx
return (0, parts[1])
# Then lib modules
elif len(parts) == 3 and parts[1] == "lib": # ngraph.lib.xxx
return (1, parts[2])
# Then algorithm modules
elif len(parts) == 4 and parts[1:3] == [
"lib",
"algorithms",
]: # ngraph.lib.algorithms.xxx
return (2, parts[3])
# Then workflow modules
elif len(parts) == 3 and parts[1] == "workflow": # ngraph.workflow.xxx
return (3, parts[2])
# Then transform modules
elif len(parts) == 3 and parts[1] == "transform": # ngraph.transform.xxx
return (4, parts[2])
# Everything else at the end
else:
return (9, module_name)
modules.sort(key=module_sort_key)
return modules
def get_class_info(cls):
"""Extract comprehensive information about a class."""
info = {
"name": cls.__name__,
"doc": inspect.getdoc(cls) or "No documentation available.",
"methods": [],
"attributes": [],
}
# Get methods (including static and class methods)
for name, method in inspect.getmembers(cls):
if not name.startswith("_") and (
inspect.ismethod(method) or inspect.isfunction(method)
):
try:
sig = str(inspect.signature(method))
except (ValueError, TypeError):
sig = "()"
method_doc = inspect.getdoc(method)
info["methods"].append(
{
"name": name,
"signature": sig,
"doc": (
method_doc.split("\n")[0]
if method_doc
else "No documentation available."
),
}
)
# Get dataclass fields if applicable
if hasattr(cls, "__dataclass_fields__"):
for field_name, field in cls.__dataclass_fields__.items():
field_type = getattr(field.type, "__name__", str(field.type))
# Handle dataclass field defaults properly
if field.default is not dataclasses.MISSING:
default_val = field.default
elif field.default_factory is not dataclasses.MISSING:
try:
# Try to call the factory to get a representative value
default_val = field.default_factory()
except Exception:
default_val = f"{field.default_factory.__name__}()"
else:
default_val = None
info["attributes"].append(
{
"name": field_name,
"type": field_type,
"default": str(default_val) if default_val is not None else None,
}
)
return info
def get_function_info(func):
"""Extract information about a function."""
try:
sig = str(inspect.signature(func))
except (ValueError, TypeError):
sig = "()"
return {
"name": func.__name__,
"signature": sig,
"doc": inspect.getdoc(func) or "No documentation available.",
}
def document_module(module_name):
"""Generate documentation for a single module."""
try:
module = importlib.import_module(module_name)
except ImportError as e:
return f"## {module_name}\n\n**Error importing module:** {e}\n\n---\n"
doc = f"## {module_name}\n\n"
# Module docstring
if module.__doc__:
doc += f"{module.__doc__.strip()}\n\n"
# Get classes and functions defined in this module
classes = []
functions = []
for name, obj in inspect.getmembers(module):
if not name.startswith("_"):
if inspect.isclass(obj) and obj.__module__ == module_name:
classes.append(get_class_info(obj))
elif inspect.isfunction(obj) and obj.__module__ == module_name:
functions.append(get_function_info(obj))
# Document classes
for cls_info in classes:
doc += f"### {cls_info['name']}\n\n"
doc += f"{cls_info['doc']}\n\n"
if cls_info["attributes"]:
doc += "**Attributes:**\n\n"
for attr in cls_info["attributes"]:
type_info = f" ({attr['type']})" if attr["type"] != "typing.Any" else ""
default_info = f" = {attr['default']}" if attr["default"] else ""
doc += f"- `{attr['name']}`{type_info}{default_info}\n"
doc += "\n"
if cls_info["methods"]:
doc += "**Methods:**\n\n"
for method in cls_info["methods"]:
doc += f"- `{method['name']}{method['signature']}`\n"
if method["doc"] and method["doc"] != "No documentation available.":
doc += f" - {method['doc']}\n"
doc += "\n"
# Document functions
for func_info in functions:
doc += f"### {func_info['name']}{func_info['signature']}\n\n"
doc += f"{func_info['doc']}\n\n"
doc += "---\n\n"
return doc
def generate_api_documentation(output_to_file=False):
"""Generate the complete API documentation.
Args:
output_to_file (bool): If True, write to docs/reference/api-full.md.
If False, return the documentation string.
Returns:
str: The generated documentation (when output_to_file=False)
"""
# Automatically discover all documentable modules
modules = discover_modules()
print(f"🔍 Auto-discovered {len(modules)} modules to document...")
# Generate header
timestamp = datetime.now().strftime("%B %d, %Y at %H:%M UTC")
header = f"""# NetGraph API Reference (Auto-Generated)
This is the complete auto-generated API documentation for NetGraph.
For a curated, example-driven API guide, see **[api.md](api.md)**.
> **📋 Documentation Types:**
> - **[Main API Guide (api.md)](api.md)** - Curated examples and usage patterns
> - **This Document (api-full.md)** - Complete auto-generated reference
> - **[CLI Reference](cli.md)** - Command-line interface
> - **[DSL Reference](dsl.md)** - YAML syntax guide
**Generated from source code on:** {timestamp}
**Modules auto-discovered:** {len(modules)}
---
"""
print("📝 Generating API documentation...")
doc = header
# Generate documentation for each module
for module_name in modules:
print(f" 📝 Documenting {module_name}")
try:
module_doc = document_module(module_name)
doc += module_doc
except Exception as e:
print(f" ⚠️ Error documenting {module_name}: {e}")
doc += f"## {module_name}\n\n**Error:** Could not generate documentation for this module: {e}\n\n---\n\n"
# Add footer
footer = """
## Error Handling
NetGraph uses standard Python exceptions:
- `ValueError` - For validation errors
- `KeyError` - For missing required fields
- `RuntimeError` - For runtime errors
For complete method signatures and detailed documentation, use Python's help system:
```python
help(ngraph.scenario.Scenario)
help(ngraph.network.Network.max_flow)
```
---
*This documentation was auto-generated from the NetGraph source code.*
"""
doc += footer
if output_to_file:
# Ensure output directory exists
output_path = Path("docs/reference/api-full.md")
output_path.parent.mkdir(parents=True, exist_ok=True)
# Write to file
with open(output_path, "w", encoding="utf-8") as f:
f.write(doc)
print("✅ API documentation generated successfully!")
print(f"📄 Written to: {output_path}")
print(f"📊 Size: {len(doc):,} characters")
print(f"📚 Modules documented: {len(modules)}")
else:
# Return the documentation string
return doc
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate API documentation for NetGraph",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python generate_api_docs.py # Output to stdout
python generate_api_docs.py --write-file # Write to docs/reference/api-full.md
""",
)
parser.add_argument(
"--write-file",
action="store_true",
help="Write documentation to docs/reference/api-full.md instead of stdout",
)
args = parser.parse_args()
if args.write_file:
generate_api_documentation(output_to_file=True)
else:
# Output to stdout
doc = generate_api_documentation(output_to_file=False)
print(doc)