Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse(self, file, encoding=None, version = None):
f = custom_open('test_files/%s' % file, encoding=encoding)
parser = mod_parser.GPXParser(f)
gpx = parser.parse(version)
f.close()
if not gpx:
print('Parser error: %s' % parser.get_error())
return gpx
def reparse(self, gpx):
xml = gpx.to_xml()
parser = mod_parser.GPXParser(xml)
gpx = parser.parse()
if not gpx:
print('Parser error while reparsing: %s' % parser.get_error())
return gpx
def test_checklxml(self):
self.assertEqual('LXML', mod_parser.GPXParser._GPXParser__library())
mod_etree.register_namespace("noglobal_" + prefix, URI.strip('"'))
else:
mod_etree.register_namespace(prefix, URI.strip('"'))
self.gpx.nsmap[prefix] = URI.strip('"')
schema_loc = mod_re.search(r'\sxsi:schemaLocation="[^"]+"', self.xml)
if schema_loc:
_, _, value = schema_loc.group(0).partition('=')
self.gpx.schema_locations = value.strip('"').split()
# Remove default namespace to simplify processing later
self.xml = mod_re.sub(r"""\sxmlns=(['"])[^'"]+\1""", '', self.xml, count=1)
# Build tree
try:
if GPXParser.__library() == "LXML":
# lxml does not like unicode strings when it's expecting
# UTF-8. Also, XML comments result in a callable .tag().
# Strip them out to avoid handling them later.
if mod_utils.PYTHON_VERSION[0] >= '3':
self.xml = self.xml.encode('utf-8')
root = mod_etree.XML(self.xml,
mod_etree.XMLParser(remove_comments=True))
else:
root = mod_etree.XML(self.xml)
except Exception as e:
# The exception here can be a lxml or ElementTree exception.
log.debug('Error in:\n%s\n-----------\n' % self.xml, exc_info=True)
# The library should work in the same way regardless of the
# underlying XML parser that's why the exception thrown
"""
Parse xml (string) or file object. This is just an wrapper for
GPXParser.parse() function.
parser may be 'lxml', 'minidom' or None (then it will be automatically
detected, lxml if possible).
xml_or_file must be the xml to parse or a file-object with the XML.
version may be '1.0', '1.1' or None (then it will be read from the gpx
xml node if possible, if not then version 1.0 will be used).
"""
from . import parser as mod_parser
parser = mod_parser.GPXParser(xml_or_file)
return parser.parse(version)