def test_issue_171(self): # First schema has an assert with naive check schema = xmlschema.XMLSchema11( self.casepath('issues/issue_171/issue_171.xsd')) self.check_validity(schema, '<tag name="test" abc="10" def="0"/>', False) self.check_validity(schema, '<tag name="test" abc="10" def="1"/>', False) self.check_validity(schema, '<tag name="test" abc="10"/>', True) # Same schema with a more reliable assert expression schema = xmlschema.XMLSchema11( self.casepath('issues/issue_171/issue_171b.xsd')) self.check_validity(schema, '<tag name="test" abc="10" def="0"/>', False) self.check_validity(schema, '<tag name="test" abc="10" def="1"/>', False) self.check_validity(schema, '<tag name="test" abc="10"/>', True) # Another schema with a simple assert expression to test that EBV of abc/def='0' is True schema = xmlschema.XMLSchema11( self.casepath('issues/issue_171/issue_171c.xsd')) self.check_validity(schema, '<tag name="test" abc="0" def="1"/>', True) self.check_validity(schema, '<tag name="test" abc="1" def="0"/>', True) self.check_validity(schema, '<tag name="test" abc="1" def="1"/>', True) self.check_validity(schema, '<tag name="test" abc="0" def="0"/>', True) self.check_validity(schema, '<tag name="test" abc="1"/>', False) self.check_validity(schema, '<tag name="test" def="1"/>', False)
def load_schema(xsd_xml, datachecks_xml): global schema, schema_etree, schema_root, datachecks schema_etree = etree.parse(xsd_xml) schema_etree.xinclude() schema = xmlschema.XMLSchema11( etree.tostring(schema_etree, encoding="unicode")) schema_root = get_node(schema_etree, f"/xs:schema/xs:element") datachecks_etree = etree.parse(datachecks_xml) datachecks_etree.xinclude() datachecks = xmlschema.XMLSchema11( etree.tostring(datachecks_etree, encoding="unicode"))
def read(self): try: schema_file = open(self.schema) except FileNotFoundError: logger.error(f'{self.schema} XSD file not found') schema = xmlschema.XMLSchema11(schema_file, base_url='', converter=CMLCuemsConverter) # schema = xmlschema.XMLSchema(schema_file, base_url='') try: xml_file = open(self.xmlfile) except FileNotFoundError: logger.error(f'{self.xmlfile} XML file not found') raise xml_dict = schema.to_dict(xml_file, dict_class=dict, list_class=list, validation='strict', strip_namespaces=True, attr_prefix='') super().__init__(xml_dict) self.loaded = True return self
def validate_scenario_schema(scenario_info): """ Validate settings in scenario xml if there is scenario schema :param xsd_doc: scenario schema :param scenario_info: scenario file """ """ XMLSchema does not process XInclude. Use lxml to expand the schema which is feed to XMLSchema as a string. """ xsd_doc = etree.parse(common.SCENARIO_SCHEMA_FILE) xsd_doc.xinclude() my_schema = xmlschema.XMLSchema11(etree.tostring(xsd_doc, encoding="unicode")) it = my_schema.iter_errors(scenario_info) for idx, validation_error in enumerate(it, start=1): key = "" if not validation_error: continue else: path = str(validation_error.path).split("/") cnt = 0 for p in path: if '[' in p: idx = int(p.split("[")[1].split("]")[0]) - 1 p = p.split("[")[0] + ":id=" + str(idx) path[cnt] = p cnt = cnt + 1 key =','.join(path[2:]) element = "'" + path[-1] + "' " reason = validation_error.reason + ": last call: " + str(validation_error.obj) scenario_cfg_lib.ERR_LIST[key] = element + reason
def schema(self, path): if path is not None: if os.path.isfile(path): self._schema = path self.schema_object = xmlschema.XMLSchema11( self._schema, converter=self.converter) else: raise FileNotFoundError("schema file not found")
def test_issue_171(self): # First schema has an assert with naive check that maps '0' to False ('0'--> 0--> false) schema = xmlschema.XMLSchema11( self.casepath('issues/issue_171/issue_171.xsd')) self.check_validity(schema, '<tag name="test" abc="10" def="0"/>', True) self.check_validity(schema, '<tag name="test" abc="10" def="1"/>', False) self.check_validity(schema, '<tag name="test" abc="10"/>', True) # Same schema with a more reliable assert expression schema = xmlschema.XMLSchema11( self.casepath('issues/issue_171/issue_171b.xsd')) self.check_validity(schema, '<tag name="test" abc="10" def="0"/>', False) self.check_validity(schema, '<tag name="test" abc="10" def="1"/>', False) self.check_validity(schema, '<tag name="test" abc="10"/>', True)
def initialize_validator(path: Path, version: str): try: __tracebackhide__ = True if version == "1.1": return xmlschema.XMLSchema11(str(path)) else: return xmlschema.XMLSchema10(str(path)) except Exception: return None
def parse(xml_file): try: import xmlschema schema = xmlschema.XMLSchema11(os.path.dirname(os.path.realpath(__file__)) + '/xsd/callbacks.xsd') schema.validate(xml_file) except ModuleNotFoundError: print(f'warning: missing python package "xmlschema"; input file {os.path.basename(xml_file)} will not be validated.', file=sys.stderr) tree = ET.parse(xml_file) root = tree.getroot() return model.Plugin(root)
def __init__(self, schema, searchpath=None, types_map=None): if isinstance(schema, xmlschema.XMLSchemaBase): self.schema = schema else: self.schema = xmlschema.XMLSchema11(schema) file_loaders = [] if searchpath: file_loaders.append(FileSystemLoader(searchpath)) if self.searchpaths: file_loaders.extend( FileSystemLoader(str(path)) for path in reversed(self.searchpaths)) if not file_loaders: raise ValueError("no search paths defined!") loader = ChoiceLoader( file_loaders) if len(file_loaders) > 1 else file_loaders[0] self.types_map = self.builtin_types.copy() if types_map: if not self.schema.target_namespace: self.types_map.update(types_map) else: ns_part = '{%s}' % self.schema.target_namespace self.types_map.update( (ns_part + k, v) for k, v in types_map.items()) self.filters = {} self.tests = {} for name in filter(lambda x: callable(getattr(self, x)), dir(self)): method = getattr(self, name) if inspect.isfunction(method): # static methods if getattr(method, 'is_filter', False): self.filters[name] = method elif getattr(method, 'is_test', False): self.tests[name] = method elif inspect.isroutine(method) and hasattr(method, '__func__'): # class and instance methods if getattr(method.__func__, 'is_filter', False): self.filters[name] = method elif getattr(method.__func__, 'is_test', False): self.tests[name] = method type_mapping_filter = '{}_type'.format( self.formal_language).lower().replace(' ', '_') if type_mapping_filter not in self.filters: self.filters[type_mapping_filter] = self.map_type self._env = Environment(loader=loader) self._env.filters.update(self.filters) self._env.tests.update(self.tests)
def validate_board(xsd_path, board_etree): schema_etree = etree.parse(xsd_path) schema_etree.xinclude() schema = xmlschema.XMLSchema11(schema_etree) it = schema.iter_errors(board_etree) count = 0 for error in it: anno = error.validator.annotation severity = anno.elem.get("{https://projectacrn.org}severity") description = anno.elem.find( "{http://www.w3.org/2001/XMLSchema}documentation").text logging_fn[severity](description) if severity in ["error", "warning"]: count += 1 return count
def generate_xsd_namespace(namespace: str, uri: str, schema_loc: str, title: Optional[str] = None) -> str: """ Return a Python module that represents the namespace :param namespace: Namespace to generate (e.g. "xsd") :param uri: Namespace uri (e.g. "http://www.w3.org/2001/XMLSchema#") :param schema_loc: file or URL of XML Schema :param title: Additional title for output file :return: python text for resource """ xs = xmlschema.XMLSchema11( 'https://www.w3.org/2009/XMLSchema/XMLSchema.xsd') schema = xs.to_dict(schema_loc) return fill_template(namespace, _hdr(schema, uri, schema_loc, title), _body(schema, uri))
def apply_data_checks(board_info, scenario_info): xsd_doc = etree.parse(common.DATACHECK_SCHEMA_FILE) xsd_doc.xinclude() datachecks_schema = xmlschema.XMLSchema11(etree.tostring(xsd_doc, encoding="unicode")) main_etree = etree.parse(board_info) scenario_etree = etree.parse(scenario_info) main_etree.getroot().extend(scenario_etree.getroot()[:]) # FIXME: Figure out proper error keys for data check failures error_key = "" it = datachecks_schema.iter_errors(main_etree) for idx, error in enumerate(it, start=1): anno = error.validator.annotation description = anno.documentation[0].text severity = anno.elem.get("{https://projectacrn.org}severity") if severity == "error": if error_key in scenario_cfg_lib.ERR_LIST.keys(): scenario_cfg_lib.ERR_LIST[error_key].append("\n" + description) else: scenario_cfg_lib.ERR_LIST[error_key] = description
def _parse_xml_config(self): """ Parses the specified xml configuration file """ self.__log.info("Parsing XML config: %s", self._xsd_schema) exit = False self.__log.debug("Calling XMLSchema11 api") try: xsd_schema = xmlschema.XMLSchema11(self._xsd_schema) except: exit = True error = traceback.format_exc() self.__log.info("Error while parsing xsd file:\n%s\n\n%s", self._xsd_schema, error) if not exit: self.__log.debug("Converting XML schema to dict") try: xml_dict = xsd_schema.to_dict(self._args.config) #A last manual validation must be done here: the InitialBank value must #be less or equal than the total number of banks if xml_dict['@InitialBank'] > len(xml_dict['Bank']): raise Exception("InitialBank is higher than the possible number of " "banks / maximum: " + str(len(xml_dict['Bank'])) + \ ", given value: " + str(xml_dict['@InitialBank'])) self.__log.debug("Got: \n%s", PrettyFormat(xml_dict)) except: exit = True error = traceback.format_exc() message = "Error while parsing xml file:\n%s\n\n%s" % ( self._args.config, error) self.__log.info(message) if exit: self.__log.debug("Unexpected error occured, aborting...") self._free_midi() sys.exit() self._xml_dict = xml_dict
def __init__(self, xsd_path): self.xmlschema = xmlschema.XMLSchema11(xsd_path)
def __init__(self, url): ''' Constructor ''' self.url = url self.schema = xmlschema.XMLSchema11(self.url)
import os try: from utilities import utilities except ModuleNotFoundError: try: from Akoma.utilities import utilities except ModuleNotFoundError: print("Import error") exit(-1) import xmlschema if __name__ == "__main__": #schema = xmlschema.XMLSchema11('../schema/akn3.0_schema1.1_Republic_Serbia.xsd') schema = xmlschema.XMLSchema11('../schema/akoma30.xsd') folder = "akoma_result" fajls = utilities.sort_file_names(os.listdir("../data/" + folder)) f = open("../data/sanity_schema.txt", mode="a+", encoding="UTF-8") for i in range(0, len(fajls)): try: try: print(fajls[i] + ";", end="") schema.validate('../data/' + folder + '/' + fajls[i]) except Exception as e1: print("\n Schema validation error :" + fajls[i] + " MES:" + e1.message) f.write(fajls[i] + " : Not valid with schema " + e1.message + "\n") except Exception as e: print("\n Not well formed " + fajls[i])
import xmlschema my_schema = xmlschema.XMLSchema11('config.xsd') my_schema.validate('hybrid_rt.xml')
def validateTests(): """ Runs validation tests on regression tests and displays results. @ In, None @ Out, int, number of failed tests. """ print('Beginning test validation...') tests = get_coverage_tests.getRegressionTests(skipExpectedFails=True) schema = os.path.join(scriptDir, 'XSDSchemas', 'raven.xsd') mySchema = xmlschema.XMLSchema11(schema) res = [0, 0, 0] #run, pass, fail failed = {} devnull = open(os.devnull, "wb") for dr, files in tests.items(): #print directory being checked' checkmsg = ' Directory: ' + dr print(colors.neutral + checkmsg.rjust(maxlen, '-')) #check files in directory for f in files: fullpath = os.path.join(dr, f) # check if input exists; if not, it probably gets created by something else, and can't get checked here if not os.path.isfile(fullpath): print('File "{}" does not exist; skipping validation test ...'. format(fullpath)) continue res[0] += 1 startmsg = 'Validating ' + f #expand external XML nodes # - first, though, check if the backup file already exists if os.path.isfile(fullpath + '.bak'): print( colors.neutral + 'Could not check for ExternalXML since a backup file exists! Please remove it to validate.' ) # Since directing output of shell commands to file /dev/null is problematic on Windows, this equivalent form is used # which is better supported on all platforms. cmd = 'python ' + os.path.join( conversionDir, 'externalXMLNode.py') + ' ' + fullpath result = subprocess.call(cmd, shell=True, stdout=devnull) err = 'Error running ' + cmd screenmsg = colors.neutral + startmsg + ' ' if result == 0: #run xmlschema library result = mySchema.is_valid(fullpath) if result: # == 0: #success res[1] += 1 endmsg = 'validated' endcolor = colors.ok postprint = '' else: try: err = mySchema.validate(fullpath) except xmlschema.validators.exceptions.XMLSchemaValidationError as ae: err = " {}".format(str(ae)) res[2] += 1 endmsg = 'FAILED' endcolor = colors.fail postprint = colors.fail + str(err) if dr not in failed.keys(): failed[dr] = [] failed[dr].append(f) screenmsg += colors.neutral + ''.rjust( maxlen - len(startmsg) - len(endmsg) - 1, '.') screenmsg += endcolor + endmsg + colors.neutral if len(postprint): screenmsg += postprint + colors.neutral print(screenmsg) #return externalNode xmls os.system('mv {orig}.bak {orig}'.format(orig=fullpath)) print(colors.neutral + '') print( '-----------------------------------------------------------------------' ) print(' Failure Summary') print( '-----------------------------------------------------------------------' ) for dr, files in failed.items(): for f in files: print(colors.fail + os.path.join(dr, f)) print(colors.neutral + '') print('Validated: {ok} Failed: {fail} Run: {run}'.format( ok=colors.ok + str(res[1]) + colors.neutral, fail=colors.fail + str(res[2]) + colors.neutral, run=res[0])) return res[2]
* main_help: text of the -h, --help option * config_help: help of the "--config" command line option """ self._parser.add_argument("-h", "--help", action="help", default=argparse.SUPPRESS, help=main_help) self._parser.add_argument("-c", "--config", default="conf/sample-config.xml", help=config_help) def parse_arguments(self): """ Validates the supplied command line options. It will show an error message if the vaildation failed and then it will exit """ return self._parser.parse_args() schema_name = 'conf/MidiBassPedalController.xsd' if __name__ == "__main__": parser = ValidateXMLArgumentParser() parser.add_arguments() args = parser.parse_arguments() my_schema = xmlschema.XMLSchema11(schema_name) pprint(my_schema.is_valid(args.config)) pprint(my_schema.to_dict(args.config))
def load_xsd(name) -> xmlschema.XMLSchema11: return xmlschema.XMLSchema11( pkg_resources.resource_string("swh.deposit", f"xsd/{name}.xsd").decode())
def __init__(self, schema_etree, datachecks_etree): """Initialize the validator with preprocessed schemas in ElementTree.""" self.schema = xmlschema.XMLSchema11(schema_etree) self.datachecks = xmlschema.XMLSchema11(datachecks_etree)
def validate(self): schema_file = open(self.schema) schema = xmlschema.XMLSchema11(schema_file, base_url='') xml_file = open(self.xmlfile) return schema.validate(xml_file)