-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_to_mdx.py
More file actions
54 lines (47 loc) · 2.22 KB
/
schema_to_mdx.py
File metadata and controls
54 lines (47 loc) · 2.22 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
import json
def schema_to_mdx(json_data, indent=0):
mdx = ""
for key, value in json_data.items():
if isinstance(value, dict):
if value.get('type') == 'json':
mdx += " " * indent + f'<Accordion title="{key}">\n'
mdx += " " * (indent + 1) + "```json\n"
json_content = json.dumps(value.get('columns', {}), indent=2)
mdx += "\n".join(" " * (indent + 1) + line for line in json_content.split("\n"))
mdx += "\n"
mdx += " " * (indent + 1) + "```\n"
mdx += " " * indent + "</Accordion>\n"
elif value.get('type') == 'csv':
mdx += " " * indent + f'<Accordion title="{key}">\n'
columns = value.get('columns', {})
mdx += " " * (indent + 1) + "| Column | Data Type | Example Value | Nullable |\n"
mdx += " " * (indent + 1) + "|---------------|-----------|---------------|----------|\n"
for col_name, col_info in columns.items():
data_type = col_info.get('data_type', '')
example = col_info.get('example_value', '')
nullable = col_info.get('nullable', '')
mdx += " " * (indent + 1) + f"| {col_name} | {data_type} | {example} | {nullable} |\n"
mdx += " " * indent + "</Accordion>\n"
elif not value:
mdx += " " * indent + f'<Card title="{key}"></Card>\n'
else:
mdx += " " * indent + f'<Accordion title="{key}">\n'
mdx += " " * (indent + 1) + "<AccordionGroup>\n"
mdx += schema_to_mdx(value, indent + 2)
mdx += " " * (indent + 1) + "</AccordionGroup>\n"
mdx += " " * indent + "</Accordion>\n"
else:
mdx += " " * indent + f'<Card title="{key}"></Card>\n'
return mdx
def generate_mdx(json_data, output_file):
mdx_content = f"""# Data Export Format
<Info>
This is an optional info card that you can use.
</Info>
Export type: [insert export type]
<AccordionGroup>
{schema_to_mdx(json_data)}
</AccordionGroup>
"""
with open(output_file, 'w', encoding='utf-8') as f:
f.write(mdx_content)