forked from carla-simulator/sumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuildConstants.py
executable file
·142 lines (125 loc) · 5.62 KB
/
rebuildConstants.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2009-2024 German Aerospace Center (DLR) and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file rebuildConstants.py
# @author Daniel Krajzewicz
# @author Michael Behrisch
# @date 2009-07-24
"""
This script extracts definitions from <SUMO>/src/libsumo/TraCIConstants.h
and builds the according constants definition python and java files
"constants.py" and Constants.java.
When called without options the script generates both files in the default
locations. To generate just python give an output file name, for Java do:
tools/traci/rebuildConstants.py -j de.tudresden.sumo.config.Constants
-o tools/contributed/traas/src/main/java/de/tudresden/sumo/config/Constants.java
"""
from __future__ import print_function
from __future__ import absolute_import
import os
import datetime
import argparse
import re
def translateFile(filePath, fdo, java, start, item, end, item_parser):
comment_marker = "//" if java else "#"
with open(filePath) as fdi:
started = False
for line in fdi:
if started:
line = line.strip()
if line.find(end) >= 0:
started = False
continue
if java:
line = line.replace("//", " //")
else:
line = line.replace("///", "#").lstrip(" ")
line = line.replace("//", "# ").lstrip(" ")
if line.find(item) >= 0 and comment_marker not in line:
(ctype, cname, cvalue) = item_parser(line)
if java:
line = " public static final {} {} = {};".format(ctype, cname, cvalue)
else:
line = "{} = {}".format(cname, cvalue)
print(line, file=fdo)
if line.find(start) >= 0:
started = True
def parseTraciConstant(line):
match = re.search(r'(\S+) ([A-Z0-9_]+) = (\S+);', line)
if match:
return match.group(1, 2, 3)
else:
return None
def parseLaneChangeAction(line):
line = line.rstrip(',')
match = re.search('([A-Z0-9_]+) = (.+)', line)
if match:
return ('int', match.group(1), match.group(2))
else:
return None
dirname = os.path.dirname(__file__)
argParser = argparse.ArgumentParser()
argParser.add_argument("-j", "--java",
help="generate Java output as static members of the given class", metavar="CLASS")
argParser.add_argument("-o", "--output",
help="File to save constants into", metavar="FILE")
options = argParser.parse_args()
header = """# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2009-2024 German Aerospace Center (DLR) and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file <file>
# @author generated by "%s"
# @date %s
\"\"\"
This script contains TraCI constant definitions from <SUMO_HOME>/src/libsumo/TraCIConstants.h.\
""" % (os.path.basename(__file__), datetime.datetime.now())
if options.output is None:
outputs = [(os.path.join(dirname, "constants.py"), None),
(dirname + "/../contributed/traas/src/main/java/de/tudresden/sumo/config/Constants.java",
options.java if options.java else "de.tudresden.sumo.config.Constants")]
else:
outputs = [(options.output, options.java)]
for out, className in outputs:
fdo = open(out, "w")
h = header.replace("<file>", os.path.basename(out))
if className:
sep = "/%s/" % (76 * "*")
print(sep, file=fdo)
h = h.replace("\n# @file", sep + "\n/// @file").replace("# @", "/// @")
print(h.replace("#", "//").replace('\n"""\n', "///\n// "), file=fdo)
print(sep, file=fdo)
if "." in className:
package, className = className.rsplit(".", 1)
fdo.write("package %s;\n" % package)
fdo.write("public class %s {" % className)
else:
print(h, file=fdo)
print('"""\n', file=fdo)
srcDir = os.path.join(dirname, "..", "..", "src")
translateFile(os.path.join(srcDir, "libsumo", "TraCIConstants.h"),
fdo, className is not None,
"namespace libsumo {", "TRACI_CONST ", "} // namespace libsumo", parseTraciConstant)
translateFile(os.path.join(srcDir, "utils", "xml", "SUMOXMLDefinitions.h"),
fdo, className is not None,
"enum LaneChangeAction {", "LCA_", "};", parseLaneChangeAction)
if className:
fdo.write("}\n")
fdo.close()