-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathcreate_table.py
More file actions
295 lines (245 loc) · 9.42 KB
/
Copy pathcreate_table.py
File metadata and controls
295 lines (245 loc) · 9.42 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
import json
from pathlib import Path
import subprocess
from typing import Any
# --- CONFIGURATION ---
SHOPPING_SCHEMAS_DIR = Path("code/sdk/schemas/ap2")
def _load_json(path: str | Path) -> dict[str, Any] | None:
try:
with Path(path).open(encoding="utf-8") as f:
return json.load(f)
except (json.JSONDecodeError, OSError):
return None
def _resolve_json_pointer(pointer: str, data: Any) -> Any | None:
"""Navigate to a JSON pointer path (e.g., '#/$defs/foo' or '#/components/x').
Args:
pointer: JSON pointer starting with '#' (e.g., '#/$defs/allocation').
data: The JSON data to navigate.
Returns:
The value at the pointer path, or None if not found.
"""
if pointer == "#":
return data
if not pointer.startswith("#/"):
return None
path_parts = pointer[2:].split("/")
current = data
for part in path_parts:
if isinstance(current, dict) and part in current:
current = current[part]
elif isinstance(current, list):
try:
current = current[int(part)]
except (ValueError, IndexError):
return None
else:
return None
return current
def _to_pascal_case(s: str) -> str:
return "".join(w.capitalize() for w in s.split("_"))
def _resolve_ref_type(
ref: str,
external_enum_refs: list | None = None,
) -> str:
"""Return the Markdown type string for a $ref value.
Internal refs (starting with '#/') become anchor links to the matching
section on the same page. External file refs always become anchor links
too — the section is expected to already exist on the page (object types)
or is generated automatically (enum types). Enum refs are appended to
*external_enum_refs* so the caller can render their subsection.
"""
if ref.startswith("#/"):
name = ref.split("/")[-1]
return f"[{_to_pascal_case(name)}](#{_to_pascal_case(name).lower()})"
filename = Path(ref).name.replace(".json", "")
pascal_name = _to_pascal_case(filename)
anchor = pascal_name.lower()
if external_enum_refs is not None:
ext_schema = _load_json(SHOPPING_SCHEMAS_DIR / ref)
if ext_schema and ext_schema.get("enum"):
entry = (ref, pascal_name, ext_schema)
if entry not in external_enum_refs:
external_enum_refs.append(entry)
return f"[{pascal_name}](#{anchor})"
def _render_table_from_schema(
schema_data,
spec_file_name,
need_header=True,
context=None,
show_sd_column=True,
external_enum_refs: list | None = None,
):
"""Render a Markdown table from a schema dictionary.
Schema dictionary must contain 'properties'. 'required' list is optional.
Args:
schema_data: A dictionary representing the JSON schema.
spec_file_name: The name of the spec file indicating where the dictionary
should be rendered.
need_header: Optional. Whether to render the header row.
context: Optional. A dictionary providing context.
show_sd_column: Optional. Whether to force display the Selectively
Disclosable column.
Returns:
A string containing a Markdown table representing the schema properties.
"""
if not schema_data:
return "_No properties defined._"
properties = schema_data.get("properties", {})
required_list = schema_data.get("required", [])
if not properties:
return "_No properties defined._"
# Check if any field is selectively disclosable
has_sd = any(
isinstance(details, dict)
and (
details.get("x-selectively-disclosable-field")
or details.get("x-selectively-disclosable-array")
)
for details in properties.values()
)
if not has_sd:
for details in properties.values():
if not isinstance(details, dict):
continue
if "anyOf" in details.get("items", {}):
for item in details["items"]["anyOf"]:
if "$ref" in item and item["$ref"].startswith("#/$defs/"):
def_name = item["$ref"].split("/")[-1]
def_details = schema_data.get("$defs", {}).get(def_name, {})
for p_details in def_details.get("properties", {}).values():
if isinstance(p_details, dict) and (
p_details.get("x-selectively-disclosable-field")
or p_details.get("x-selectively-disclosable-array")
):
has_sd = True
break
if has_sd:
break
if has_sd:
break
md = []
if need_header:
if show_sd_column and has_sd:
md.append(
"| Name | Type | Required | Selectively Disclosable | Description |"
)
md.append("| :--- | :--- | :--- | :--- | :--- |")
else:
md.append("| Name | Type | Required | Description |")
md.append("| :--- | :--- | :--- | :--- |")
for field_name, details in properties.items():
if not isinstance(details, dict):
continue
f_type = details.get("type", "any")
ref = details.get("$ref")
# Handle Reference
if ref:
f_type = _resolve_ref_type(ref, external_enum_refs)
elif items_ref := details.get("items", {}).get("$ref"):
f_type = f"Array[{_resolve_ref_type(items_ref, external_enum_refs)}]"
elif "anyOf" in details.get("items", {}):
any_of_items = details["items"]["anyOf"]
types = []
for item in any_of_items:
if "$ref" in item:
ref_name = Path(item["$ref"]).name.replace(".json", "")
if item["$ref"].startswith("#/$defs/"):
ref_name = item["$ref"].split("/")[-1]
types.append(
f"[{_to_pascal_case(ref_name)}](#{_to_pascal_case(ref_name).lower()})"
)
elif "type" in item:
types.append(f"`{item['type']}`")
if types:
f_type = f"Array[{', '.join(types)}]"
elif f_type == "array":
inner_type = details.get("items", {}).get("type", "any")
f_type = f"Array[`{inner_type}`]"
desc = details.get("description", "")
# Replace newlines with <br> for HTML table
desc = desc.replace("\n", "<br>")
# Enum
enum_values = details.get("enum")
if enum_values and isinstance(enum_values, list):
formatted_enums = ", ".join([f"`{str(v)}`" for v in enum_values])
if desc:
desc += "<br>"
desc += f"**Enum:** {formatted_enums}"
req_display = "**Yes**" if field_name in required_list else "No"
if isinstance(details, dict) and (
details.get("x-selectively-disclosable-field")
or details.get("x-selectively-disclosable-array")
):
sd_display = "Yes"
else:
sd_display = "No"
# Deep scan anyOf in arrays for SD fields in local $defs
if sd_display == "No" and "anyOf" in details.get("items", {}):
for item in details["items"]["anyOf"]:
if "$ref" in item and item["$ref"].startswith("#/$defs/"):
def_name = item["$ref"].split("/")[-1]
def_details = schema_data.get("$defs", {}).get(def_name, {})
for p_details in def_details.get("properties", {}).values():
if isinstance(p_details, dict) and (
p_details.get("x-selectively-disclosable-field")
or p_details.get("x-selectively-disclosable-array")
):
sd_display = "Yes"
break
if sd_display == "Yes":
break
if show_sd_column and has_sd:
md.append(
f"| {field_name} | {f_type} | {req_display} | {sd_display} | {desc} |"
)
else:
md.append(f"| {field_name} | {f_type} | {req_display} | {desc} |")
return "\n".join(md)
def define_env(env):
"""Injects custom macros into the MkDocs environment.
This function is called by MkDocs and receives the `env` object,
allowing it to register custom macros like `schema_fields` and
`method_fields` for use in Markdown pages.
Args:
env: The MkDocs environment object.
"""
@env.macro
def schema_fields(entity_name, spec_file_name, show_sd=True, pointer=None):
"""Parse a standalone JSON Schema file and render a table.
Args:
entity_name: The name of the schema file (without .json).
spec_file_name: The name of the specification file for link generation.
show_sd: Optional. Whether to force display the Selectively Disclosable
column. Default would show sd when sd presents.
pointer: Optional. JSON pointer to resolve within the schema.
Returns:
A Markdown table as a string.
"""
base_name = entity_name
full_path = SHOPPING_SCHEMAS_DIR / (base_name + ".json")
if not full_path.exists():
return f"_Schema file not found at {full_path}_"
schema_data = _load_json(full_path)
if not schema_data:
return f"_Failed to load or parse schema from {full_path}_"
external_enum_refs: list = []
render = lambda data: _render_table_from_schema(
data, spec_file_name, show_sd_column=show_sd,
external_enum_refs=external_enum_refs,
)
if pointer:
schema_data = _resolve_json_pointer(pointer, schema_data)
if not schema_data:
return f"_Pointer {pointer} not found in schema_"
output = [render(schema_data)]
if not pointer:
for def_name, def_schema in schema_data.get("$defs", {}).items():
output.append(f"\n### {_to_pascal_case(def_name)}")
output.append(render(def_schema))
for _, pascal_name, ext_schema in external_enum_refs:
output.append(f"\n### {pascal_name}")
if desc := ext_schema.get("description"):
output.append(f"\n{desc}\n")
values = " ".join(f"`{v}`" for v in ext_schema["enum"])
output.append(f"**Values:** {values}")
return "\n".join(output)