-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_model.py
More file actions
executable file
·39 lines (34 loc) · 1.35 KB
/
generate_model.py
File metadata and controls
executable file
·39 lines (34 loc) · 1.35 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
from glob import glob
from pathlib import Path
from tempfile import NamedTemporaryFile
from datamodel_code_generator import InputFileType, generate, DataModelType
from datamodel_code_generator.model import PythonVersion
SCHEMA_DIR = Path("format/schemas")
OUTPUT_DIR = Path("src/ethdebug/format")
# Ensure output directory exists
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# Temporarily rename LICENSE file so it doesn't cause parsing issues
license_path = SCHEMA_DIR / "LICENSE"
with NamedTemporaryFile(delete=False) as tmp_file:
if license_path.exists():
license_path.rename(tmp_file.name)
temp_license_path = Path(tmp_file.name)
try:
generate(
input_=SCHEMA_DIR,
input_file_type=InputFileType.JsonSchema,
output=OUTPUT_DIR,
output_model_type=DataModelType.PydanticV2BaseModel,
target_python_version=PythonVersion.PY_312,
allow_extra_fields=False,
disable_timestamp=True,
reuse_model=True,
use_annotated=True,
field_constraints=True,
custom_class_name_generator=lambda x: x.title().replace("Ethdebug/Format/", "").replace("/", "_"),
use_exact_imports=True
)
finally:
# Restore LICENSE file
if temp_license_path.exists():
temp_license_path.rename(license_path)