def validate_xml(self): # First, create a full XML doc to validate doc = get_base_open511_element() el = self.get_validation_xml() if hasattr(self, 'get_validation_xml') else self.xml_elem container = etree.Element(pluralize(el.tag)) container.append(el) doc.append(container) doc.set('version', settings.OPEN511_DEFAULT_VERSION) # Then run it through schema validate(doc)
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)
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)
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)
def get_events(self, url=None, **kwargs): params = { 'jurisdiction': 'test.open511.org' } if url is not None and 'jurisdiction=test.open511.org' in url: params = {} headers = { 'Accept': 'application/xml' } headers.update(kwargs.pop('headers', {})) params.update(kwargs) full_url = EVENTS_URL if url: full_url = urljoin(full_url, url) full_url += '&' if '?' in full_url else '?' full_url += urlencode(params) resp = self.get(full_url, headers=headers) assert resp.status_code == 200 open511_el = etree.fromstring(resp.content) assert open511_el.tag == 'open511' validate(open511_el) return open511_el