def convert(): doc_content = _load_document() if not isinstance(doc_content, unicode): doc_content = doc_content.decode('utf8') doc, doc_format = deserialize(doc_content) format = request.values['format'] result = open511_convert(doc, format) format_info = FORMATS[format] return Response(result, mimetype=format_info.content_type)
def load_path(source): if source == '-': content = sys.stdin.read() elif re.match(r'https?://', source): content = urllib2.urlopen(source).read() else: with open(source) as f: content = f.read() return deserialize(content)
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 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)