forked from COVESA/vss-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvspec2franca.py
executable file
·132 lines (97 loc) · 3.61 KB
/
vspec2franca.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
#!/usr/bin/env python3
#
# (C) 2016 Jaguar Land Rover
#
# All files and artifacts in this repository are licensed under the
# provisions of the license provided by the LICENSE file in this repository.
#
#
# Convert vspec file to FrancaIDL spec.
#
import sys
import vspec
import argparse
def traverse_tree(tree, outf, prefix_arr, is_first_element):
# Convert a prefix array of path elements to a string
def prefix_to_string(prefix_arr):
if len(prefix_arr) == 0:
return ""
res = prefix_arr[0]
for elem in prefix_arr[1:]:
res = "{}.{}".format(res, elem)
return res
# Traverse all elemnts in tree.
for key, val in tree.items():
# Is this a branch?
if "children" in val:
# Yes. Recurse
traverse_tree(val['children'], outf, prefix_arr + [ key ], is_first_element)
continue
# Drop a comma in before the next element.
if not is_first_element:
outf.write(",\n")
outf.write("""{{
name: "{}"
type: "{}"
description: "{}"
""".format(prefix_to_string(prefix_arr + [ key ]),
val['type'],
val['description']))
if "datatype" in val:
outf.write(" datatype: {}\n".format(val["datatype"]))
if "uuid" in val:
outf.write(" uuid: {}\n".format(val["uuid"]))
if "min" in val:
outf.write(" min: {}\n".format(val["min"]))
if "max" in val:
outf.write(" max: {}\n".format(val["max"]))
if "unit" in val:
outf.write(" unit: {}\n".format(val["unit"]))
if "allowed" in val:
outf.write(" allowed: {}\n".format(val["allowed"]))
if "sensor" in val:
outf.write(" sensor: {}\n".format(val["sensor"]))
if "actuator" in val:
outf.write(" actuator: {}\n".format(val["actuator"]))
outf.write("}\n")
is_first_element = False
if __name__ == "__main__":
# The arguments we accept
parser = argparse.ArgumentParser(description='Convert vspec to franca.')
parser.add_argument('-I', '--include-dir', action='append', metavar='dir', type=str, default=[],
help='Add include directory to search for included vspec files.')
parser.add_argument('-s', '--strict', action='store_true', help='Use strict checking: Terminate when non-core attribute is found.' )
parser.add_argument('-v', '--vssversion', type=str, default="unspecified version", help='VSS version' )
parser.add_argument('vspec_file', metavar='<vspec_file>', help='The vehicle specification file to convert.')
parser.add_argument('franca_file', metavar='<franca file>', help='The file to output the Franca IDL spec to.')
args = parser.parse_args()
strict=args.strict
include_dirs = ["."]
include_dirs.extend(args.include_dir)
franca_out = open(args.franca_file, "w")
try:
tree = vspec.load(args.vspec_file, include_dirs)
except vspec.VSpecError as e:
print("Error: {}".format(e))
sys.exit(255)
franca_out.write(
"""// Copyright (C) 2016, GENIVI Alliance
//
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
const UTF8String VSS_VERSION = "{}"
struct SignalSpec {{
UInt32 id
String name
String type
String description
}}
const SignalSpec[] signal_spec = [
""".format(args.vssversion))
traverse_tree(tree, franca_out, [], True)
franca_out.write(
"""
]
""")
franca_out.close()