Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@pytest.fixture(scope="session")
def schema():
"""Returns a xmlschema.XMLSchema object for the junit-10.xsd file"""
fn = Path(__file__).parent / "example_scripts/junit-10.xsd"
with fn.open() as f:
return xmlschema.XMLSchema(f)
def parse_pw_xml_post_6_2(xml, include_deprecated_v2_keys=False):
"""Parse the content of XML output file written by `pw.x` with the new schema-based XML format.
:param xml: parsed XML
:param include_deprecated_v2_keys: boolean, if True, includes deprecated keys from old parser v2
:returns: tuple of two dictionaries, with the parsed data and log messages, respectively
"""
e_bohr2_to_coulomb_m2 = 57.214766 # e/a0^2 to C/m^2 (electric polarization) from Wolfram Alpha
logs = get_logging_container()
# detect schema name+path from XML contents
schema_filepath = get_schema_filepath(xml)
try:
xsd = XMLSchema(schema_filepath)
except URLError:
# If loading the XSD file specified in the XML file fails, we try the default
schema_filepath_default = get_default_schema_filepath()
try:
xsd = XMLSchema(schema_filepath_default)
except URLError:
raise XMLParseError('Could not open or parse the XSD files {} and {}'.format(schema_filepath, schema_filepath_default))
else:
schema_filepath = schema_filepath_default
# Validate XML document against the schema
# Returned dictionary has a structure where, if tag ['key'] is "simple", xml_dictionary['key'] returns its content.
# Otherwise, the following keys are available:
#
def __init__(self, xsd, elem, enable_choice):
self.xsd = xmlschema.XMLSchema(xsd)
self.elem = elem
self.enable_choice = enable_choice
self.root = True
self.vals = {}
>Valid_TCX.tcx
> Valid
>Invalid_TCX.tcx
> Invalid:
> mismatched tag: line 61, column 48
"""
# Imports
import sys, tempfile, urllib.request, xmlschema
# Make temporary directory
with tempfile.TemporaryDirectory() as tempdir:
# Download and import schema to check against
url = 'https://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd'
urllib.request.urlretrieve(url, tempdir+'/TrainingCenterDatabasev2.xsd')
schema = xmlschema.XMLSchema(tempdir+'/TrainingCenterDatabasev2.xsd')
# Validate files given in command line to validate
for argument in sys.argv[1:]:
if argument[-4:] == '.tcx':
print(argument)
try:
schema.validate(argument)
print('\tValid')
except Exception as e:
print('\tInvalid: ', end='\n\t\t')
print(e)
def _validate_openscenario_catalog_configuration(self, catalog_xml_tree):
"""
Validate the given OpenSCENARIO catalog config against the 0.9.1 XSD
Note: This will throw if the catalog config is not valid. But this is fine here.
"""
xsd_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../openscenario/OpenSCENARIO.xsd")
xsd = xmlschema.XMLSchema(xsd_file)
xsd.validate(catalog_xml_tree)
def validateXmlschema(xml_src, xsd_file):
try:
import xmlschema
schema = xmlschema.XMLSchema(xsd_file)
schema.validate(xml_src)
except ImportError:
log.debug("xmlschema not found, validation disabled")
def __loadSchema(self):
try:
self.__oscSchema = xmlschema.XMLSchema(self._scenarioFormatFilePath)
return True
except:
return False
def _validate_openscenario_configuration(self):
"""
Validate the given OpenSCENARIO config against the 0.9.1 XSD
Note: This will throw if the config is not valid. But this is fine here.
"""
xsd_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../openscenario/OpenSCENARIO.xsd")
xsd = xmlschema.XMLSchema(xsd_file)
xsd.validate(self.xml_tree)