def storeTree(item): print(f'{item.name} start: {datetime.now()}') actXSD = xmlschema.XMLSchema( '/Users/john/Development/HumAI_data/Schema/iati-activities-schema.xsd') orgXSD = xmlschema.XMLSchema( '/Users/john/Development/HumAI_data/Schema/iati-organisations-schema.xsd' ) db = firestore.Client() try: tree = ET.parse(item) except xml.etree.ElementTree.ParseError as exp: parse_error += 1 print('Parse error:', exp, file=sys.stderr) else: tree_ = bf.data(tree.getroot()) if "iati-activities" in tree_: print('Prune activity ', item.name) # pruneTree(db, None, tree_, actXSD) elif "iati-organisations" in tree_: print('Prune organisation ', item.name) # pruneTree(db, None, tree_, orgXSD) else: pass print(f'{item.name} end: {datetime.now()}')
def setUpClass(cls): cls.schema_class = xmlschema.XMLSchema cls.xsd_types = xmlschema.XMLSchema.builtin_types() cls.content_pattern = re.compile(r'(xs:sequence|xs:choice|xs:all)') cls.default_namespaces = { 'ns': 'ns', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } cls.vh_dir = cls.abspath('cases/examples/vehicles') cls.vh_schema_file = cls.abspath( 'cases/examples/vehicles/vehicles.xsd') cls.vh_xml_file = cls.abspath('cases/examples/vehicles/vehicles.xml') cls.vh_json_file = cls.abspath('cases/examples/vehicles/vehicles.json') cls.vh_schema = xmlschema.XMLSchema(cls.vh_schema_file) cls.vh_namespaces = fetch_namespaces(cls.vh_xml_file) cls.col_dir = cls.abspath('cases/examples/collection') cls.col_schema_file = cls.abspath( 'cases/examples/collection/collection.xsd') cls.col_xml_file = cls.abspath( 'cases/examples/collection/collection.xml') cls.col_json_file = cls.abspath( 'cases/examples/collection/collection.json') cls.col_schema = xmlschema.XMLSchema(cls.col_schema_file) cls.col_namespaces = fetch_namespaces(cls.col_xml_file) cls.st_schema_file = cls.abspath( 'cases/features/decoder/simple-types.xsd') cls.st_schema = xmlschema.XMLSchema(cls.st_schema_file)
def setUpClass(cls): cls.test_dir = os.path.dirname(__file__) cls.xs1 = xmlschema.XMLSchema( os.path.join(cls.test_dir, "examples/vehicles/vehicles.xsd")) cls.xs2 = xmlschema.XMLSchema( os.path.join(cls.test_dir, "examples/collection/collection.xsd")) cls.cars = cls.xs1.elements['vehicles'].type.content_type[0] cls.bikes = cls.xs1.elements['vehicles'].type.content_type[1]
def test_attributes_type(self): parser = XPath2Parser(namespaces=self.namespaces) token = parser.parse("@min le @max") self.assertTrue( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="20" />')))) self.assertTrue( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="2" />')))) schema = xmlschema.XMLSchema(''' <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xpath.test/ns"> <xs:element name="range" type="intRange"/> <xs:complexType name="intRange"> <xs:attribute name="min" type="xs:int"/> <xs:attribute name="max" type="xs:int"/> </xs:complexType> </xs:schema>''') parser = XPath2Parser(namespaces=self.namespaces, schema=XMLSchemaProxy(schema, schema.elements['range'])) token = parser.parse("@min le @max") self.assertTrue( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="20" />')))) self.assertFalse( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="2" />')))) schema = xmlschema.XMLSchema(''' <xs:schema xmlns="http://xpath.test/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xpath.test/ns"> <xs:element name="range" type="intRange"/> <xs:complexType name="intRange"> <xs:attribute name="min" type="xs:int"/> <xs:attribute name="max" type="xs:string"/> </xs:complexType> </xs:schema>''') parser = XPath2Parser(namespaces=self.namespaces, schema=XMLSchemaProxy(schema, schema.elements['range'])) if PY3: self.assertRaises(TypeError, parser.parse, '@min le @max') else: # In Python 2 strings and numbers are comparable and strings are 'greater than' numbers. token = parser.parse("@min le @max") self.assertTrue( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="20" />')))) self.assertTrue( token.evaluate(context=XPathContext( self.etree.XML('<root min="10" max="2" />'))))
def setUpClass(cls): cls.test_dir = os.path.dirname(__file__) cls.namespaces = { 'xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'vh': 'http://example.com/vehicles', 'col': 'http://example.com/ns/collection', 'dt': 'http://example.com/decoder' } cls.vh_schema = xmlschema.XMLSchema( os.path.join(cls.test_dir, 'examples/vehicles/vehicles.xsd')) cls.col_schema = xmlschema.XMLSchema( os.path.join(cls.test_dir, 'examples/collection/collection.xsd')) cls.decoder_schema = xmlschema.XMLSchema( os.path.join(cls.test_dir, 'examples/decoder/decoder.xsd'))
def test_skipped_test(testdir, tmpdir): """ Test a basic skipped test """ testdir.makepyfile(""" import pytest @pytest.mark.skip() def test_skip(): assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) result = testdir.runpytest('-v', '--nunit-xml=' + outfile_pth) result.stdout.fnmatch_lines([ '*test_skip SKIPPED*', ]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema(os.path.join( os.path.abspath(os.path.dirname(__file__)), '../../ext/nunit-src/TestResult.xsd'), validation='lax') out = xs.to_dict(outfile_pth) assert out['@total'] == 1, out assert out['@passed'] == 0, out assert out['@failed'] == 0, out assert out['@skipped'] == 1, out assert out['test-suite']['@total'] == 1 assert out['test-suite']['@passed'] == 0 assert out['test-suite']['@failed'] == 0 assert out['test-suite']['@skipped'] == 1
def test_add_success(self): self.test_result.start_test(self.test1) self.test_result.end_test(self.test1.get_state()) self.test_result.end_tests() xunit_result = xunit.XUnitResult() xunit_result.render(self.test_result, self.job) xunit_output = self.job.config.get("job.run.result.xunit.output") with open(xunit_output, "rb") as fp: xml = fp.read() try: dom = minidom.parseString(xml) except Exception as details: raise ParseXMLError( f"Error parsing XML: '{details}'.\nXML Contents:\n{xml}") self.assertTrue(dom) els = dom.getElementsByTagName("testsuite") self.assertEqual(len(els), 1) self.assertEqual(els[0].attributes["time"].value, "678.237") els = dom.getElementsByTagName("testcase") self.assertEqual(len(els), 1) self.assertEqual(els[0].attributes["time"].value, "678.237") junit_xsd = os.path.abspath( os.path.join( os.path.dirname(os.path.dirname(__file__)), os.path.pardir, ".data", "jenkins-junit.xsd", )) xml_schema = xmlschema.XMLSchema(junit_xsd) self.assertTrue(xml_schema.is_valid(xunit_output))
def test_error_test(testdir, tmpdir): """ Test a test that fails """ testdir.makepyfile(""" def test_error(test_madeup_fixture): assert 1 == 1 """) outfile = tmpdir.join("out.xml") outfile_pth = str(outfile) result = testdir.runpytest("-v", "--nunit-xml=" + outfile_pth) result.stdout.fnmatch_lines(["*test_error ERROR*"]) assert result.ret != 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema( os.path.join( os.path.abspath(os.path.dirname(__file__)), "../../ext/nunit-src/TestResult.xsd", ), validation="lax", ) out = xs.to_dict(outfile_pth) assert out["@total"] == 1, out assert out["@passed"] == 0, out assert out["@failed"] == 1, out assert out["@skipped"] == 0, out assert out["test-suite"]["@total"] == 1 assert out["test-suite"]["@passed"] == 0 assert out["test-suite"]["@failed"] == 1 assert out["test-suite"]["@skipped"] == 0
def test_error_test(testdir, tmpdir): """ Test a test that fails """ testdir.makepyfile(""" def test_error(test_madeup_fixture): assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) result = testdir.runpytest('-v', '--nunit-xml=' + outfile_pth) result.stdout.fnmatch_lines([ '*test_error ERROR*', ]) assert result.ret != 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema(os.path.join( os.path.abspath(os.path.dirname(__file__)), '../../ext/nunit-src/TestResult.xsd'), validation='lax') out = xs.to_dict(outfile_pth) assert out['@total'] == 1, out assert out['@passed'] == 0, out assert out['@failed'] == 1, out assert out['@skipped'] == 0, out assert out['test-suite']['@total'] == 1 assert out['test-suite']['@passed'] == 0 assert out['test-suite']['@failed'] == 1 assert out['test-suite']['@skipped'] == 0
def test_nillable(self): # Issue #76 xsd_string = """<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="foo" type="Foo" /> <xs:complexType name="Foo"> <xs:sequence minOccurs="1" maxOccurs="1"> <xs:element name="bar" type="xs:integer" nillable="true" /> </xs:sequence> </xs:complexType> </xs:schema> """ xsd_schema = xmlschema.XMLSchema(xsd_string) xml_string_1 = "<foo><bar>0</bar></foo>" xml_string_2 = """<?xml version="1.0" encoding="UTF-8"?> <foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <bar xsi:nil="true"></bar> </foo> """ self.assertTrue( xsd_schema.is_valid(source=xml_string_1, use_defaults=False)) self.assertTrue( xsd_schema.is_valid(source=xml_string_2, use_defaults=False)) obj = xsd_schema.decode(xml_string_2, use_defaults=False) self.check_etree_elements(ElementTree.fromstring(xml_string_2), xsd_schema.encode(obj))
def invalid_svg_dir_to_valid_svg_dir(invalid_svg_dir, valid_svg_dir): """ Classifies the svg files into invalid and valid, and attempts to correct invalid files. invalid_svg_dir: directory of invalid svg files valid_svg_dir: directory of valid svg files """ #creating valid_svg_dir if doesn't exist already if not os.path.exists(valid_svg_dir): os.mkdir(valid_svg_dir) print("Directory ", valid_svg_dir, " Created ") else: print("Directory ", valid_svg_dir, " already exists") #iterating through the files in invalid_svg_dir for filename in os.listdir(invalid_svg_dir): schema = xmlschema.XMLSchema('./svg-schema.xsd') try: valid = schema.is_valid(invalid_svg_dir + "/" + filename) except: valid = False #moving the validated files into valid_svg_dir if valid: os.rename(invalid_svg_dir + "/" + filename, valid_svg_dir + "/" + filename) else: correction_attempt(filename, 1, invalid_svg_dir + "/" + filename, invalid_svg_dir, valid_svg_dir) return None
def create_miriam_json() -> None: """Parse the latest miriam information. :return: """ # "Tue, 04 Jun 2019 15:31:52 GMT" -> 2019-06-04T15:31:52 # date="2019-06-04T15:31:52" data-version="2019-04-05T10:42:00 # xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating 'http://www.fungalbarcoding.org/BioloMICS.aspx?Table=Fungal barcodes&Rec=$id&Fields=All&ExactMatch=T' with XsdPatternFacets(['\\S*$id\\S*']): # > http://www.fungalbarcoding.org/BioloMICS.aspx?$id xs = xmlschema.XMLSchema(str(RESOURCES_DIR / "MiriamXML.xsd")) d = xs.to_dict(str(RESOURCES_DIR / "IdentifiersOrg-Registry.xml")) # pprint(d['datatype'][1]) datatypes = {} for entry in d["datatype"]: datatypes[entry["namespace"]] = { "id": entry["@id"], "pattern": entry["@pattern"], "name": entry["name"], "namespace": entry["namespace"], "definition": entry["definition"], } # pprint(datatypes) with open(MIRIAM_JSON, "w") as fp: json.dump(datatypes, fp)
def test_slow_test(testdir, tmpdir): """ Test a test that takes 3 seconds """ testdir.makepyfile(""" import time def test_basic(): time.sleep(3) assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) result = testdir.runpytest( '-v', '--nunit-xml='+outfile_pth ) result.stdout.fnmatch_lines([ '*test_basic PASSED*', ]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../ext/nunit-src/TestResult.xsd'), validation='lax') out = xs.to_dict(outfile_pth) assert out['@total'] == 1, out assert out['@passed'] == 1, out assert out['@failed'] == 0, out assert out['@skipped'] == 0, out assert out['test-suite']['@total'] == 1 assert out['test-suite']['@passed'] == 1 assert out['test-suite']['@failed'] == 0 assert out['test-suite']['@skipped'] == 0 assert out['test-suite']['test-case']['@duration'] > 3.0
def test_keyword_filter(testdir, tmpdir): testdir.makepyfile( """ def test_basic(record_nunit_property): assert 1 == 1 """ ) outfile = tmpdir.join("out.xml") outfile_pth = str(outfile) result = testdir.runpytest("-v", "--nunit-xml=" + outfile_pth, "-k basic") result.stdout.fnmatch_lines(["*test_basic PASSED*"]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema( os.path.join( os.path.abspath(os.path.dirname(__file__)), "../../ext/nunit-src/TestResult.xsd", ), validation="lax", ) out = xs.to_dict(outfile_pth) assert out["@total"] == 1, out assert out["@passed"] == 1, out assert out["@failed"] == 0, out assert out["@skipped"] == 0, out assert out["test-suite"]["@total"] == 1 assert out["test-suite"]["@passed"] == 1 assert out["test-suite"]["@failed"] == 0 assert out["test-suite"]["@skipped"] == 0 assert "filter" in out assert out["filter"]["name"][0]["$"] == "basic" assert out["filter"]["name"][0]["@re"] == 0
def test_validate(filename): schema = xmlschema.XMLSchema(filename) assert not schema.is_valid('<invalid/>') with pytest.raises(xmlschema.XMLSchemaValidationError): schema.validate('<invalid/>')
def test_basic_against_reference_schema(testdir, tmpdir): """ Test a basic output against the schema in the Nunit3 source """ # create a temporary pytest test module testdir.makepyfile(""" def test_basic(): assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) # run pytest with the following cmd args result = testdir.runpytest( '-v', '--nunit-xml='+outfile_pth ) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*test_basic PASSED*', ]) # ensure the output file exists os.path.exists(outfile_pth) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 my_path = os.path.abspath(os.path.dirname(__file__)) xs = xmlschema.XMLSchema(os.path.join(my_path, '../../ext/nunit-src/TestResult.xsd'), validation='lax') xt = ElementTree.parse(outfile_pth) assert xs.is_valid(xt), xs.validate(xt)
def __init__(self, verbose: bool = False): self.verbose = verbose self.schema = xmlschema.XMLSchema( resource_string(__name__, "resources/OpenSCENARIO-1.1.xsd").decode("utf-8"))
def test_skipped_test(testdir, tmpdir): """ Test a basic skipped test """ testdir.makepyfile(""" import pytest @pytest.mark.skip() def test_skip(): assert 1 == 1 """) outfile = tmpdir.join("out.xml") outfile_pth = str(outfile) result = testdir.runpytest("-v", "--nunit-xml=" + outfile_pth) result.stdout.fnmatch_lines(["*test_skip SKIPPED*"]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema( os.path.join( os.path.abspath(os.path.dirname(__file__)), "../../ext/nunit-src/TestResult.xsd", ), validation="lax", ) out = xs.to_dict(outfile_pth) assert out["@total"] == 1, out assert out["@passed"] == 0, out assert out["@failed"] == 0, out assert out["@skipped"] == 1, out assert out["test-suite"]["@total"] == 1 assert out["test-suite"]["@passed"] == 0 assert out["test-suite"]["@failed"] == 0 assert out["test-suite"]["@skipped"] == 1
def test_attachment(testdir, tmpdir): """ Test a basic test with an additional property """ testdir.makepyfile(""" def test_basic(add_nunit_attachment): add_nunit_attachment("file.pth", "desc") assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) result = testdir.runpytest( '-v', '--nunit-xml='+outfile_pth ) result.stdout.fnmatch_lines([ '*test_basic PASSED*', ]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../ext/nunit-src/TestResult.xsd'), validation='lax') out = xs.to_dict(outfile_pth) assert out['@total'] == 1, out assert out['@passed'] == 1, out assert out['@failed'] == 0, out assert out['@skipped'] == 0, out assert out['test-suite']['@total'] == 1 assert out['test-suite']['@passed'] == 1 assert out['test-suite']['@failed'] == 0 assert out['test-suite']['@skipped'] == 0 assert out['test-suite']['test-case']['attachments']['attachment'][0]['description'] == "desc" assert out['test-suite']['test-case']['attachments']['attachment'][0]['filePath'] == "file.pth"
def __init__(self, Url, mime, cc, xsdPath): self.UrlLocation = Url self.LocalPath = None self.xsdPath = xsdPath # add country code in front if the name: some lists have the same name self.LocalName = cc + "-" + url_remote_file_name(self.UrlLocation) self.TypeVersion = None self.SeqNumber = None self.OperatorName = None self.OperatorTeritory = cc self.MimeType = mime self.TSLType = None self.NextUpdate = None self.Signature = None self.ForceDownload = False self.LocalWD = None self.ChildrenGenericCount = 0 self.ChildrenLOTLCount = 0 self.ChildrenPdfCount = 0 self.Status = ListStatus.NotDownloaded self.ListsOfTrust = [] self.TrustServiceProviders = [] self.AllServices = [] self.eutlschema = xmlschema.XMLSchema(self.xsdPath, )
def test_basic_property(testdir, tmpdir): """ Test a basic test with an additional property """ testdir.makepyfile(""" def test_basic(record_nunit_property): record_nunit_property("test", "value") assert 1 == 1 """) outfile = tmpdir.join('out.xml') outfile_pth = str(outfile) result = testdir.runpytest( '-v', '--nunit-xml='+outfile_pth ) result.stdout.fnmatch_lines([ '*test_basic PASSED*', ]) assert result.ret == 0 os.path.exists(outfile_pth) xs = xmlschema.XMLSchema(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../ext/nunit-src/TestResult.xsd'), validation='lax') out = xs.to_dict(outfile_pth) assert out['@total'] == 1, out assert out['@passed'] == 1, out assert out['@failed'] == 0, out assert out['@skipped'] == 0, out assert out['test-suite']['@total'] == 1 assert out['test-suite']['@passed'] == 1 assert out['test-suite']['@failed'] == 0 assert out['test-suite']['@skipped'] == 0 assert "test" in [i['@name'] for i in out['test-suite']['test-case']['properties']['property']] assert "value" in [i['@value'] for i in out['test-suite']['test-case']['properties']['property']] assert out['test-suite']['test-case']['@classname'] == 'test_basic_property.py' assert out['test-suite']['test-case']['@methodname'] == 'test_basic'
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 test_non_global_schema_path(self): # Issue #157 xs = xmlschema.XMLSchema("""<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:foo="http://example.com/foo" targetNamespace="http://example.com/foo"> <xs:complexType name="type1"> <xs:sequence> <xs:element name="sub_part1" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="type2"> <xs:sequence> <xs:element name="sub_part2" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:element name="foo"> <xs:complexType> <xs:sequence> <xs:element name="part1" type="foo:type1" /> <xs:element name="part2" type="foo:type2" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") self.assertEqual( xs.to_dict( """<part1 xmlns:foo="http://example.com/foo"> <sub_part1>test</sub_part1> </part1>""", schema_path='.//part1', ), {"sub_part1": "test"})
def validate(kwargs): """Validate xml against xsd schema file Arguments: kwargs {Object} -- Keyword arguments parsed by Click library """ # validate xml against xsd schema file rt = True pout(kwargs, kwargs["verbose"], Level.DEBUG) try: pout("Schema: {file}".format(file=kwargs["schema"]), kwargs["verbose"], Level.DEBUG) mySchema = xmlschema.XMLSchema(kwargs["schema"]) try: pout("XML: {file}".format(file=kwargs["xml"]), kwargs["verbose"], Level.DEBUG) mySchema.validate(kwargs["xml"]) except Exception as e: pout("validation failed", kwargs["verbose"], Level.WARNING) pout("{msg}".format(msg=str(e)), kwargs["verbose"], Level.ERROR) rt = False except Exception as e: pout("XML Schema load failed.", kwargs["verbose"], Level.WARNING) pout("{msg}".format(msg=str(e)), kwargs["verbose"], Level.ERROR) rt = False if rt: pout("XML Schema valid!", kwargs["verbose"], Level.INFO) return
def test_add_success(self): self.test_result.start_test(self.test1) self.test_result.end_test(self.test1.get_state()) self.test_result.end_tests() xunit_result = xunit.XUnitResult() xunit_result.render(self.test_result, self.job) with open(self.job.args.xunit_output, 'rb') as fp: xml = fp.read() try: dom = minidom.parseString(xml) except Exception as details: raise ParseXMLError("Error parsing XML: '%s'.\nXML Contents:\n%s" % (details, xml)) self.assertTrue(dom) els = dom.getElementsByTagName('testsuite') self.assertEqual(len(els), 1) self.assertEqual(els[0].attributes['time'].value, '678.237') els = dom.getElementsByTagName('testcase') self.assertEqual(len(els), 1) self.assertEqual(els[0].attributes['time'].value, '678.237') junit_xsd = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir, ".data", 'jenkins-junit.xsd')) xml_schema = xmlschema.XMLSchema(junit_xsd) self.assertTrue(xml_schema.is_valid(self.job.args.xunit_output))
def validate_musicxml(xml, debug=False): """ Validate an XML file against an XSD. Parameters ---------- xml: str Path to XML file debug: bool, optional If True, raise an exception when the xml is invalid, and print out the cause. Otherwise just return True when the XML is valid and False otherwise Returns ------- bool or None None if debug=True, True or False otherwise, signalling validity """ global _XML_VALIDATOR if not _XML_VALIDATOR: _XML_VALIDATOR = xmlschema.XMLSchema(_MUSICXML_SCHEMA) if debug: return _XML_VALIDATOR.validate(xml) else: return _XML_VALIDATOR.is_valid(xml)
def on_eafUpload(contents, name, projectDirectory): if name is None: return ("", "") print("on_eafUpload, name: %s" % name) data = contents.encode("utf8").split(b";base64,")[1] filename = os.path.join(projectDirectory, name) if not filename[-4:] == '.eaf': eaf_validationMessage = 'Please select an ELAN project (.eaf) file.' return eaf_validationMessage, '' with open(filename, "wb") as fp: fp.write(base64.decodebytes(data)) fileSize = os.path.getsize(filename) print("eaf file size: %d" % fileSize) schema = xmlschema.XMLSchema( 'http://www.mpi.nl/tools/elan/EAFv3.0.xsd') validXML = schema.is_valid(filename) eaf_validationMessage = "File %s (%d bytes) is valid XML." % (name, fileSize) if (not validXML): try: schema.validate(filename) except xmlschema.XMLSchemaValidationError as e: failureReason = e.reason eaf_validationMessage = "XML parsing error: %s [File: %s]" % ( failureReason, filename) return eaf_validationMessage, filename
def main(): """Generate RST from XSD documentation tags Iterate through specified schema file, accumulate documentation elements, convert to RST and write to output files. Output files are opened (overwritting) for each main level of the schema: Preamble, Network, Station, Channel, Response in the pattern: `OUTDIR/level-LEVEL.rst` """ print (f'Reading schema file {args.schemafile} and writing RST to directory {args.outdir}') schema = xmlschema.XMLSchema(args.schemafile) # create warning.rst file for deprectaion warnings with open("warnings.rst", "w") as warnfile: print(".. Put any comments here\n", file=warnfile) print(" Warning, this file is regenerated from the annotations in the schema file.\n", file=warnfile) print(" Any changes will be overwritten by convert_xsd_to_rst.py.\n\n\n", file=warnfile) print("\n", file=warnfile) print("The following are potential future changes, as tagged in the schema with <warning> elements in the documentation. They may result in modifications or deletions in future versions of StationXML.\n", file=warnfile) print("\n\n", file=warnfile) level_xpaths = ['fsx:FDSNStationXML', 'fsx:FDSNStationXML/fsx:Network', 'fsx:FDSNStationXML/fsx:Network/fsx:Station', 'fsx:FDSNStationXML/fsx:Network/fsx:Station/fsx:Channel', 'fsx:FDSNStationXML/fsx:Network/fsx:Station/fsx:Channel/fsx:Response'] words = set() for i, xpath in enumerate(level_xpaths): xsd_element = schema.find(xpath) stop_element = None this_element = os.path.basename(xpath).split(':')[1] if i < 4: stop_element = os.path.basename(level_xpaths[i+1]).split(':')[1] level_elem = walk_tree(xsd_element) # Use Preamble instead of root element name if this_element=="FDSNStationXML": this_element="Preamble" # Generate output file name for this level rst_file = os.path.join (args.outdir, f'level-{this_element.lower()}.rst') if args.verbose: print (f'Writing to {rst_file}') with open(rst_file, 'w') as outfile: print(".. Put any comments here\n", file=outfile) print(" Warning, this file is regenerated from the annotations in the schema file.\n", file=outfile) print(" Any changes will be overwritten by convert_xsd_to_rst.py.\n", file=outfile) write_tree(level_elem, stop_element, outfile = outfile) recur_spelling(words, level_elem) save_spelling(words)
def setUpClass(cls): cls.schema = xmlschema.XMLSchema(''' <!-- Dummy schema for testing proxy API --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xpath.test/ns"> <xs:element name="test_element" type="xs:string"/> <xs:attribute name="test_attribute" type="xs:string"/> </xs:schema>''')
def validate_item_xml_configuration_file(self, xml_schema_file_name): xsd = xmlschema.XMLSchema(xml_schema_file_name) if xsd.is_valid(self.item_xml_configuration_file_name): valid_item_configuration_xml_file = True else: valid_item_configuration_xml_file = False return (valid_item_configuration_xml_file)