Beispiel #1
0
def validator():
    ctx = {
        'conversion_formats': FORMATS_LIST
    }
    if request.method == 'GET' and 'url' not in request.args:
        return render_template('validator.html', **ctx)
    else:
        try:
            doc_content = _load_document()
        except FetchError as e:
            ctx['fetch_error'] = unicode(e)
            return render_template('validator.html', **ctx)
        try:
            if not isinstance(doc_content, unicode):
                doc_content = doc_content.decode('utf8')
            doc, doc_format = deserialize(doc_content)
        except Exception as e:
            ctx.update(doc_content=doc_content, deserialize_error=unicode(e))
            return render_template('validator.html', **ctx)

        if doc_format == 'json':
            json_doc = doc
            try:
                xml_doc = json_doc_to_xml(json_doc, custom_namespace='http://validator.open511.com/custom-field')
            except Exception as e:
                ctx.update(error=unicode(e), doc_content=doc_content)
                return render_template('validator.html', **ctx)
        elif doc_format == 'xml':
            xml_doc = doc
            try:
                json_doc = xml_to_json(xml_doc)
            except:
                json_doc = 'Error generating JSON'
        else:
            raise NotImplementedError

        try:
            validate(xml_doc)
            success = True
        except Open511ValidationError as e:
            success = False
            error = unicode(e)
        ctx.update(
            success=success,
            error=None if success else error,
            xml_string=serialize(xml_doc),
            json_string=serialize(json_doc),
            doc_format=doc_format
        )
        return render_template('validator.html', **ctx)
Beispiel #2
0
def validate_cmdline():
    parser = argparse.ArgumentParser(description='Validate an Open511 document.')
    parser.add_argument('source', metavar='DOC', type=str,
        help='Document to validate: path, URL, or - to read from stdin')
    arguments = parser.parse_args()
    obj, obj_type = load_path(arguments.source)
    if obj_type == 'json':
        obj = json_doc_to_xml(obj, custom_namespace='http://validator.open511.org/custom-field')
    try:
        validate(obj)
    except Open511ValidationError as e:
        sys.stderr.write(unicode(e))
        sys.stderr.write("\n")
        sys.exit(1)
Beispiel #3
0
def validate_cmdline():
    parser = argparse.ArgumentParser(description='Validate an Open511 document.')
    parser.add_argument('source', metavar='DOC', type=str,
        help='Document to validate: path, URL, or - to read from stdin')
    arguments = parser.parse_args()
    obj, obj_type = load_path(arguments.source)
    if obj_type == 'json':
        obj = json_doc_to_xml(obj, custom_namespace='http://validator.open511.org/custom-field')
    try:
        validate(obj)
    except Open511ValidationError as e:
        sys.stderr.write(unicode(e))
        sys.stderr.write("\n")
        sys.exit(1)
Beispiel #4
0
def validator():
    ctx = {'conversion_formats': FORMATS_LIST}
    if request.method == 'GET' and 'url' not in request.args:
        return render_template('validator.html', **ctx)
    else:
        try:
            doc_content = _load_document()
        except FetchError as e:
            ctx['fetch_error'] = unicode(e)
            return render_template('validator.html', **ctx)
        try:
            if not isinstance(doc_content, unicode):
                doc_content = doc_content.decode('utf8')
            doc, doc_format = deserialize(doc_content)
        except Exception as e:
            ctx.update(doc_content=doc_content, deserialize_error=unicode(e))
            return render_template('validator.html', **ctx)

        if doc_format == 'json':
            json_doc = doc
            try:
                xml_doc = json_doc_to_xml(
                    json_doc,
                    custom_namespace='http://validator.open511.com/custom-field'
                )
            except Exception as e:
                ctx.update(error=unicode(e), doc_content=doc_content)
                return render_template('validator.html', **ctx)
        elif doc_format == 'xml':
            xml_doc = doc
            try:
                json_doc = xml_to_json(xml_doc)
            except:
                json_doc = 'Error generating JSON'
        else:
            raise NotImplementedError

        try:
            validate(xml_doc)
            success = True
        except Open511ValidationError as e:
            success = False
            error = unicode(e)
        ctx.update(success=success,
                   error=None if success else error,
                   xml_string=serialize(xml_doc),
                   json_string=serialize(json_doc),
                   doc_format=doc_format)
        return render_template('validator.html', **ctx)