Skip to content

Commit 628b16d

Browse files
generalize flag constants
1 parent bcf23ea commit 628b16d

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# App architect
22

3+
```sh
4+
python .\compiler\compiler.py c "django.json.v1" ./blueprints/examples/facebook/main.app.json ./compiler/build
5+
```
36
Design your application in a json like structure, this way the resulting project is going to be easily understood both by human and by machine.
47

58
This json like structure will let you:

blueprints/examples/calculator/v2/services/processing/operations/sum.code.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
"__constructor":{
55
"number_a":{"type":"str", "default":"number_a"},
66
"number_b":{"type":"str", "default":"number_b"},
7-
"message":{"type":"str", "default":"adding {__number_a} to {__number_b}"}
7+
"message":{"type":"str", "default":"adding {__number_a} to {__number_b}"},
8+
"function_name":{"type":"str", "default":"sum"}
89
},
10+
"type": "function",
11+
"name": "__function_name",
12+
"args": ["f\"__message\""],
913
"inputs":{
1014
"__number_a":{"type":"int", "required":true},
1115
"__number_b":{"type":"int", "required":true}
@@ -14,16 +18,11 @@
1418
"result":{"type":"int", "required":true}
1519
},
1620
"code": [
17-
{
18-
"type": "function",
19-
"name": "print",
20-
"args": ["f\"__message\""]
21-
},
2221
{
2322
"type": "variable",
2423
"variable_type": "str",
2524
"name": "result",
26-
"equals": ["__number_a+number_b"]
25+
"equals": "__number_a+number_b"
2726
},
2827
{
2928
"type": "return",

compiler/json_compiler/Compiler.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def load_json_as_dict(file_name):
1717
with open(file_name, "r") as f:
1818
return json.load(f)
1919
def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
20-
extends_from = json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"].split(".")
20+
extends_from = json_dict[FLAG_EXTENDS][FLAG_FROM].split(".")
2121
attr_build = {}
2222
if len(extends_from) == 1:
2323
new_route = object_route_join(object_route,extends_from[0])
@@ -26,35 +26,35 @@ def extends(json_dict, *, base_folder=None, base_dict={}, object_route=""):
2626
else:
2727
CustomLogging.error(f"Attribute {extends_from[0]} not found extending {object_route}\n{base_dict}")
2828
else:
29-
attr_file_name = search_json(json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"], base_folder=base_folder)
29+
attr_file_name = search_json(json_dict[FLAG_EXTENDS][FLAG_FROM], base_folder=base_folder)
3030
if not attr_file_name:
31-
CustomLogging.error(f"{json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']} path does not exists in")
31+
CustomLogging.error(f"{json_dict[FLAG_EXTENDS][FLAG_FROM]} path does not exists in")
3232
attr_json = load_json_as_dict(attr_file_name)
3333
attr_build = json_global_compile(
3434
attr_json,
35-
args = json_dict[SPECIAL_FIELD_FLAG+"extends"],
35+
args = json_dict[FLAG_EXTENDS],
3636
base_folder = os.path.dirname(attr_file_name),
37-
object_route=json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"from"]
37+
object_route=json_dict[FLAG_EXTENDS][FLAG_FROM]
3838
)
39-
is_excluding = SPECIAL_FIELD_FLAG+"excludes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
40-
is_including = SPECIAL_FIELD_FLAG+"includes" in json_dict[SPECIAL_FIELD_FLAG+"extends"]
39+
is_excluding = FLAG_EXCLUDES in json_dict[FLAG_EXTENDS]
40+
is_including = FLAG_INCLUDES in json_dict[FLAG_EXTENDS]
4141
if is_including and is_excluding:
4242
CustomLogging.error("can not use excludes and includes in a same block")
4343
if is_including:
4444
new_attr_build = {}
45-
for include in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"includes"]:
45+
for include in json_dict[FLAG_EXTENDS][FLAG_INCLUDES]:
4646
if include not in attr_build:
4747
CustomLogging.error(f"{object_route} include error: attribute {include} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
4848
new_attr_build[include] = attr_build[include]
4949
attr_build = new_attr_build
5050
if is_excluding:
51-
for exclude in json_dict[SPECIAL_FIELD_FLAG+"extends"][SPECIAL_FIELD_FLAG+"excludes"]:
51+
for exclude in json_dict[FLAG_EXTENDS][FLAG_EXCLUDES]:
5252
if exclude in attr_build:
5353
attr_build.pop(exclude)
5454
else:
5555
CustomLogging.warning(f"exclude error {object_route}: attribute {exclude} not in {json_dict[SPECIAL_FIELD_FLAG+'extends'][SPECIAL_FIELD_FLAG+'from']}")
5656
json_dict.update(attr_build)
57-
json_dict.pop(SPECIAL_FIELD_FLAG+"extends")
57+
json_dict.pop(FLAG_EXTENDS)
5858
return json_dict
5959
def cosntruct_replace(main_object, arg_replace, value , *, object_route=""):
6060
if type(main_object) == str:
@@ -85,12 +85,12 @@ def cosntructor(json_dict, *, args = {}, object_route=""):
8585
constructor_dict = json_dict.pop(SPECIAL_FIELD_FLAG+"constructor")
8686
response_json = copy.deepcopy(json_dict)
8787
args_to_check = copy.deepcopy(args)
88-
if SPECIAL_FIELD_FLAG+"from" in args_to_check:
89-
args_to_check.pop(SPECIAL_FIELD_FLAG+"from")
90-
if SPECIAL_FIELD_FLAG+"excludes" in args_to_check:
91-
args_to_check.pop(SPECIAL_FIELD_FLAG+"excludes")
92-
if SPECIAL_FIELD_FLAG+"includes" in args_to_check:
93-
args_to_check.pop(SPECIAL_FIELD_FLAG+"includes")
88+
if FLAG_FROM in args_to_check:
89+
args_to_check.pop(FLAG_FROM)
90+
if FLAG_EXCLUDES in args_to_check:
91+
args_to_check.pop(FLAG_EXCLUDES)
92+
if FLAG_INCLUDES in args_to_check:
93+
args_to_check.pop(FLAG_INCLUDES)
9494
for arg_to_check in args_to_check:
9595
if arg_to_check not in constructor_dict:
9696
CustomLogging.warning(f"error in constructor: invalid parameter {arg_to_check}")
@@ -118,7 +118,7 @@ def special_flags_processing(json_dict, *, args = {}, base_folder=None, base_dic
118118
if SPECIAL_FIELD_FLAG+"constructor" in json_dict:
119119
json_dict = cosntructor(json_dict, args=args, object_route=object_route)
120120
base_dict = copy.deepcopy(json_dict)
121-
if SPECIAL_FIELD_FLAG+"extends" in json_dict:
121+
if FLAG_EXTENDS in json_dict:
122122
json_dict = extends(json_dict, base_folder=base_folder, base_dict=base_dict, object_route=object_route)
123123
for attribute in json_dict:
124124
if type(json_dict[attribute]) == dict:

compiler/python_compiler/Compiler.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
class Compiler:
88
blueprint: dict = {}
99

10-
def __init__(self, main_file) -> None:
11-
self.main_folder = os.path.dirname(main_file)
12-
self.main_file = main_file
13-
self.blueprint = load_json_as_dict(main_file)
10+
def __init__(self, *, main_file:str = "", blueprint:dict = {}) -> None:
11+
if blueprint:
12+
self.blueprint = blueprint
13+
if main_file:
14+
if not blueprint:
15+
self.blueprint = load_json_as_dict(main_file)
16+
self.main_folder = os.path.dirname(main_file)
17+
self.main_file = main_file
18+
1419
def compile(self):
1520
return

compiler/utils/flags.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
MODELS_FIELD = "models"
22
SEVICES_FIELD = "services"
33
SPECIAL_FIELD_FLAG = "__"
4-
EXTENDS_FIELD = SPECIAL_FIELD_FLAG+"extends"
4+
def is_flag(text:str):
5+
return text.startswith(SPECIAL_FIELD_FLAG)
6+
def to_flag(text:str):
7+
return SPECIAL_FIELD_FLAG+text
8+
9+
FLAG_EXTENDS = to_flag("extends")
10+
FLAG_EXCLUDES = to_flag("excludes")
11+
FLAG_INCLUDES = to_flag("includes")
12+
FLAG_FROM = to_flag("from")

0 commit comments

Comments
 (0)