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 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 add_message(self, message, path, el=None, level='ERROR'): assert level in ( 'ERROR', 'WARNING', ) if el is not None and el.sourceline is not None: location = '%s' % el.sourceline else: location = path location = '%s:%s' % (self.basename, location) msg = Message(level=level, validator=self.name, location=location, message=message, error_type='SPECIFICATION ERROR') if level == 'ERROR': self.errors.append(msg) elif level == 'WARNING': self.warnings.append(msg)
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)