sumolib.xml

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2011-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    __init__.py
14# @author  Michael Behrisch
15# @author  Jakob Erdmann
16# @author  Mirko Barthauer
17# @date    2011-06-23
18
19from __future__ import print_function
20import os
21import sys
22import datetime
23
24from .. import version
25from .. import miscutils
26from .parsing import *  # noqa
27from . import xsd  # noqa
28
29
30def buildHeader(script=None, root=None, schemaPath=None, rootAttrs="", options=None, includeXMLDeclaration=False):
31    """
32    Builds an XML header with schema information and a comment on how the file has been generated
33    (script name, arguments and datetime).
34    If script name is not given, it is determined from the command line call.
35    If root is not given, no root element is printed (and thus no schema).
36    If schemaPath is not given, it is derived from the root element.
37    If rootAttrs is given as a string, it can be used to add further attributes to the root element.
38    If rootAttrs is set to None, the schema related attributes are not printed.
39    """
40    if script is None or script == "$Id$":
41        script = os.path.basename(sys.argv[0])
42    if options is None:
43        optionString = u"  options: %s\n" % (' '.join(sys.argv[1:]).replace('--', '<doubleminus>'))
44    else:
45        optionString = options.config_as_string
46    if includeXMLDeclaration:
47        header = u'<?xml version="1.0" encoding="UTF-8"?>\n\n'
48    else:
49        header = u''
50    header += u'<!-- generated on %s by Eclipse SUMO %s %s\n%s-->\n\n' % (datetime.datetime.now(), script,
51                                                                          version.gitDescribe(), optionString)
52    if root is not None:
53        if rootAttrs is None:
54            header += u'<%s>\n' % root
55        else:
56            if schemaPath is None:
57                schemaPath = root + "_file.xsd"
58            header += (u'<%s%s xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
59                       u'xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/%s">\n') % (root, rootAttrs, schemaPath)
60    return header
61
62
63def writeHeader(outf, script=None, root=None, schemaPath=None, rootAttrs="", options=None, includeXMLDeclaration=True):
64    """
65    Writes an XML header with schema information and a comment on how the file has been generated
66    (script name, arguments and datetime). Please use this as first call whenever you open a
67    SUMO related XML file for writing from your script.
68    If script name is not given, it is determined from the command line call.
69    If root is not given, no root element is printed (and thus no schema).
70    If schemaPath is not given, it is derived from the root element.
71    If rootAttrs is given as a string, it can be used to add further attributes to the root element.
72    If rootAttrs is set to None, the schema related attributes are not printed.
73    """
74    outf.write(buildHeader(script, root, schemaPath, rootAttrs, options, includeXMLDeclaration))
75
76
77def insertOptionsHeader(filename, options):
78    """
79    Inserts a comment header with the options used to call the script into an existing file.
80    """
81    header = buildHeader(options=options)
82    fpath, fbase = os.path.split(filename)
83    tmpfile = os.path.join(fpath, "tmp." + fbase)
84    with miscutils.openz(tmpfile, 'w') as tmpf:
85        with miscutils.openz(filename) as inpf:
86            for lineNbr, line in enumerate(inpf):
87                if lineNbr == 2:
88                    tmpf.write(header)
89                tmpf.write(line)
90    os.remove(filename)  # on windows, rename does not overwrite
91    os.rename(tmpfile, filename)
def buildHeader( script=None, root=None, schemaPath=None, rootAttrs='', options=None, includeXMLDeclaration=False):
31def buildHeader(script=None, root=None, schemaPath=None, rootAttrs="", options=None, includeXMLDeclaration=False):
32    """
33    Builds an XML header with schema information and a comment on how the file has been generated
34    (script name, arguments and datetime).
35    If script name is not given, it is determined from the command line call.
36    If root is not given, no root element is printed (and thus no schema).
37    If schemaPath is not given, it is derived from the root element.
38    If rootAttrs is given as a string, it can be used to add further attributes to the root element.
39    If rootAttrs is set to None, the schema related attributes are not printed.
40    """
41    if script is None or script == "$Id$":
42        script = os.path.basename(sys.argv[0])
43    if options is None:
44        optionString = u"  options: %s\n" % (' '.join(sys.argv[1:]).replace('--', '<doubleminus>'))
45    else:
46        optionString = options.config_as_string
47    if includeXMLDeclaration:
48        header = u'<?xml version="1.0" encoding="UTF-8"?>\n\n'
49    else:
50        header = u''
51    header += u'<!-- generated on %s by Eclipse SUMO %s %s\n%s-->\n\n' % (datetime.datetime.now(), script,
52                                                                          version.gitDescribe(), optionString)
53    if root is not None:
54        if rootAttrs is None:
55            header += u'<%s>\n' % root
56        else:
57            if schemaPath is None:
58                schemaPath = root + "_file.xsd"
59            header += (u'<%s%s xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
60                       u'xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/%s">\n') % (root, rootAttrs, schemaPath)
61    return header

Builds an XML header with schema information and a comment on how the file has been generated (script name, arguments and datetime). If script name is not given, it is determined from the command line call. If root is not given, no root element is printed (and thus no schema). If schemaPath is not given, it is derived from the root element. If rootAttrs is given as a string, it can be used to add further attributes to the root element. If rootAttrs is set to None, the schema related attributes are not printed.

def writeHeader( outf, script=None, root=None, schemaPath=None, rootAttrs='', options=None, includeXMLDeclaration=True):
64def writeHeader(outf, script=None, root=None, schemaPath=None, rootAttrs="", options=None, includeXMLDeclaration=True):
65    """
66    Writes an XML header with schema information and a comment on how the file has been generated
67    (script name, arguments and datetime). Please use this as first call whenever you open a
68    SUMO related XML file for writing from your script.
69    If script name is not given, it is determined from the command line call.
70    If root is not given, no root element is printed (and thus no schema).
71    If schemaPath is not given, it is derived from the root element.
72    If rootAttrs is given as a string, it can be used to add further attributes to the root element.
73    If rootAttrs is set to None, the schema related attributes are not printed.
74    """
75    outf.write(buildHeader(script, root, schemaPath, rootAttrs, options, includeXMLDeclaration))

Writes an XML header with schema information and a comment on how the file has been generated (script name, arguments and datetime). Please use this as first call whenever you open a SUMO related XML file for writing from your script. If script name is not given, it is determined from the command line call. If root is not given, no root element is printed (and thus no schema). If schemaPath is not given, it is derived from the root element. If rootAttrs is given as a string, it can be used to add further attributes to the root element. If rootAttrs is set to None, the schema related attributes are not printed.

def insertOptionsHeader(filename, options):
78def insertOptionsHeader(filename, options):
79    """
80    Inserts a comment header with the options used to call the script into an existing file.
81    """
82    header = buildHeader(options=options)
83    fpath, fbase = os.path.split(filename)
84    tmpfile = os.path.join(fpath, "tmp." + fbase)
85    with miscutils.openz(tmpfile, 'w') as tmpf:
86        with miscutils.openz(filename) as inpf:
87            for lineNbr, line in enumerate(inpf):
88                if lineNbr == 2:
89                    tmpf.write(header)
90                tmpf.write(line)
91    os.remove(filename)  # on windows, rename does not overwrite
92    os.rename(tmpfile, filename)

Inserts a comment header with the options used to call the script into an existing file.