Skip to content

Commit 013f8b3

Browse files
django model compiling
1 parent 8654112 commit 013f8b3

File tree

11 files changed

+137
-42
lines changed

11 files changed

+137
-42
lines changed

blueprints/examples/facebook/main.app.json

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22
"name": "facebook",
33
"models": {
44
"user":"global.utils.auth.user.models.user",
5-
"conversation":"global.communication.chat.models.conversation",
6-
"message":"global.communication.chat.feed.models.message"
5+
"conversation":{"__extends":{
6+
"__from":"global.communication.chat.models.conversation",
7+
"user":"user"
8+
}},
9+
"message":{"__extends":{
10+
"__from":"global.communication.chat.feed.models.message",
11+
"user":"user"
12+
}}
713
},
814
"services": {
9-
"user":"global.utils.auth.user.services.user",
10-
"conversation":"global.communication.chat.services.conversation",
11-
"message":"global.communication.chat.services.message"
15+
"user":{"__extends":{
16+
"__from":"global.utils.auth.user.services.user",
17+
"user":"user"
18+
}},
19+
"conversation":{"__extends":{
20+
"__from":"global.communication.chat.services.conversation",
21+
"user":"user",
22+
"conversation":"conversation"
23+
}},
24+
"message":{"__extends":{
25+
"__from":"global.communication.chat.services.message",
26+
"user":"user"
27+
}}
1228
}
1329
}

blueprints/global/communication/chat/feed/models/feed.model.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"user to be used",
5-
"type":"str",
6-
"default":"global.communication.chat.models.conversation"
4+
"description":"user model to be used",
5+
"type":"str"
76
}
87
},
98
"attributes":{
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"user to be used",
5-
"type":"str",
6-
"default":"global.communication.chat.models.conversation"
4+
"description":"user model to be used",
5+
"type":"str"
76
}
87
},
8+
"__extends":{
9+
"__from":"global.communication.chat.models.message",
10+
"user":"__user"
11+
},
912
"attributes":{
1013
"feed":{"type":"feed", "required":true, "user":"__user"}
11-
},
12-
"__extends":{
13-
"__from":"global.communication.chat.models.message"
1414
}
1515
}

blueprints/global/communication/chat/feed/models/reaction.model.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"user to be used",
5-
"type":"str",
6-
"default":"global.communication.chat.models.conversation"
4+
"description":"user model to be used",
5+
"type":"str"
76
}
87
},
98
"attributes":{

blueprints/global/communication/chat/models/conversation.model.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"conversation model to be used",
5-
"type": "str",
6-
"default":"global.communication.chat.models.conversation"
4+
"description":"user model to be used",
5+
"type":"str"
76
}
87
},
98
"attributes":{

blueprints/global/communication/chat/models/message.model.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"__constructor":{
33
"user":{
4-
"description":"route to user to be used",
5-
"type":"str",
6-
"default":"global.communication.chat.models.conversation"
4+
"description":"user model to be used",
5+
"type":"str"
76
}
87
},
98
"attributes":{

blueprints/global/communication/chat/services/conversation.service.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"__constructor":{
33
"conversation":{
44
"description":"conversation model to be used",
5-
"type": "str",
6-
"default":"global.communication.chat.models.conversation"
5+
"type": "str"
76
}
87
},
98
"endpoints":{

compiler/django_compiler/Compiler.py

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,83 @@
11
import os
2+
import json
23

3-
from json_compiler.Compiler import Compiler as json_compiler, special_flags_processing, load_json_as_dict
4+
from json_compiler.Compiler import Compiler as json_compiler, special_flags_processing, load_json_as_dict, object_route_join
45
from utils.CustomLogging import CustomLogging
56
from utils.flags import *
67

7-
def compile_model(model_name:str, model_dict:dict, *, base_folder="", base_dict={}, object_route=""):
8-
print(model_name)
9-
if not model_name == "user":
8+
DJANGO_FIELD_TYPES = {
9+
"email":{
10+
"name":"CharField",
11+
"max":"max_length"
12+
},
13+
"id":{
14+
"name":"BigAutoField",
15+
"required":{
16+
"primary_key": True
17+
}
18+
},
19+
"str":{
20+
"name":"CharField",
21+
"max":"max_length"
22+
},
23+
"password":{
24+
"name":"CharField",
25+
"max":"max_length"
26+
},
27+
"email":{
28+
"name":"CharField",
29+
"max":"max_length"
30+
},
31+
"foreign":{
32+
"name":"ForeignKey",
33+
"required":{
34+
"on_delete": "models.CASCADE"
35+
}
36+
},
37+
}
38+
39+
def compile_field(field_dict, model_names, *, base_folder="", object_route=""):
40+
#compiled = ""
41+
if field_dict["type"] == "id":
42+
#models.BigAutoField(primary_key=True)
43+
CustomLogging.log(f"skipping id since django already defines it")
1044
return
45+
field_type_name = field_dict['type']
46+
field_args = []
47+
if field_dict['type'] in model_names:
48+
field_type_name = "foreign"
49+
field_args = [field_dict['type']]
50+
field_type = DJANGO_FIELD_TYPES.get(field_type_name, False)
51+
52+
if not field_type:
53+
CustomLogging.error(f"{object_route} {field_dict['type']} type is not defined")
54+
55+
field_kwargs = {}
56+
if "max" in field_dict and "max" in field_type:
57+
field_kwargs[field_type["max"]] = field_dict["max"]
58+
field_call_definition = {
59+
"type":"function_call",
60+
"function":f"models.{field_type['name']}",
61+
"kwargs":field_kwargs,
62+
"args":field_args
63+
}
64+
#field_compiled_args = ", ".join([ f"{x}={field_args[x]}" for x in field_args])
65+
#compiled = f"models.{field_type['name']}({field_compiled_args})"
66+
return field_call_definition#compiled
67+
def compile_model(model_name:str, model_dict:dict, model_names:list, *, base_folder="", base_dict={}, object_route=""):
68+
atributes = {}
69+
for field_name in model_dict:
70+
compiled_field = compile_field(model_dict[field_name], model_names, base_folder=base_folder, object_route=object_route_join(object_route, field_name))
71+
if compiled_field:
72+
atributes[field_name] = compiled_field
1173
args = {
12-
"model_name":model_name,
13-
"atributes":{
14-
"test1":"n1",
15-
"test2":"n2"
16-
}
74+
"model_name":model_name.title(),
75+
"atributes": atributes,
76+
"desc":"__model_name model class" # recursive constructor
1777
}
1878
model_template = load_json_as_dict("./django_compiler/templates/model.code.json")
1979
processed = special_flags_processing(model_template, args=args, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
20-
print(processed)
80+
return processed
2181

2282

2383
class Compiler:
@@ -34,8 +94,10 @@ def compile_models(self, build):
3494
if MODELS_FIELD not in build:
3595
CustomLogging.error(f"{MODELS_FIELD} field is not defined")
3696
models = build[MODELS_FIELD]
97+
self.model_names = list(models.keys())
3798
for model in models.copy():
38-
compile_model(model, models[model]["attributes"], base_folder=self.main_folder, base_dict=models[model], object_route=model)
99+
compiled_model = compile_model(model, models[model]["attributes"], self.model_names, base_folder=self.main_folder, base_dict=models[model], object_route=model)
100+
models[model] = compiled_model
39101
build[MODELS_FIELD] = models
40102
return build
41103
def compile_services(self, build):
@@ -51,4 +113,6 @@ def compile(self):
51113
build = self.compile_models(self.blueprint)
52114
#build = self.compile_services(build)
53115
#print(build)
116+
with open(f"{self.blueprint['name']}.build.json", "w") as f:
117+
json.dump(build, f, indent=4)
54118
return build

compiler/django_compiler/templates/model.code.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
22
"engine": "python3.8/django3.2",
3-
"__description": "template for django models",
43
"__constructor":{
5-
"model_name":{"type":"str", "default":"DefaultModel"},
6-
"atributes":{"type":"dict"}
4+
"model_name":{"type":"str", "default":"CharField"},
5+
"atributes":{"type":"dict"},
6+
"desc":{"type":"str", "default":"template for django models"}
77
},
8+
"description": "__desc",
89
"import":{
910
"models":{"from":"django.db"}
1011
},
1112
"code": [
1213
{
1314
"type": "class",
1415
"name": "__model_name",
15-
"extends": ["models"],
16+
"extends": ["models.Model"],
1617
"attributes":"__atributes"
1718
}
1819
]

compiler/json_compiler/Compiler.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ def cosntructor(json_dict, *, args = {}, object_route=""):
9898
for default_attribute in constructor_dict:
9999
if default_attribute.startswith(SPECIAL_FIELD_FLAG):
100100
continue
101-
args_to_check[default_attribute] = set_type(constructor_dict[default_attribute]["default"], constructor_dict[default_attribute]["type"])
101+
if not constructor_dict[default_attribute].get("required", False):
102+
if "default" not in constructor_dict[default_attribute]:
103+
CustomLogging.error(f"{object_route} non required input should have default value")
104+
args_to_check[default_attribute] = set_type(constructor_dict[default_attribute]["default"], constructor_dict[default_attribute]["type"])
105+
else:
106+
if default_attribute not in args_to_check:
107+
CustomLogging.error(f"{object_route} {default_attribute} is required")
102108
updates = True
103109
while updates: # Dangerous Loop
104110
for arg in args_to_check:
@@ -109,7 +115,7 @@ def cosntructor(json_dict, *, args = {}, object_route=""):
109115
return response_json
110116
def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route=""):
111117
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
112-
json_dict = cosntructor(json_dict, args=args)
118+
json_dict = cosntructor(json_dict, args=args, object_route=object_route)
113119
base_dict = copy.deepcopy(json_dict)
114120
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
115121
json_dict = extends(json_dict, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
@@ -124,6 +130,17 @@ def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dic
124130
)
125131
return copy.deepcopy(json_dict)
126132

133+
def get_object(route, base_folder):
134+
attr_file_name=search_json(route, base_folder=base_folder)
135+
if not attr_file_name:
136+
CustomLogging.error(f"{route} path does not exists")
137+
attr_json = load_json_as_dict(attr_file_name)
138+
attr_build = json_global_compile(
139+
attr_json,
140+
base_folder = os.path.dirname(attr_file_name),
141+
object_route=route
142+
)
143+
return attr_build
127144
def json_global_compile(json_dict, *, args = {}, base_folder=None, base_dict={}, object_route= ""):
128145
data = special_flags_processing(json_dict, args=args, base_folder=base_folder, base_dict=json_dict, object_route=object_route)
129146
return data

compiler/utils/CustomLogging.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class CustomLogging(object):
2+
def log(msg:str):
3+
print("LOG:",msg)
24
def error(msg:str):
35
raise NameError(msg)
46
def warning(msg:str):

0 commit comments

Comments
 (0)