Exemple #1
0
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)
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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)
Exemple #5
0
def test_get_validator_o2():
    o2_ref = OnixFile(data.VALID_ONIX2_REF)
    validator = o2_ref.get_validator()
    assert isinstance(validator, etree.XMLSchema)