sumolib.xml.parsing
index
/home/delphi/gcc/sumo/tools/sumolib/xml/parsing.py

# -*- coding: utf-8 -*-
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2011-2025 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

 
Modules
       
xml.etree.cElementTree
gzip
io
sumolib.miscutils
re
xml.sax.saxutils
sys
xml
sumolib.xml.xsd

 
Classes
       
xml.sax.handler.ContentHandler(builtins.object)
NestingHandler
AttrFinder

 
class AttrFinder(NestingHandler)
    AttrFinder(xsdFile, source, split, keepAttrs=None)
 

 
 
Method resolution order:
AttrFinder
NestingHandler
xml.sax.handler.ContentHandler
builtins.object

Methods defined here:
__init__(self, xsdFile, source, split, keepAttrs=None)
Initialize self.  See help(type(self)) for accurate signature.
addElement(self, root, name, depth)
recursiveAttrFind(self, root, currEle, depth)
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
 
The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

Methods inherited from NestingHandler:
depth(self)
endElement(self, name)
Signals the end of an element in non-namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElement event.

Methods inherited from xml.sax.handler.ContentHandler:
characters(self, content)
Receive notification of character data.
 
The Parser will call this method to report each chunk of
character data. SAX parsers may return all contiguous
character data in a single chunk, or they may split it into
several chunks; however, all of the characters in any single
event must come from the same external entity so that the
Locator provides useful information.
endDocument(self)
Receive notification of the end of a document.
 
The SAX parser will invoke this method only once, and it will
be the last method invoked during the parse. The parser shall
not invoke this method until it has either abandoned parsing
(because of an unrecoverable error) or reached the end of
input.
endElementNS(self, name, qname)
Signals the end of an element in namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElementNS event.
endPrefixMapping(self, prefix)
End the scope of a prefix-URI mapping.
 
See startPrefixMapping for details. This event will always
occur after the corresponding endElement event, but the order
of endPrefixMapping events is not otherwise guaranteed.
ignorableWhitespace(self, whitespace)
Receive notification of ignorable whitespace in element content.
 
Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.
 
SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.
processingInstruction(self, target, data)
Receive notification of a processing instruction.
 
The Parser will invoke this method once for each processing
instruction found: note that processing instructions may occur
before or after the main document element.
 
A SAX parser should never report an XML declaration (XML 1.0,
section 2.8) or a text declaration (XML 1.0, section 4.3.1)
using this method.
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for
locating the origin of document events.
 
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.
 
The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.
 
Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.
skippedEntity(self, name)
Receive notification of a skipped entity.
 
The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.
startDocument(self)
Receive notification of the beginning of a document.
 
The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).
startElementNS(self, name, qname, attrs)
Signals the start of an element in namespace mode.
 
The name parameter contains the name of the element type as a
(uri, localname) tuple, the qname parameter the raw XML 1.0
name used in the source document, and the attrs parameter
holds an instance of the Attributes class containing the
attributes of the element.
 
The uri part of the name tuple is None for elements which have
no namespace.
startPrefixMapping(self, prefix, uri)
Begin the scope of a prefix-URI Namespace mapping.
 
The information from this event is not necessary for normal
Namespace processing: the SAX XML reader will automatically
replace prefixes for element and attribute names when the
http://xml.org/sax/features/namespaces feature is true (the
default).
 
There are cases, however, when applications need to use
prefixes in character data or in attribute values, where they
cannot safely be expanded automatically; the
start/endPrefixMapping event supplies the information to the
application to expand prefixes in those contexts itself, if
necessary.
 
Note that start/endPrefixMapping events are not guaranteed to
be properly nested relative to each-other: all
startPrefixMapping events will occur before the corresponding
startElement event, and all endPrefixMapping events will occur
after the corresponding endElement event, but their order is
not guaranteed.

Data descriptors inherited from xml.sax.handler.ContentHandler:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class NestingHandler(xml.sax.handler.ContentHandler)
    A handler which knows the current nesting of tags
 
 
Method resolution order:
NestingHandler
xml.sax.handler.ContentHandler
builtins.object

Methods defined here:
__init__(self)
Initialize self.  See help(type(self)) for accurate signature.
depth(self)
endElement(self, name)
Signals the end of an element in non-namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElement event.
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
 
The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

Methods inherited from xml.sax.handler.ContentHandler:
characters(self, content)
Receive notification of character data.
 
The Parser will call this method to report each chunk of
character data. SAX parsers may return all contiguous
character data in a single chunk, or they may split it into
several chunks; however, all of the characters in any single
event must come from the same external entity so that the
Locator provides useful information.
endDocument(self)
Receive notification of the end of a document.
 
The SAX parser will invoke this method only once, and it will
be the last method invoked during the parse. The parser shall
not invoke this method until it has either abandoned parsing
(because of an unrecoverable error) or reached the end of
input.
endElementNS(self, name, qname)
Signals the end of an element in namespace mode.
 
The name parameter contains the name of the element type, just
as with the startElementNS event.
endPrefixMapping(self, prefix)
End the scope of a prefix-URI mapping.
 
See startPrefixMapping for details. This event will always
occur after the corresponding endElement event, but the order
of endPrefixMapping events is not otherwise guaranteed.
ignorableWhitespace(self, whitespace)
Receive notification of ignorable whitespace in element content.
 
Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.
 
SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.
processingInstruction(self, target, data)
Receive notification of a processing instruction.
 
The Parser will invoke this method once for each processing
instruction found: note that processing instructions may occur
before or after the main document element.
 
A SAX parser should never report an XML declaration (XML 1.0,
section 2.8) or a text declaration (XML 1.0, section 4.3.1)
using this method.
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for
locating the origin of document events.
 
SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.
 
The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.
 
Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.
skippedEntity(self, name)
Receive notification of a skipped entity.
 
The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.
startDocument(self)
Receive notification of the beginning of a document.
 
The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).
startElementNS(self, name, qname, attrs)
Signals the start of an element in namespace mode.
 
The name parameter contains the name of the element type as a
(uri, localname) tuple, the qname parameter the raw XML 1.0
name used in the source document, and the attrs parameter
holds an instance of the Attributes class containing the
attributes of the element.
 
The uri part of the name tuple is None for elements which have
no namespace.
startPrefixMapping(self, prefix, uri)
Begin the scope of a prefix-URI Namespace mapping.
 
The information from this event is not necessary for normal
Namespace processing: the SAX XML reader will automatically
replace prefixes for element and attribute names when the
http://xml.org/sax/features/namespaces feature is true (the
default).
 
There are cases, however, when applications need to use
prefixes in character data or in attribute values, where they
cannot safely be expanded automatically; the
start/endPrefixMapping event supplies the information to the
application to expand prefixes in those contexts itself, if
necessary.
 
Note that start/endPrefixMapping events are not guaranteed to
be properly nested relative to each-other: all
startPrefixMapping events will occur before the corresponding
startElement event, and all endPrefixMapping events will occur
after the corresponding endElement event, but their order is
not guaranteed.

Data descriptors inherited from xml.sax.handler.ContentHandler:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
Functions
       
average(elements, attrname)
compound_object(element_name, attrnames, warn=False, sort=False)
return a class which delegates bracket access to an internal dict.
Missing attributes are delegated to the child dict for convenience.
@note: Care must be taken when child nodes and attributes have the same names
contextualRename(xmlTree, prefixes, attribute='id', ids=None)
Renames the given attribute in a specified set of child elements within
xmlTree and also replaces all attribute values that referred to such an id
with the new value.
Example:
Given an opendrive file, when called with prefixes={'road': 'r', 'junction': 'j'}
  - all road ids will be renamed to rN where N is a running integer
  - all junction ids will be renamed to jN where N is also a running integer
  - all attributes that refered to roads or junctions will now refer to
    their new ids
create_document(root_element_name, attrs=None, schema=None)
iskeyword = __contains__(...) method of builtins.frozenset instance
x.__contains__(y) <==> y in x.
parse(xmlfile, element_names=None, element_attrs=None, attr_conversions=None, heterogeneous=True, warn=False, ignoreXmlns=False, outputLevel=1)
Parses the given element_names from xmlfile and yield compound objects for
their xml subtrees (no extra objects are returned if element_names appear in
the subtree) The compound objects provide all element attributes of
the root of the subtree as attributes unless attr_names are supplied. In this
case attr_names maps element names to a list of attributes which are
supplied. If attr_conversions is not empty it must map attribute names to
callables which will be called upon the attribute value before storing under
the attribute name.
The compound objects gives dictionary style access to list of compound
objects o for any children with the given element name
o['child_element_name'] = [osub0, osub1, ...]
As a shorthand, attribute style access to the list of child elements is
provided unless an attribute with the same name as the child elements
exists (i.e. o.child_element_name = [osub0, osub1, ...])
@Note: All elements with the same name must have the same type regardless of
the subtree in which they occur (heterogeneous cases may be handled by
setting heterogeneous=True (with reduced parsing speed)
@Note: Attribute names may be modified to avoid name clashes
with python keywords. (set warn=True to receive renaming warnings)
@Note: The element_names may be either a single string or a list of strings.
@Example: parse('plain.edg.xml', ['edge'])
parse_fast(xmlfile, element_name, attrnames, warn=False, optional=False, encoding='utf8', line_filter=None)
Parses the given attrnames from all elements with element_name
@Note: The element must be on its own line and the attributes must appear in
the given order. If you set "optional=True", missing attributes will be set to None.
Make sure that you list all (potential) attributes (even the ones you are not interested in)
in this case. You can only leave out attributes at the end.
@Example: parse_fast('plain.edg.xml', 'edge', ['id', 'speed'])
parse_fast_nested(xmlfile, element_name, attrnames, element_name2, attrnames2, warn=False, optional=False, encoding='utf8')
Parses the given attrnames from all elements with element_name
And attrnames2 from element_name2 where element_name2 is a child element of element_name
If you set "optional=True", missing attributes will be set to None.
Make sure that you list all (potential) attributes (even the ones you are not interested in)
in this case. You can only leave out attributes at the end.
@Note: The element must be on its own line and the attributes must appear in
the given order.
@Example: parse_fast_nested('fcd.xml', 'timestep', ['time'], 'vehicle', ['id', 'speed', 'lane']):
parse_fast_structured(xmlfile, element_name, attrnames, nested, warn=False, optional=False, encoding='utf8')
Parses the given attrnames from all elements with element_name and nested elements of level 1.
Unlike parse_fast_nested this function can handle multiple different child elements and
returns objects where the child elements can be accessed by name (e.g. timestep.vehicle[0])
as with the parse method. The returned object is not modifiable though.
If you set "optional=True", missing attributes will be set to None.
Make sure that you list all (potential) attributes (even the ones you are not interested in)
in this case. You can only leave out attributes at the end.
@Note: Every element must be on its own line and the attributes must appear in the given order.
@Example: parse_fast_structured('fcd.xml', 'timestep', ['time'],
                                {'vehicle': ['id', 'speed', 'lane'], 'person': ['id', 'speed', 'edge']}):
parselines(xmlline, element_name, element_attrs=None, attr_conversions=None, heterogeneous=True, warn=False, addRoot='dummy')
quoteattr(val, ensureUnicode=False)
reduce(...)
reduce(function, iterable[, initial]) -> value
 
Apply a function of two arguments cumulatively to the items of a sequence
or iterable, from left to right, so as to reduce the iterable to a single
value.  For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the iterable in the calculation, and serves as a default when the
iterable is empty.
sum(elements, attrname)
supports_comments()
xmlescape(value)

 
Data
        DEFAULT_ATTR_CONVERSIONS = {'angle': <class 'float'>, 'contPos': <class 'float'>, 'endOffset': <class 'float'>, 'endPos': <class 'float'>, 'freq': <class 'float'>, 'fromLane': <class 'int'>, 'index': <class 'int'>, 'lat': <class 'float'>, 'length': <class 'float'>, 'linkIndex': <class 'int'>, ...}