-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgenerate.py
151 lines (114 loc) · 4.58 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
import argparse
import os
from messgen.go_generator import GoGenerator
from messgen.json_generator import JsonGenerator
from messgen.ts_generator import TsGenerator
from messgen.md_generator import MdGenerator
from messgen.parser import load_modules
from messgen.cpp_generator import CppGenerator
from messgen.data_types_preprocessor import DataTypesPreprocessor
from messgen import MessgenException
MODULE_SEP = "/"
generators = {
"cpp": CppGenerator,
"go": GoGenerator,
"json": JsonGenerator,
"ts": TsGenerator,
"md": MdGenerator
}
PLAIN_TYPES = {
"char": {"size": 1, "align": 1},
"int8": {"size": 1, "align": 1},
"uint8": {"size": 1, "align": 1},
"int16": {"size": 2, "align": 2},
"uint16": {"size": 2, "align": 2},
"int32": {"size": 4, "align": 4},
"uint32": {"size": 4, "align": 4},
"int64": {"size": 8, "align": 8},
"uint64": {"size": 8, "align": 8},
"float32": {"size": 4, "align": 4},
"float64": {"size": 8, "align": 8},
}
SPECIAL_TYPES = {
"string": {"size": 1, "align": 1}
}
def __get_free_id_list(module, free_ids_cnt):
module_msg_ids = []
for msg in module["messages"]:
module_msg_ids.append(msg["id"])
module_msg_ids.sort()
free_ids = []
last_id = 0
for id in module_msg_ids:
if id - last_id > 0:
free_ids.extend(range(last_id+1, id))
last_id = id
if len(free_ids) < free_ids_cnt:
d = free_ids_cnt - len(free_ids)
free_ids.extend(range(module_msg_ids[-1] + 1, module_msg_ids[-1] + d + 1))
else:
free_ids = free_ids[:free_ids_cnt]
return free_ids
def __dump_datatypes(modules_map, datatypes_map, free_ids_cnt=10):
dump = ""
for module_name, module_obj in modules_map.items():
free_ids = __get_free_id_list(module_obj, free_ids_cnt)
dump += ("%s free ids list: %s" % (module_name, str(free_ids))) + os.linesep
dump += os.linesep
for typename, datatype in datatypes_map.items():
dump += "****************" + os.linesep
dump += typename + os.linesep
if datatype["plain"]:
continue
for field in datatype["fields"]:
dump += "\t\t" + field["type"] + " " + field["name"] + ": "
if field["is_array"]:
if field["is_dynamic"]:
dump += "[]"
else:
dump += ("[%d]" % field["num"])
dump += os.linesep
# type_info = message["type_info"]
dump += "\t\tAlignment: " + str(datatype["align"]) + os.linesep
dump += "\t\tStatic size: " + str(datatype["static_size"]) + os.linesep
dump += "\t\tDepends:" + os.linesep
for dep in datatype["deps"]:
dump += ("\t\t\t" + dep) + os.linesep
dump += os.linesep + os.linesep
return dump
def main():
parser = argparse.ArgumentParser(description='Message generator.')
parser.add_argument("-b", "--basedirs", required=True, type=str, nargs="+",
help='Message definition base directories')
parser.add_argument("-m", "--modules", required=True, type=str, nargs="+", help='Modules')
parser.add_argument("-o", "--outdir", type=str, help='Output directory', default=".")
parser.add_argument("-l", "--lang", required=True, type=str,
help='Output language (cpp=C++, go=Golang, js=JavaScript, md=Markdown)')
parser.add_argument("-D", "--define", action='append', help="Define variables in 'key=value' format")
args = parser.parse_args()
try:
# Parse variables
variables = {}
if args.define:
for v in args.define:
p = v.split("=")
if len(p) != 2:
raise Exception("Invalid argument in -D option, must be 'key=value'")
variables[p[0]] = p[1]
modules_map = load_modules(args.basedirs, args.modules)
data_types_preprocessor = DataTypesPreprocessor(PLAIN_TYPES, SPECIAL_TYPES)
data_types_map = data_types_preprocessor.create_types_map(modules_map)
with open("dump.txt", "w+") as f:
f.write(__dump_datatypes(modules_map, data_types_map))
g_type = generators.get(args.lang)
if g_type is None:
raise MessgenException("Unsupported language \"%s\"" % args.lang)
g = g_type(modules_map, data_types_map, MODULE_SEP, variables)
g.generate(args.outdir)
print("Successfully generated to %s" % args.outdir)
except MessgenException as e:
print("ERROR: %s" % e)
exit(-1)
if __name__ == "__main__":
main()