-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_models.py
59 lines (49 loc) · 1.63 KB
/
generate_models.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
53
54
55
56
57
58
59
import json
from pathlib import Path
from tempfile import TemporaryDirectory
import httpx
from datamodel_code_generator import (
DataModelType,
InputFileType,
PythonVersion,
generate,
)
def main() -> int:
json_schema = httpx.get(
"https://grand-challenge.org/api/schema/",
headers={"accept": "application/json"},
)
with TemporaryDirectory(
prefix="gcapi_modelgen_"
) as temporary_directory_name:
temporary_directory = Path(temporary_directory_name)
input = temporary_directory / "schema.json"
output = temporary_directory / "models.py"
with open(input, "w") as f:
f.write(json.dumps(json_schema.json()))
generate(
input,
output=output,
base_class="gcapi.model_base.BaseModel",
strip_default_none=True,
strict_nullable=True,
output_model_type=DataModelType.DataclassesDataclass,
target_python_version=PythonVersion.PY_39,
input_file_type=InputFileType.OpenAPI,
)
with open(Path(__file__).parent / "gcapi" / "models.py", "w") as f:
text = output.read_text()
to_replace = "from dataclasses import dataclass"
if to_replace not in text:
raise ValueError(
"Could not insert dataclass import for pydantic"
)
f.write(
text.replace(
to_replace,
"from pydantic.dataclasses import dataclass",
)
)
return 0
if __name__ == "__main__":
raise (SystemExit(main()))