forked from COVESA/vss-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vspec2json.py
executable file
·127 lines (104 loc) · 3.98 KB
/
vspec2json.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
#!/usr/bin/env python3
#
# (c) 2016 Jaguar Land Rover
# (c) 2021 Robert Bosch GmbH
#
# All files and artifacts in this repository are licensed under the
# provisions of the license provided by the LICENSE file in this repository.
#
#
# Concert vspec file to JSON
#
import sys
import vspec
import json
import getopt
from model.vsstree import VSSNode, VSSType
def usage():
print(
"Usage:", sys.argv[0], "[-I include_dir] ... [-i prefix:id_file] vspec_file [-s] json_file")
print(" -s Use strict checking: Terminate when non-core attribute is found")
print(" -I include_dir Add include directory to search for included vspec")
print(" files. Can be used multiple timees.")
print()
print(" -i prefix:uuid_file File to use for storing generated UUIDs for signals with")
print(" a given path prefix. Can be used multiple times to store")
print(" UUIDs for signal sub-trees in different files.")
print()
print(" vspec_file The vehicle specification file to parse.")
print(" json_file The file to output the JSON objects to.")
sys.exit(255)
def export_node(json_dict, node):
json_dict[node.name] = {}
if node.type == VSSType.SENSOR or node.type == VSSType.ACTUATOR or node.type == VSSType.ATTRIBUTE:
json_dict[node.name]["datatype"] = str(node.data_type.value)
json_dict[node.name]["type"] = str(node.type.value)
# many optional attributes are initilized to "" in vsstree.py
if node.min != "":
json_dict[node.name]["min"] = node.min
if node.max != "":
json_dict[node.name]["max"] = node.max
if node.enum != "":
json_dict[node.name]["enum"] = node.enum
if node.default_value != "":
json_dict[node.name]["default"] = node.default_value
if node.deprecation != "":
json_dict[node.name]["deprecation"] = node.deprecation
# in case of unit or aggregate, the attribute will be missing
try:
json_dict[node.name]["unit"] = str(node.unit.value)
except AttributeError:
pass
try:
json_dict[node.name]["aggregate"] = node.aggregate
except AttributeError:
pass
json_dict[node.name]["description"] = node.description
json_dict[node.name]["uuid"] = node.uuid
# Might be better to not generate child dict, if there are no children
# if node.type == VSSType.BRANCH and len(node.children) != 0:
# json_dict[node.name]["children"]={}
# But old JSON code always generates children, so lets do so to
if node.type == VSSType.BRANCH:
json_dict[node.name]["children"] = {}
for child in node.children:
export_node(json_dict[node.name]["children"], child)
def export_json(file, root):
json_dict = {}
export_node(json_dict, root)
json.dump(json_dict, file, indent=2, sort_keys=True)
if __name__ == "__main__":
#
# Check that we have the correct arguments
#
opts, args = getopt.getopt(sys.argv[1:], "sI:i:")
strict = False
# Always search current directory for include_file
include_dirs = ["."]
for o, a in opts:
if o == "-I":
include_dirs.append(a)
elif o == "-i":
id_spec = a.split(":")
if len(id_spec) != 2:
print("ERROR: -i needs a 'prefix:id_file' argument.")
usage()
[prefix, file_name] = id_spec
vspec.db_mgr.create_signal_uuid_db(prefix, file_name)
elif o == "-s":
strict = True
else:
usage()
if len(args) != 2:
usage()
json_out = open(args[1], "w")
try:
print("Loading vspec...")
tree = vspec.load_tree(
args[0], include_dirs, exclude_private=False, break_on_noncore_attribute=strict)
print("Recursing tree and creating JSON...")
export_json(json_out, tree)
print("All done.")
except vspec.VSpecError as e:
print(f"Error: {e}")
exit(255)