1212from ipyautoui .custom .filechooser import FileChooser
1313from ipyautoui .custom .date_string import DatePickerString , NaiveDatetimePickerString
1414from ipyautoui .autobox import AutoBox
15- from tempfile import TemporaryDirectory
16- import pathlib
17- from datamodel_code_generator import DataModelType , InputFileType , generate
18- import json
19- import importlib .util
20- import sys
2115
2216import logging
2317
2418logger = logging .getLogger (__name__ )
2519
26- def pydantic_model_file_from_json_schema (json_schema , fpth ):
27- return generate (
28- json .dumps (json_schema , ensure_ascii = False ),
29- input_file_type = InputFileType .JsonSchema ,
30- input_filename = "example.json" ,
31- output = fpth ,
32- output_model_type = DataModelType .PydanticV2BaseModel ,
33- capitalise_enum_members = True ,
34- field_include_all_keys = True
35- )
36-
37- def pydantic_model_from_json_schema (json_schema : dict ) -> ty .Type [BaseModel ]:
38- load = json_schema ["title" ].replace (" " , "" ) if "title" in json_schema else "Model"
39-
40- with TemporaryDirectory () as temporary_directory_name :
41- temporary_directory = pathlib .Path (temporary_directory_name )
42- file_path = "model.py"
43- module_name = file_path .split ("." )[0 ]
44- output = pathlib .Path (temporary_directory / file_path )
45-
46- pydantic_model_file_from_json_schema (json_schema , output )
47-
48- #HACK refer to https://github.com/koxudaxi/datamodel-code-generator/issues/2534 for official fix, then remove the PATCH LOGIC once that is resolved
49- # --- NEW PATCH LOGIC ---
50- if json_schema .get ("title" ) == "Project Building Area" :
51- text = output .read_text ()
52-
53- # Replace Enum → IntEnum in TargetYear only
54- text = text .replace ("class TargetYear(Enum):" , "class TargetYear(IntEnum):" )
55-
56- # Ensure IntEnum is imported
57- if "from enum import IntEnum" not in text :
58- text = text .replace ("from enum import Enum" , "from enum import Enum, IntEnum" )
59-
60- output .write_text (text )
61- # --- END PATCH LOGIC ---
62-
63- spec = importlib .util .spec_from_file_location (module_name , output )
64- module = importlib .util .module_from_spec (spec )
65- sys .modules [module_name ] = module
66- spec .loader .exec_module (module )
67- return getattr (module , load )
68-
6920def _init_model_schema (
70- schema = None , by_alias = False , generate_pydantic_model_from_json_schema = True
21+ schema = None , by_alias = False
7122) -> tuple [ty .Optional [ty .Type [BaseModel ]], dict ]:
7223 if schema is None :
7324 return None , {
@@ -76,17 +27,9 @@ def _init_model_schema(
7627 "items" : {"properties" : {}},
7728 }
7829 if isinstance (schema , dict ):
79- if generate_pydantic_model_from_json_schema :
80- schema = replace_refs (schema , merge_props = True )
81- schema = {k : v for k , v in schema .items () if k != "$defs" }
82- model = pydantic_model_from_json_schema (schema )
83- else :
84- model = None
85- schema = replace_refs (schema , merge_props = True )
86- schema = {k : v for k , v in schema .items () if k != "$defs" }
87- # IDEA: Possible implementations -@jovyan at 8/24/2022, 12:05:02 PM
88- # jsonschema_to_pydantic
89- # https://koxudaxi.github.io/datamodel-code-generator/using_as_module/
30+ model = None
31+ schema = replace_refs (schema , merge_props = True )
32+ schema = {k : v for k , v in schema .items () if k != "$defs" }
9033 else :
9134 model = schema # the "model" passed is a pydantic model
9235 schema = model .model_json_schema (by_alias = by_alias ).copy ()
0 commit comments