Path: blob/main/tools/build_config/rebuildConfigDocsAndXsd.py
169674 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file rebuildConfigDocsAndXsd.py14# @author Michael Behrisch15# @date 2012-01-261617from __future__ import absolute_import18from __future__ import print_function19import os20import sys21import subprocess22from xml.sax import parseString, handler232425class ConfigReader(handler.ContentHandler):2627def __init__(self, mergeWikiTxt, out=None):28self._level = 029self._mergeWiki = mergeWikiTxt30self._intro = {}31self._end = len(mergeWikiTxt)32self._file = open(out, "w") if out else sys.stdout33active = 034currSect = ""35for idx, line in enumerate(mergeWikiTxt):36line = line.strip('\n\r')37if line == "# Options" or line == "## Options":38active = 139elif active:40if line[:3] == "###":41start = idx42currSect = line43elif line.startswith("| Option"):44self._intro[currSect] = (start, idx)45elif line[:4] == "----" or (len(line) > 2 and line[0] == "#" and line[1] != "#"):46self._end = idx47break48if currSect == "":49print(line, file=self._file)5051def startElement(self, name, attrs):52if self._level == 1:53# subtopic54title = "### " + name.replace("_", " ").title()55if title in self._intro:56begin, end = self._intro[title]57title = ("".join(self._mergeWiki[begin:end]))58else:59title += "\n"60print("%s| Option | Description |\n|%s|%s|" % (title, 8 * "-", 13 * "-"), file=self._file)61if self._level == 2:62# entry63deprecated = set(attrs.get('deprecated', '').split())64a = ""65for s in attrs.get('synonymes', '').split():66if len(s) == 1 and s not in deprecated:67a = s68print('|', end=' ', file=self._file)69if a != "":70print('**-%s** {{DT_%s}}<br>' % (a, attrs['type'].replace('[]', '_LIST')), end=' ', file=self._file)71print('**--%s** {{DT_%s}}' % (name, attrs['type'].replace('[]', '_LIST')), end=' ', file=self._file)72helpStr = attrs['help'].replace('|', ',')73if attrs['value']:74if helpStr:75helpStr += "; "76helpStr += "*default:* **%s**" % attrs['value']77print('| %s |' % helpStr, file=self._file)78self._level += 17980def endElement(self, name):81self._level -= 182if self._level == 1:83# subtopic end84print(file=self._file)8586def endDocument(self):87if self._mergeWiki[self._end:]:88print(("".join(self._mergeWiki[self._end:])).strip(), file=self._file)899091if __name__ == "__main__":92homeDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))93if len(sys.argv) == 1:94for app in ("activitygen", "dfrouter", "duarouter",95"emissionsDrivingCycle", "emissionsMap", "jtrrouter", "marouter",96"netconvert", "netedit", "netgenerate", "od2trips", "polyconvert", "sumo"):97cfg = subprocess.check_output([app, "--save-template", "stdout"], universal_newlines=True)98docs = os.path.join(homeDir, "docs", "web", "docs", app + ".md")99if os.path.exists(docs):100parseString(cfg, ConfigReader(open(docs).readlines(), docs))101schemaFile = os.path.join(homeDir, "data", "xsd", "types", app + "ConfigurationType.xsd")102subprocess.check_call([app, "--save-schema", schemaFile])103try:104diffStat = subprocess.check_output(105['git', 'diff', '--numstat', schemaFile], universal_newlines=True).split()106if diffStat[:2] == ["1", "1"]:107subprocess.check_call(['git', 'restore', schemaFile])108except subprocess.CalledProcessError:109pass110subprocess.call(['netedit', '--attribute-help-output',111os.path.join(homeDir, "docs", "web", "docs", 'Netedit', 'attribute_help.md')])112elif len(sys.argv) == 2:113app = sys.argv[1].lower()114cfg = subprocess.check_output([app, "--save-template", "stdout"])115docs = os.path.join(homeDir, "docs", "web", "docs", app + ".md")116parseString(cfg, ConfigReader(open(docs).readlines()))117elif len(sys.argv) == 3:118parseString(sys.argv[1], ConfigReader(open(sys.argv[2]).readlines()))119else:120print("Usage: %s <template> <wikisrc>\n or: %s <app>" % (121os.path.basename(__file__), os.path.basename(__file__)), file=sys.stderr)122123124