Ejemplo n.º 1
0
def test_validation_with_file_obj_and_schema_name():
    with open(data.VALID_GOOGLE_ONIX_30_SAMPLE, 'rb') as ofile:
        errors = onixcheck.validate(
            ofile,
            schemas=('google',)
        )
    assert len(errors) == 2
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):

        singleton_instance = OnixFile.objects.get(id=1)
        file_serializer = XMLFileSerializer(instance=singleton_instance,
                                            data=request.data)

        if (file_serializer.is_valid()):

            onix_file = request.data['onix_file']
            onix_errors = onixcheck.validate(onix_file)
            if len(onix_errors) > 0:
                file_serializer.save()
                print("Onix Errors")
                return Response(onix_errors,
                                status=status.HTTP_400_BAD_REQUEST)

            else:
                file_serializer.save()
                print(OnixFile.load())
                return Response(OnixFile.load().__str__(),
                                status=status.HTTP_201_CREATED)

        else:
            return Response(file_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
    def test_onix_file(self):
        byte_str = io.BytesIO(
            requests.get(TestOnixParser.TEST_ONIX_FEED_URL).content)

        with tempfile.NamedTemporaryFile() as temp:
            temp.write(byte_str.getvalue())
            temp.flush()
            errors = onixcheck.validate(temp.name)

        for error in errors:
            print(error.short)

        self.assertTrue(len(errors) == 0)
Ejemplo n.º 4
0
    def __init__(self, filename):
        """
        1. Checks if there are any errors in the ONIX File 
        2. Returns an etree object called products which allows you to parse the file
        """
        errors = onixcheck.validate(filename)
        for error in errors:
            print(error.short)

        if errors:
            print(
                "Your ONIX Record has a mistake. Kindly check the ONIX File again before parsing."
            )
            exit(0)

        tree = etree.parse(filename)

        self.products = tree.xpath('/ONIXMessage/Product')
        self.onix_records = [[]]
Ejemplo n.º 5
0
def test_validation_with_file_path():
    errors = onixcheck.validate(data.VALID_ONIX2_REF)
    assert errors == []
Ejemplo n.º 6
0
def test_validation_with_file_obj_and_schema_name():
    with open(data.VALID_GOOGLE_ONIX_30_SAMPLE, 'rb') as ofile:
        errors = onixcheck.validate(ofile, schemas=('google', ))
    assert len(errors) == 2
Ejemplo n.º 7
0
def test_validation_with_file_path_and_schema_name():
    errors = onixcheck.validate(data.VALID_GOOGLE_ONIX_30_SAMPLE,
                                schemas=('google', ))
    assert len(errors) == 2
Ejemplo n.º 8
0
def test_validation_with_file_obj():
    with open(data.VALID_ONIX3_REF, 'rb') as onix_file:
        errors = onixcheck.validate(onix_file)
    assert not errors
Ejemplo n.º 9
0
def test_validation_with_file_path():
    errors = onixcheck.validate(data.VALID_ONIX2_REF)
    assert errors == []
Ejemplo n.º 10
0
def test_validation_with_file_path_and_schema_name():
    errors = onixcheck.validate(
        data.VALID_GOOGLE_ONIX_30_SAMPLE,
        schemas=('google',)
    )
    assert len(errors) == 2
Ejemplo n.º 11
0
def test_validation_with_file_obj():
    with open(data.VALID_ONIX3_REF, 'rb') as onix_file:
        errors = onixcheck.validate(onix_file)
    assert not errors
Ejemplo n.º 12
0
def main(argv=None):
    """Command line app main function.

    :param list | None argv: Overrides command options (for libuse or testing)
    """
    parser = create_parser()

    args = parser.parse_args() if argv is None else parser.parse_args(argv)

    schemas = ('xsd',) if not args.schemas else tuple(args.schemas)

    if args.debug:
        logging.basicConfig(
            level=logging.DEBUG,
            format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
        )
        print('DEBUG logging enabled.')

    try:
        import win_unicode_console
        win_unicode_console.enable()
        log.debug('Running with win-unicode-console patch')
    except Exception:
        pass

    log.debug('TYPE of path: %s' % type(args.path))
    # validate current working dir
    if not args.infile and not args.path:
        args.path = os.getcwdu()
        log.debug('NEW TYPE of path: %s' % type(args.path))

    all_valid = True

    if args.infile:
        log.debug('TYPE of infile.name: %s' % type(args.infile.name))
        print('Validating: %s' % args.infile.name)
        messages = validate(args.infile, schemas)
        is_valid = messages == []
        if is_valid:
            print('VALID - No errors found')
        else:
            print('INVALID - errors found:', file=sys.stderr)
            all_valid = False
            for msg in messages:
                if args.debug:
                    print(msg.__str__(), file=sys.stderr)
                else:
                    print(msg.short, file=sys.stderr)

    if args.path:
        tree_or_dir = 'tree' if args.recursive else 'dir'
        print()
        print('Validating all files in %s %s' % (tree_or_dir, args.path))

        for onix_file_path in iter_files(args.path, args.ext, args.recursive):
            print()
            print('Validating: %s' % onix_file_path)
            with open(onix_file_path, 'rb') as onix_file:
                messages = validate(onix_file, schemas)
                is_valid = messages == []

            if is_valid:
                print('VALID - No errors found')
            else:
                print('INVALID - errors found:', file=sys.stderr)
                all_valid = False
                for msg in messages:
                    if args.debug:
                        print(msg.__str__(), file=sys.stderr)
                    else:
                        print(msg.short, file=sys.stderr)
    if all_valid:
        return 0
    else:
        return 1
Ejemplo n.º 13
0
import onixcheck
import xml.etree.ElementTree as ET
from lxml import etree

# File 
FILE = 'data/SampleONIX.xml'

"""
Step 1: Check if there are any errors in the XML file
"""
errors = onixcheck.validate(FILE)
for error in errors:
    print(error.short)

# Exit the script in case there are errors
if errors:
    print("Kindly use a correct XML File to parse")
    exit(0)

"""
Step 2: Parse the XML file to get all relevant features from the dataset
"""
tree = etree.parse(FILE)

products = tree.xpath('/ONIXMessage/Product')

# onix_record = [['Title','Publisher','City Of Publication','Country of Publication','isbn10', 'isbn13', 'Boocover Link', 'Language']]
onix_records = [[]]

for product in products:
    product = etree.fromstring(etree.tostring(product))
Ejemplo n.º 14
0
def main(argv=None):
    """Command line app main function.

    :param list | None argv: Overrides command options (for libuse or testing)
    """
    parser = create_parser()

    args = parser.parse_args() if argv is None else parser.parse_args(argv)

    schemas = ('xsd',) if not args.schemas else tuple(args.schemas)

    if args.debug:
        logging.basicConfig(
            level=logging.DEBUG,
            format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
        )
        print('DEBUG logging enabled.')

    try:
        import win_unicode_console
        win_unicode_console.enable()
        log.debug('Running with win-unicode-console patch')
    except Exception:
        pass

    log.debug('TYPE of path: %s' % type(args.path))
    # validate current working dir
    if not args.infile and not args.path:
        args.path = getcwd()
        log.debug('NEW TYPE of path: %s' % type(args.path))

    all_valid = True

    if args.infile:
        log.debug('TYPE of infile.name: %s' % type(args.infile.name))
        print('Validating: %s' % args.infile.name)
        messages = validate(args.infile, schemas)
        is_valid = messages == []
        if is_valid:
            print('VALID - No errors found')
        else:
            print('INVALID - errors found:', file=sys.stderr)
            all_valid = False
            for msg in messages:
                if args.debug:
                    print(msg.__str__(), file=sys.stderr)
                else:
                    print(msg.short, file=sys.stderr)

    if args.path:
        tree_or_dir = 'tree' if args.recursive else 'dir'
        print()
        print('Validating all files in %s %s' % (tree_or_dir, args.path))

        for onix_file_path in iter_files(args.path, args.ext, args.recursive):
            print()
            print('Validating: %s' % onix_file_path)
            with open(onix_file_path, 'rb') as onix_file:
                messages = validate(onix_file, schemas)
                is_valid = messages == []

            if is_valid:
                print('VALID - No errors found')
            else:
                print('INVALID - errors found:', file=sys.stderr)
                all_valid = False
                for msg in messages:
                    if args.debug:
                        print(msg.__str__(), file=sys.stderr)
                    else:
                        print(msg.short, file=sys.stderr)
    if all_valid:
        return 0
    else:
        return 1