def test_get_validator_o3(): o3_ref = OnixFile(data.VALID_ONIX3_REF) validator = o3_ref.get_validator() assert isinstance(validator, etree.XMLSchema) o3_short = OnixFile(data.VALID_ONIX3_SHORT) validator = o3_short.get_validator() assert isinstance(validator, etree.XMLSchema)
def __init__(self, onixfile, profile): if hasattr(onixfile, 'name'): self.basename = basename(onixfile.name) else: self.basename = basename(onixfile) if hasattr(onixfile, 'seek'): onixfile.seek(0) self._onix_file = OnixFile(onixfile) self.onix = self._onix_file.xml_tree().getroot() # Get rid of namespaces for easier xpath search for elem in self.onix.getiterator(): if not hasattr(elem.tag, 'find'): continue i = elem.tag.find('}') if i >= 0: elem.tag = elem.tag[i + 1:] objectify.deannotate(self.onix, cleanup_namespaces=True) self.spec = yaml.load(open(profile, 'rb')) name, ext = splitext(basename(profile)) self.name = name.upper() self.urls_checked = set() self.errors = [] self.warnings = []
def validate(infile, schemas=('xsd', )): """Validate an ONIX file. :param infile: File or path to file :type infile: file or str :param schemas: Iterable with paths to custom valdation profiles :type schemas: collections.Iterable[str] :return: List of `Message` objects (invalid ONIX) or empty list (valid ONIX) :rtype: list[Message] """ if hasattr(infile, 'name'): filename = basename(infile.name) else: filename = basename(infile) try: onix_file = OnixFile(infile) except (OnixError, XMLSyntaxError) as e: return [Message.from_exception(e, filename)] messages = [] for s in schemas: if s in ( 'xsd', 'rng', ): try: validator = onix_file.get_validator(s) except OnixError as e: messages.append(Message.from_exception(e, filename)) continue validator(onix_file.xml_tree()) errors = validator.error_log msg = Message.from_logentry messages.extend([msg(err, filename) for err in errors]) for s in schemas: if s in ('google', 'biblon'): if s == 'google': profile = schema.GOOGLE_O30_YML_REFERENCE elif s == 'biblon': profile = schema.BIBLON_O30_YML_REFERENCE validator = OnixFix(infile, profile) validator.validate() messages.extend(validator.errors) return messages
def test_onix_file_with_path(): ofile = OnixFile(data.VALID_ONIX3_REF) assert isinstance(ofile, OnixFile) assert isinstance(ofile.xml_tree(), etree._ElementTree)
def test_message_from_logentry(): onix_file = OnixFile(data.INVALID_ONIX3_REF) validator = onix_file.get_validator() validator(onix_file.xml_tree()) msg = Message.from_logentry(validator.error_log[0]) assert isinstance(msg, Message)
def test_get_validator_o2(): o2_ref = OnixFile(data.VALID_ONIX2_REF) validator = o2_ref.get_validator() assert isinstance(validator, etree.XMLSchema)
def test_onix_file_with_file_obj(): with open(data.VALID_ONIX3_REF, 'rb') as infile: ofile = OnixFile(infile) assert isinstance(ofile.xml_tree(), etree._ElementTree) assert isinstance(ofile, OnixFile)