-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_schema_docs.py
More file actions
339 lines (292 loc) · 10.9 KB
/
generate_schema_docs.py
File metadata and controls
339 lines (292 loc) · 10.9 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
"""
Generate comprehensive schema documentation for all endpoints.
This script generates RST documentation files for each endpoint schema,
including:
- Schema overview
- Method listings with parameters
- Example code for each functional method
- Deep dive into schema JSON
"""
from pathlib import Path
from pymlb_statsapi import api
from pymlb_statsapi.model.registry import EXCLUDED_METHODS
def get_functional_methods(endpoint_name: str) -> tuple[list[str], list[str]]:
"""
Get lists of functional and non-functional methods for an endpoint.
Returns:
Tuple of (functional_methods, non_functional_methods)
"""
endpoint = api.get_endpoint(endpoint_name)
# get_method_names() already excludes non-functional methods
functional = endpoint.get_method_names()
# Get excluded methods from the registry
non_functional = list(EXCLUDED_METHODS.get(endpoint_name, set()))
return functional, non_functional
def generate_method_example(endpoint_name: str, method_name: str) -> str:
"""Generate a Python code example for a method."""
try:
info = api.get_method_info(endpoint_name, method_name)
params = info.get("query_params", []) + info.get("path_params", [])
# Build example parameters
example_params = []
for param in params[:3]: # Limit to 3 params for brevity
param_name = param["name"]
param_type = param.get("type", "string")
# Generate example values based on type
if param_type == "integer":
example_val = "1"
elif param_name in ["date", "startDate", "endDate"]:
example_val = '"2024-07-04"'
elif param_name in ["season"]:
example_val = '"2024"'
elif param_name.endswith("Id"):
example_val = "147"
else:
example_val = '"value"'
example_params.append(f"{param_name}={example_val}")
params_str = ", ".join(example_params) if example_params else ""
code = f"""from pymlb_statsapi import api
# {info.get("summary", "API call")}
response = api.{endpoint_name.capitalize()}.{method_name}({params_str})
data = response.json()
# Save to file
result = response.save_json(prefix="mlb-data")
print(f"Saved to: {{result['path']}}")"""
return code
except Exception as e:
return f"# Error generating example: {e}"
def generate_endpoint_doc(endpoint_name: str, output_dir: Path):
"""Generate RST documentation for a single endpoint."""
functional_methods, non_functional_methods = get_functional_methods(endpoint_name)
# Start building RST content
title = f"{endpoint_name.capitalize()} Endpoint"
lines = [
title,
"=" * len(title),
"",
f"The ``{endpoint_name}`` endpoint provides access to {endpoint_name}-related data from the MLB Stats API.",
"",
".. contents:: Table of Contents",
" :local:",
" :depth: 2",
"",
]
# Overview section
lines.extend(
[
"Overview",
"-" * 50,
"",
f"This endpoint has **{len(functional_methods)} functional methods** and "
f"**{len(non_functional_methods)} non-functional methods**.",
"",
]
)
# Functional methods section
if functional_methods:
lines.extend(
[
"Functional Methods",
"-" * 50,
"",
"The following methods are fully functional and tested:",
"",
]
)
for method_name in sorted(functional_methods):
try:
info = api.get_method_info(endpoint_name, method_name)
lines.extend(
[
f"{method_name}()",
"^" * (len(method_name) + 2),
"",
f"**Summary:** {info.get('summary', 'N/A')}",
"",
f"**Path:** ``{info.get('path', 'N/A')}``",
"",
]
)
# Parameters
path_params = info.get("path_params", [])
query_params = info.get("query_params", [])
if path_params:
lines.append("**Path Parameters:**")
lines.append("")
for param in path_params:
req = "**required**" if param.get("required") else "*optional*"
lines.append(
f"- ``{param['name']}`` (*{param.get('type', 'string')}*, {req}): {param.get('description', 'N/A')}"
)
lines.append("")
if query_params:
lines.append("**Query Parameters:**")
lines.append("")
for param in query_params[:5]: # Show first 5
req = "**required**" if param.get("required") else "*optional*"
lines.append(
f"- ``{param['name']}`` (*{param.get('type', 'string')}*, {req}): {param.get('description', 'N/A')}"
)
if len(query_params) > 5:
lines.append(f"- ... and {len(query_params) - 5} more parameters")
lines.append("")
# Example
lines.extend(
[
"**Example:**",
"",
".. code-block:: python",
"",
]
)
example = generate_method_example(endpoint_name, method_name)
for line in example.split("\n"):
lines.append(f" {line}")
lines.extend(["", ""])
except Exception as e:
lines.extend(
[
f"*Error generating documentation: {e}*",
"",
]
)
# Non-functional methods section
if non_functional_methods:
lines.extend(
[
"",
"Non-Functional Methods",
"-" * 50,
"",
".. warning::",
"",
" The following methods are **not functional** due to issues in the MLB Stats API or schema mismatches:",
"",
]
)
for method_name in sorted(non_functional_methods):
lines.append(f" - ``{method_name}()``")
lines.extend(
[
"",
" These methods are excluded from the API and will not be available.",
"",
]
)
# Schema introspection section
lines.extend(
[
"",
"Schema Introspection",
"-" * 50,
"",
f"You can explore the full schema for the ``{endpoint_name}`` endpoint programmatically:",
"",
".. code-block:: python",
"",
" from pymlb_statsapi import api",
"",
" # List all methods",
f" methods = api.{endpoint_name.capitalize()}.get_method_names()",
" print(methods)",
"",
" # Get method details",
f" method = api.{endpoint_name.capitalize()}.get_method('{'schedule' if endpoint_name == 'schedule' else functional_methods[0] if functional_methods else 'method_name'}')",
" schema = method.get_schema()",
" print(json.dumps(schema, indent=2))",
"",
" # Get detailed description",
f" description = api.{endpoint_name.capitalize()}.describe_method('{'schedule' if endpoint_name == 'schedule' else functional_methods[0] if functional_methods else 'method_name'}')",
" print(description)",
"",
]
)
# Write to file
output_file = output_dir / f"{endpoint_name}.rst"
output_file.write_text("\n".join(lines))
print(f"Generated: {output_file}")
def generate_index_doc(endpoint_names: list[str], output_dir: Path):
"""Generate the schemas index page."""
lines = [
"Schema Reference",
"=" * 50,
"",
"This section provides comprehensive documentation for all MLB Stats API endpoints.",
"",
".. note::",
"",
" These schemas were sourced from the MLB Stats API Beta documentation",
" (https://beta-statsapi.mlb.com/docs/), which is no longer publicly available.",
"",
"Each endpoint page includes:",
"",
"- Overview of functional and non-functional methods",
"- Detailed parameter documentation",
"- Python code examples",
"- Schema introspection examples",
"",
".. toctree::",
" :maxdepth: 1",
" :caption: Endpoints",
"",
]
for name in sorted(endpoint_names):
lines.append(f" {name}")
lines.extend(
[
"",
"Quick Reference",
"-" * 50,
"",
".. code-block:: python",
"",
" from pymlb_statsapi import api",
"",
" # List all available endpoints",
" endpoints = api.get_endpoint_names()",
" print(endpoints)",
"",
" # Get methods for an endpoint",
" methods = api.Schedule.get_method_names()",
" print(methods)",
"",
" # Get detailed info about a method",
" info = api.get_method_info('schedule', 'schedule')",
" print(info)",
"",
]
)
output_file = output_dir / "index.rst"
output_file.write_text("\n".join(lines))
print(f"Generated: {output_file}")
def main():
"""Main entry point."""
# Create output directory
docs_dir = Path(__file__).parent.parent / "docs"
schemas_dir = docs_dir / "schemas"
schemas_dir.mkdir(exist_ok=True)
print("Generating schema documentation...")
print("=" * 70)
# Get all endpoints
endpoint_names = api.get_endpoint_names()
print(f"Found {len(endpoint_names)} endpoints")
print()
# Generate docs for each endpoint
for endpoint_name in sorted(endpoint_names):
try:
generate_endpoint_doc(endpoint_name, schemas_dir)
except Exception as e:
print(f"Error generating docs for {endpoint_name}: {e}")
# Generate index
generate_index_doc(endpoint_names, schemas_dir)
print()
print("=" * 70)
print(f"Generated documentation in: {schemas_dir}")
print()
print("Next steps:")
print("1. Add schemas/index to docs/index.rst toctree")
print("2. Run 'make docs' to build the documentation")
print("3. Run 'make serve-docs' to view locally")
if __name__ == "__main__":
main()