-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamplesExporter.py
52 lines (47 loc) · 1.96 KB
/
ExamplesExporter.py
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
#!/usr/bin/env python3
from FileSystemManager import FileSystemManager
import os
import json
from typing import Any
class ExamplesExporter:
def export(self, data: dict[str, Any], target_path: str) -> None:
"""Export examples and component examples to JSON files"""
self._export_examples(data, target_path)
self._export_components_examples(data, target_path)
def _export_examples(self, data: dict[str, Any], target_path: str) -> None:
if "examples" not in data:
return
for example_id, example in data["examples"].items():
if "renderable" not in example:
continue
renderable = example["renderable"]
if renderable is list and len(renderable) == 1:
renderable = renderable[0]
parts = [
target_path,
"examples",
example_id + ".json",
]
path = os.path.join(*parts)
content = json.dumps(renderable, indent=4, ensure_ascii=False)
FileSystemManager.write_file(path, content)
def _export_components_examples(
self, data: dict[str, Any], target_path: str
) -> None:
for component_id, component in data["components"].items():
if "examples" not in component:
continue
for example_id, example in component["examples"].items():
if "renderable" not in example:
continue
renderable = example["renderable"]
if renderable is list and len(renderable) == 1:
renderable = renderable[0]
parts = [
target_path,
"tests",
component_id + "--" + example_id + ".json",
]
path = os.path.join(*parts)
content = json.dumps(renderable, indent=4, ensure_ascii=False)
FileSystemManager.write_file(path, content)