9
9
SPECIAL_FIELD_FLAG = "__"
10
10
EXTENDS_FIELD = SPECIAL_FIELD_FLAG + "extends"
11
11
12
-
12
+ def set_type (object , to_type ):
13
+ # TODO: implement type serialization
14
+ return object
13
15
def load_json_as_dict (file_name ):
14
16
with open (file_name , "r" ) as f :
15
17
return json .load (f )
16
- def special_flags_processing (json_dict , args = {}, * , base_folder = None , base_dict = {}, object_route = "" ):
17
- if SPECIAL_FIELD_FLAG + "constructor" in json_dict :
18
- json_dict .pop (SPECIAL_FIELD_FLAG + "constructor" )
19
- if SPECIAL_FIELD_FLAG + "extends" in json_dict :
20
- extends_from = json_dict [SPECIAL_FIELD_FLAG + "extends" ]["from" ].split ("." )
21
- attr_build = {}
22
- if len (extends_from ) == 1 :
23
- if extends_from [0 ] in base_dict :
24
- attr_build = special_flags_processing (base_dict [extends_from [0 ]], base_dict = base_dict , object_route = object_route + "." + extends_from [0 ])
18
+ def extends (json_dict , * , base_folder = None , base_dict = {}, object_route = "" ):
19
+ extends_from = json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "from" ].split ("." )
20
+ attr_build = {}
21
+ if len (extends_from ) == 1 :
22
+ new_route = "." .join (object_route .split ("." )+ [extends_from [0 ]])
23
+ if extends_from [0 ] in base_dict :
24
+ attr_build = special_flags_processing (base_dict [extends_from [0 ]], base_dict = base_dict , object_route = new_route )
25
+ else :
26
+ CustomLogging .error (f"Attribute { extends_from [0 ]} not found \n { base_dict } " )
27
+ else :
28
+ attr_file_name = search_json (json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "from" ], base_folder = base_folder )
29
+ if not attr_file_name :
30
+ CustomLogging .error (f"{ json_dict [SPECIAL_FIELD_FLAG + 'extends' ][SPECIAL_FIELD_FLAG + 'from' ]} path does not exists in" )
31
+ attr_json = load_json_as_dict (attr_file_name )
32
+ attr_build = json_global_compile (
33
+ attr_json ,
34
+ args = json_dict [SPECIAL_FIELD_FLAG + "extends" ],
35
+ base_folder = os .path .dirname (attr_file_name ),
36
+ object_route = json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "from" ]
37
+ )
38
+ is_excluding = SPECIAL_FIELD_FLAG + "excludes" in json_dict [SPECIAL_FIELD_FLAG + "extends" ]
39
+ is_including = SPECIAL_FIELD_FLAG + "includes" in json_dict [SPECIAL_FIELD_FLAG + "extends" ]
40
+ if is_including and is_excluding :
41
+ CustomLogging .error ("can not use excludes and includes in a same block" )
42
+ if is_including :
43
+ new_attr_build = {}
44
+ for include in json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "includes" ]:
45
+ if include not in attr_build :
46
+ CustomLogging .error (f"{ object_route } include error: attribute { include } not in { json_dict [SPECIAL_FIELD_FLAG + 'extends' ][SPECIAL_FIELD_FLAG + 'from' ]} " )
47
+ new_attr_build [include ] = attr_build [include ]
48
+ attr_build = new_attr_build
49
+ if is_excluding :
50
+ for exclude in json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "excludes" ]:
51
+ if exclude in attr_build :
52
+ attr_build .pop (exclude )
25
53
else :
26
- CustomLogging .error (f"Attribute not found { object_route } .{ extends_from [0 ]} \n { base_dict } " )
54
+ CustomLogging .warning (f"exclude error: attribute { exclude } not in { json_dict [SPECIAL_FIELD_FLAG + 'extends' ][SPECIAL_FIELD_FLAG + 'from' ]} " )
55
+ json_dict .update (attr_build )
56
+ json_dict .pop (SPECIAL_FIELD_FLAG + "extends" )
57
+ return json_dict
58
+ def cosntruct_replace (main_object , arg_replace , value ):
59
+ if type (main_object ) == str :
60
+ return main_object .replace (arg_replace , value )
61
+ response_json = {}
62
+ for attribute in main_object :
63
+ attribute_new_name = attribute .replace (arg_replace , value )
64
+ attribute_new_value = main_object [attribute ]
65
+ if type (attribute_new_value ) == dict :
66
+ attribute_new_value = cosntruct_replace (attribute_new_value , arg_replace , value )
67
+ elif type (attribute_new_value ) == list :
68
+ attribute_new_value = [ cosntruct_replace (element , arg_replace , value ) for element in attribute_new_value ]
69
+ response_json [attribute_new_name ] = attribute_new_value
70
+ return response_json
71
+ def cosntructor (json_dict , * , args = {}, object_route = "" ):
72
+ constructor_dict = json_dict .pop (SPECIAL_FIELD_FLAG + "constructor" )
73
+ response_json = copy .deepcopy (json_dict )
74
+ args_to_check = copy .deepcopy (args )
75
+ args_to_check .pop (SPECIAL_FIELD_FLAG + "from" )
76
+ if SPECIAL_FIELD_FLAG + "excludes" in args_to_check :
77
+ args_to_check .pop (SPECIAL_FIELD_FLAG + "excludes" )
78
+ if SPECIAL_FIELD_FLAG + "includes" in args_to_check :
79
+ args_to_check .pop (SPECIAL_FIELD_FLAG + "includes" )
80
+ for arg_to_check in args_to_check :
81
+ if arg_to_check not in constructor_dict :
82
+ CustomLogging .warning (f"error in constructor: invalid parameter { arg_to_check } " )
27
83
else :
28
- attr_file_name = search_json (json_dict [SPECIAL_FIELD_FLAG + "extends" ]["from" ], base_folder = base_folder )
29
- if not attr_file_name :
30
- CustomLogging .error (f"{ json_dict [SPECIAL_FIELD_FLAG + 'extends' ]['from' ]} path does not exists in" )
31
- attr_json = load_json_as_dict (attr_file_name )
32
- attr_build = json_global_compile (attr_json , base_folder = os .path .dirname (attr_file_name ),object_route = json_dict [SPECIAL_FIELD_FLAG + "extends" ]["from" ])
33
- is_excluding = SPECIAL_FIELD_FLAG + "excludes" in json_dict [SPECIAL_FIELD_FLAG + "extends" ]
34
- is_including = SPECIAL_FIELD_FLAG + "includes" in json_dict [SPECIAL_FIELD_FLAG + "extends" ]
35
- if is_including and is_excluding :
36
- CustomLogging .error ("can not use excludes and includes in a same block" )
37
- if is_including :
38
- new_attr_build = {}
39
- for include in json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "includes" ]:
40
- if include not in attr_build :
41
- CustomLogging .error (f"include error: attribute { include } not in { json_dict [SPECIAL_FIELD_FLAG + 'extends' ]['from' ]} " )
42
- new_attr_build [include ] = attr_build [include ]
43
- attr_build = new_attr_build
44
- if is_excluding :
45
- for exclude in json_dict [SPECIAL_FIELD_FLAG + "extends" ][SPECIAL_FIELD_FLAG + "excludes" ]:
46
- if exclude in attr_build :
47
- attr_build .pop (exclude )
48
- else :
49
- CustomLogging .warning (f"exclude error: attribute { exclude } not in { json_dict [SPECIAL_FIELD_FLAG + 'extends' ]['from' ]} " )
50
- json_dict .update (attr_build )
51
- json_dict .pop (SPECIAL_FIELD_FLAG + "extends" )
84
+ constructor_dict .pop (arg_to_check ) # remove argument so we know it already was defined
85
+ for default_attribute in constructor_dict :
86
+ if default_attribute .startswith (SPECIAL_FIELD_FLAG ):
87
+ continue
88
+ 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
92
+ return response_json
93
+ def special_flags_processing (json_dict , * , args = {}, base_folder = None , base_dict = {}, object_route = "" ):
94
+ if SPECIAL_FIELD_FLAG + "constructor" in json_dict :
95
+ json_dict = cosntructor (json_dict , args = args )
96
+ base_dict = copy .deepcopy (json_dict )
97
+ if SPECIAL_FIELD_FLAG + "extends" in json_dict :
98
+ json_dict = extends (json_dict , base_folder = base_folder , base_dict = base_dict , object_route = object_route )
52
99
for attribute in json_dict :
53
100
if type (json_dict [attribute ]) == dict :
54
- json_dict [attribute ] = special_flags_processing (json_dict [attribute ], args , base_folder = base_folder , base_dict = base_dict , object_route = f"{ object_route } .{ attribute } " )
101
+ json_dict [attribute ] = special_flags_processing (json_dict [attribute ], args = args , base_folder = base_folder , base_dict = base_dict , object_route = f"{ object_route } .{ attribute } " )
55
102
return copy .deepcopy (json_dict )
56
103
57
- def json_global_compile (json_dict , args = {}, * , base_folder = None , base_dict = {}, object_route = "" ):
58
- data = special_flags_processing (json_dict , args , base_folder = base_folder , base_dict = json_dict , object_route = object_route )
104
+ def json_global_compile (json_dict , * , args = {}, base_folder = None , base_dict = {}, object_route = "" ):
105
+ data = special_flags_processing (json_dict , args = args , base_folder = base_folder , base_dict = json_dict , object_route = object_route )
59
106
return data
60
107
class Compiler :
61
108
blueprint : dict = {}
@@ -68,18 +115,20 @@ def __init__(self, main_file) -> None:
68
115
def compile_models (self ):
69
116
if MODELS_FIELD not in self .blueprint and EXTENDS_FIELD not in self .blueprint :
70
117
CustomLogging .error ("models is not defined" )
71
- build = json_global_compile (self .blueprint )
118
+ build = json_global_compile (self .blueprint , base_folder = self . main_folder )
72
119
for model in build ["models" ].copy ():
73
120
model_file_name = self .main_file
74
- if type (model ) == str :
121
+ if type (build [ "models" ][ model ] ) == str :
75
122
model_file_name = search_json (
76
123
build ["models" ][model ], base_folder = self .main_folder )
77
124
if not model_file_name :
78
125
CustomLogging .error (build ["models" ][model ], "path does not exists in" )
79
126
continue
80
127
model_json = load_json_as_dict (model_file_name )
81
- elif type (model ) == dict :
82
- model_json = model
128
+ elif type (build ["models" ][model ]) == dict :
129
+ model_json = build ["models" ][model ]
130
+ else :
131
+ CustomLogging .error (f"invalid model { model } " )
83
132
model_build = json_global_compile (model_json , base_folder = os .path .dirname (model_file_name ), object_route = model )
84
133
build ["models" ][model ] = model_build
85
134
pp = pprint .PrettyPrinter (indent = 2 )
0 commit comments