-
Notifications
You must be signed in to change notification settings - Fork 36
/
convert.py
64 lines (52 loc) · 2.03 KB
/
convert.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
import json
import os
import sys
from collections import OrderedDict
def convert_type_registry(name):
module_path = os.path.dirname(__file__)
path = os.path.join(module_path, '{}'.format(name))
with open(os.path.abspath(path), 'r') as fp:
data = fp.read()
type_registry = json.loads(data, object_pairs_hook=OrderedDict)
convert_dict = dict()
for type_string, type_struct in type_registry.items():
if type(type_struct) == OrderedDict:
if '_enum' in type_struct:
convert_dict[type_string] = {"type": "enum"}
if type(type_struct["_enum"]) == list:
convert_dict[type_string]["value_list"] = try_struct_convert(type_struct["_enum"])
else:
convert_dict[type_string]["type_mapping"] = try_struct_convert(type_struct["_enum"])
elif '_set' in type_struct:
set_element = OrderedDict(type_struct["_set"]).keys()
set_element.pop(0)
convert_dict[type_string] = {
"type": "set",
"bit_length": type_struct["_set"]["_bitLength"],
"type_mapping": set_element,
}
else:
convert_dict[type_string] = {
"type": "struct",
"type_mapping": try_struct_convert(type_struct)
}
else:
convert_dict[type_string] = type_struct
return convert_dict
def try_struct_convert(struct):
n = []
if type(struct) == list:
return struct
for type_string, type_struct in struct.items():
if type_struct is None:
type_struct = "null"
if type(type_struct) == OrderedDict:
ln = []
for k, v in type_struct.items():
ln.append([k, v])
type_struct = json.dumps(ln)
n.append([type_string, type_struct])
return n
if __name__ == '__main__':
covert = convert_type_registry(sys.argv[1])
print(json.dumps(covert, indent=2))