Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/build_config/rebuildConfigDocsAndXsd.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file rebuildConfigDocsAndXsd.py
15
# @author Michael Behrisch
16
# @date 2012-01-26
17
18
from __future__ import absolute_import
19
from __future__ import print_function
20
import os
21
import sys
22
import subprocess
23
from xml.sax import parseString, handler
24
25
26
class ConfigReader(handler.ContentHandler):
27
28
def __init__(self, mergeWikiTxt, out=None):
29
self._level = 0
30
self._mergeWiki = mergeWikiTxt
31
self._intro = {}
32
self._end = len(mergeWikiTxt)
33
self._file = open(out, "w") if out else sys.stdout
34
active = 0
35
currSect = ""
36
for idx, line in enumerate(mergeWikiTxt):
37
line = line.strip('\n\r')
38
if line == "# Options" or line == "## Options":
39
active = 1
40
elif active:
41
if line[:3] == "###":
42
start = idx
43
currSect = line
44
elif line.startswith("| Option"):
45
self._intro[currSect] = (start, idx)
46
elif line[:4] == "----" or (len(line) > 2 and line[0] == "#" and line[1] != "#"):
47
self._end = idx
48
break
49
if currSect == "":
50
print(line, file=self._file)
51
52
def startElement(self, name, attrs):
53
if self._level == 1:
54
# subtopic
55
title = "### " + name.replace("_", " ").title()
56
if title in self._intro:
57
begin, end = self._intro[title]
58
title = ("".join(self._mergeWiki[begin:end]))
59
else:
60
title += "\n"
61
print("%s| Option | Description |\n|%s|%s|" % (title, 8 * "-", 13 * "-"), file=self._file)
62
if self._level == 2:
63
# entry
64
deprecated = set(attrs.get('deprecated', '').split())
65
a = ""
66
for s in attrs.get('synonymes', '').split():
67
if len(s) == 1 and s not in deprecated:
68
a = s
69
print('|', end=' ', file=self._file)
70
if a != "":
71
print('**-%s** {{DT_%s}}<br>' % (a, attrs['type'].replace('[]', '_LIST')), end=' ', file=self._file)
72
print('**--%s** {{DT_%s}}' % (name, attrs['type'].replace('[]', '_LIST')), end=' ', file=self._file)
73
helpStr = attrs['help'].replace('|', ',')
74
if attrs['value']:
75
if helpStr:
76
helpStr += "; "
77
helpStr += "*default:* **%s**" % attrs['value']
78
print('| %s |' % helpStr, file=self._file)
79
self._level += 1
80
81
def endElement(self, name):
82
self._level -= 1
83
if self._level == 1:
84
# subtopic end
85
print(file=self._file)
86
87
def endDocument(self):
88
if self._mergeWiki[self._end:]:
89
print(("".join(self._mergeWiki[self._end:])).strip(), file=self._file)
90
91
92
if __name__ == "__main__":
93
homeDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
94
if len(sys.argv) == 1:
95
for app in ("activitygen", "dfrouter", "duarouter",
96
"emissionsDrivingCycle", "emissionsMap", "jtrrouter", "marouter",
97
"netconvert", "netedit", "netgenerate", "od2trips", "polyconvert", "sumo"):
98
cfg = subprocess.check_output([app, "--save-template", "stdout"], universal_newlines=True)
99
docs = os.path.join(homeDir, "docs", "web", "docs", app + ".md")
100
if os.path.exists(docs):
101
parseString(cfg, ConfigReader(open(docs).readlines(), docs))
102
schemaFile = os.path.join(homeDir, "data", "xsd", "types", app + "ConfigurationType.xsd")
103
subprocess.check_call([app, "--save-schema", schemaFile])
104
try:
105
diffStat = subprocess.check_output(
106
['git', 'diff', '--numstat', schemaFile], universal_newlines=True).split()
107
if diffStat[:2] == ["1", "1"]:
108
subprocess.check_call(['git', 'restore', schemaFile])
109
except subprocess.CalledProcessError:
110
pass
111
subprocess.call(['netedit', '--attribute-help-output',
112
os.path.join(homeDir, "docs", "web", "docs", 'Netedit', 'attribute_help.md')])
113
elif len(sys.argv) == 2:
114
app = sys.argv[1].lower()
115
cfg = subprocess.check_output([app, "--save-template", "stdout"])
116
docs = os.path.join(homeDir, "docs", "web", "docs", app + ".md")
117
parseString(cfg, ConfigReader(open(docs).readlines()))
118
elif len(sys.argv) == 3:
119
parseString(sys.argv[1], ConfigReader(open(sys.argv[2]).readlines()))
120
else:
121
print("Usage: %s <template> <wikisrc>\n or: %s <app>" % (
122
os.path.basename(__file__), os.path.basename(__file__)), file=sys.stderr)
123
124