def write_error(self, status_code, **kwargs): tb = "" ex = kwargs if "exception" in kwargs: #: :type: Exception ex = kwargs["exception"] if isinstance(ex, Failure): tb = traceback.format_tb( kwargs["exception"].getTracebackObject()) kwargs["exception"].printTraceback() else: self.plugin.logger.error("%s" % ex) tb = ex.message try: self.render("generic.html", _title="HTTP %s" % self.get_status(), show_breadcrumbs=False, content=""" <div class="ui red segment"> HTTP %s - %s <br /> <pre class="ui segment">%s</pre> </div> """ % (self.get_status(), escape.xhtml_escape( str(ex)), escape.xhtml_escape("\n".join(tb))), nav_items={}) except Exception as e: self.set_status(status_code) self.finish("Error: %s" % e)
def write_error(self, status_code, **kwargs): tb = "" ex = kwargs if "exception" in kwargs: #: :type: Exception ex = kwargs["exception"] if isinstance(ex, Failure): tb = traceback.format_tb( kwargs["exception"].getTracebackObject() ) kwargs["exception"].printTraceback() else: self.plugin.logger.error("%s" % ex) tb = ex.message try: self.render( "generic.html", _title="HTTP %s" % self.get_status(), show_breadcrumbs=False, content=""" <div class="ui red segment"> HTTP %s - %s <br /> <pre class="ui segment">%s</pre> </div> """ % (self.get_status(), escape.xhtml_escape(str(ex)), escape.xhtml_escape("\n".join(tb))), nav_items={} ) except Exception as e: self.set_status(status_code) self.finish("Error: %s" % e)
def insert_embeds(self, _, html, embeds): """ Inserts embedded stuff into the html """ top = [] bottom = [] unique = defaultdict(set) if embeds['css_files']: link = '<link href="{0}" type="text/css" rel="stylesheet" />' for path in imap(self.static_url, embeds['css_files']): if path not in unique['css_files']: unique['css_files'].add(path) top.append(link.format(escape.xhtml_escape(path))) if embeds['css_inline']: style = '<style type="text/css">\n{0}\n</style>' top.append(style.format('\n'.join(embeds['css_inline']))) if embeds['heads']: top.extend(embeds['heads']) if embeds['js_files']: script = '<script src="{0}" type="text/javascript"></script>' for path in imap(self.static_url, embeds['js_files']): if path not in unique['js_files']: unique['js_files'].add(path) bottom.append(script.format(escape.xhtml_escape(path))) if embeds['js_inline']: script = '<script type="text/javascript">\n//<![CDATA[\n{0}\n//]]>\n</script>' bottom.append(script.format('\n'.join(embeds['js_inline']))) parts = [] if top: sloc = html.index('</head>') parts.append(html[:sloc]) parts.extend(top) parts.append(html[sloc:]) if bottom: if parts: # use last part fragment = parts.pop() else: fragment = html sloc = fragment.rindex('</body>') parts.append(fragment[:sloc]) parts.extend(bottom) parts.append(fragment[sloc:]) if parts: return '\n'.join(parts) else: return html
def test_xhtml(self): self.assertEqual( escape.xhtml_escape("abc42"), "abc42" ) self.assertEqual( escape.xhtml_escape("<>"), "<>" ) self.assertEqual( escape.xhtml_escape("\"'"), ""'" )
def _render_parts(self, value, parts=[]): if isinstance(value, basestring): parts.append(escape.xhtml_escape(value)) elif isinstance(value, int) or isinstance(value, long): parts.append(str(value)) elif isinstance(value, datetime.datetime): parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z")) elif isinstance(value, dict): for name, subvalue in value.iteritems(): if not isinstance(subvalue, list): subvalue = [subvalue] for subsubvalue in subvalue: parts.append('<%s>' % escape.utf8(name)) self._render_parts(subsubvalue, parts) parts.append('</%s>' % escape.utf8(name)) else: raise Exception("Unknown S3 value type %r", value)
def _render_parts(self, value, parts=[]): if isinstance(value, basestring): parts.append(escape.xhtml_escape(value)) elif isinstance(value, int) or isinstance(value, long): parts.append(str(value)) elif isinstance(value, datetime.datetime): parts.append(value.strftime("%Y-%m-%dT%H:%M:%S.000Z")) elif isinstance(value, dict): for name, subvalue in value.iteritems(): if not isinstance(subvalue, list): subvalue = [subvalue] for subsubvalue in subvalue: parts.append('<' + escape.utf8(name) + '>') self._render_parts(subsubvalue, parts) parts.append('</' + escape.utf8(name) + '>') else: raise Exception("Unknown S3 value type %r", value)
def test_xhtml_escape(self): xml = "<" result = xhtml_escape(xml) self.assertEqual(result, "<")