6
6
from utils .CustomLogging import CustomLogging
7
7
8
8
MODELS_FIELD = "models"
9
+ SEVICES_FIELD = "services"
9
10
SPECIAL_FIELD_FLAG = "__"
10
11
EXTENDS_FIELD = SPECIAL_FIELD_FLAG + "extends"
11
12
@@ -23,7 +24,7 @@ def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
23
24
if extends_from [0 ] in base_dict :
24
25
attr_build = special_flags_processing (base_dict [extends_from [0 ]], base_dict = base_dict , object_route = new_route )
25
26
else :
26
- CustomLogging .error (f"Attribute { extends_from [0 ]} not found \n { base_dict } " )
27
+ CustomLogging .error (f"Attribute { extends_from [0 ]} not found extending { object_route } \n { base_dict } " )
27
28
else :
28
29
attr_file_name = search_json (json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "from" ], base_folder = base_folder )
29
30
if not attr_file_name :
@@ -86,9 +87,13 @@ def cosntructor(json_dict, *, args = {}, object_route=""):
86
87
if default_attribute .startswith (SPECIAL_FIELD_FLAG ):
87
88
continue
88
89
args_to_check [default_attribute ] = set_type (constructor_dict [default_attribute ]["default" ], constructor_dict [default_attribute ]["type" ])
89
- for arg in args_to_check :
90
- new_value = cosntruct_replace (response_json , SPECIAL_FIELD_FLAG + arg , args_to_check [arg ])
91
- response_json = new_value
90
+ updates = True
91
+ while updates : # Dangerous Loop
92
+ for arg in args_to_check :
93
+ new_value = cosntruct_replace (response_json , SPECIAL_FIELD_FLAG + arg , args_to_check [arg ])
94
+ if new_value == response_json :
95
+ updates = False
96
+ response_json = new_value
92
97
return response_json
93
98
def special_flags_processing (json_dict , * , args = {}, base_folder = None , base_dict = {}, object_route = "" ):
94
99
if SPECIAL_FIELD_FLAG + "constructor" in json_dict :
@@ -112,27 +117,53 @@ def __init__(self, main_file) -> None:
112
117
self .main_file = main_file
113
118
self .blueprint = load_json_as_dict (main_file )
114
119
115
- def compile_models (self ):
116
- if MODELS_FIELD not in self . blueprint and EXTENDS_FIELD not in self . blueprint :
117
- CustomLogging .error ("models is not defined" )
118
- build = json_global_compile ( self . blueprint , base_folder = self . main_folder )
119
- for model in build [ " models" ] .copy ():
120
+ def compile_models (self , build ):
121
+ if MODELS_FIELD not in build :
122
+ CustomLogging .error (f" { MODELS_FIELD } field is not defined" )
123
+ models = build [ MODELS_FIELD ]
124
+ for model in models .copy ():
120
125
model_file_name = self .main_file
121
- if type (build [ " models" ] [model ]) == str :
126
+ if type (models [model ]) == str :
122
127
model_file_name = search_json (
123
- build [ " models" ] [model ], base_folder = self .main_folder )
128
+ models [model ], base_folder = self .main_folder )
124
129
if not model_file_name :
125
- CustomLogging .error (build [ " models" ] [model ], "path does not exists in" )
130
+ CustomLogging .error (models [model ], "path does not exists in" )
126
131
continue
127
132
model_json = load_json_as_dict (model_file_name )
128
- elif type (build [ " models" ] [model ]) == dict :
129
- model_json = build [ " models" ] [model ]
133
+ elif type (models [model ]) == dict :
134
+ model_json = models [model ]
130
135
else :
131
136
CustomLogging .error (f"invalid model { model } " )
132
137
model_build = json_global_compile (model_json , base_folder = os .path .dirname (model_file_name ), object_route = model )
133
- build ["models" ][model ] = model_build
134
- pp = pprint .PrettyPrinter (indent = 2 )
135
- pp .pprint (build )
138
+ models [model ] = model_build
139
+ build [MODELS_FIELD ] = models
140
+ return build
141
+ def compile_services (self , build ):
142
+ if SEVICES_FIELD not in build :
143
+ CustomLogging .error (f"{ SEVICES_FIELD } field is not defined" )
144
+ services = build [SEVICES_FIELD ]
145
+ for service in services .copy ():
146
+ service_file_name = self .main_file
147
+ if type (services [service ]) == str :
148
+ service_file_name = search_json (
149
+ services [service ], base_folder = self .main_folder )
150
+ if not service_file_name :
151
+ CustomLogging .error (services [service ], "path does not exists in" )
152
+ continue
153
+ service_json = load_json_as_dict (service_file_name )
154
+ elif type (services [service ]) == dict :
155
+ service_json = services [service ]
156
+ else :
157
+ CustomLogging .error (f"invalid service { service } " )
158
+ service_build = json_global_compile (service_json , base_folder = os .path .dirname (service_file_name ), object_route = service )
159
+ services [service ] = service_build
160
+ build [SEVICES_FIELD ] = services
161
+ return build
136
162
137
163
def compile (self ):
138
- self .compile_models ()
164
+ build = json_global_compile (self .blueprint , base_folder = self .main_folder )
165
+ build = self .compile_models (build )
166
+ build = self .compile_services (build )
167
+ #print(build)
168
+ pp = pprint .PrettyPrinter (indent = 2 )
169
+ pp .pprint (build )
0 commit comments