from cStringIO import StringIO from twisted.python import failure import html import resource import linecache import string, re import types def redirectTo(URL, request): request.redirect(URL) return """ Click here. """ % {'url': URL} class Redirect(resource.Resource): def __init__(self, url): resource.Resource.__init__(self) self.url = url def render(self, request): return redirectTo(self.url, request) def htmlrepr(x): return htmlReprTypes.get(type(x), htmlUnknown)(x) def saferepr(x): try: rx = repr(x) except: rx = "" % (x.__class__, id(x)) return rx def htmlUnknown(x): return ''+html.escape(saferepr(x))+'' def htmlDict(d): io = StringIO() w = io.write w('' % id(d)) for k, v in d.items(): if k == '__builtins__': v = 'builtin dictionary' w('\n\n\n' % (htmlrepr(k), htmlrepr(v))) w('
Dictionary %d
%s%s
') return io.getvalue() def htmlList(l): io = StringIO() w = io.write w('' % id(l)) for i in l: w('\n' % htmlrepr(i)) w('
List %d
%s
\n') return io.getvalue() def htmlInst(i): if hasattr(i, "__html__"): s = i.__html__() else: s = ''+html.escape(saferepr(i))+'' return '''
%s instance @ 0x%x
%s
''' % (i.__class__, id(i), s) def htmlString(s): return html.escape(saferepr(s)) def htmlFunc(f): return ('' + html.escape("function %s in file %s at line %s" % (f.__name__, f.func_code.co_filename, f.func_code.co_firstlineno))+ '') htmlReprTypes = {types.DictType: htmlDict, types.ListType: htmlList, types.InstanceType: htmlInst, types.StringType: htmlString, types.FunctionType: htmlFunc} def formatFailure(myFailure): if not isinstance(myFailure, failure.Failure): return html.PRE(str(myFailure)) io = StringIO() w = io.write w("") w('' % (html.escape(str(myFailure.type)), html.escape(str(myFailure.value)))) line = 0 for method, filename, lineno, localVars, globalVars in myFailure.frames: # Cheat to make tracebacks shorter. if filename == '': continue # file, line number w('') # Self vars w('' % (["bbbbbb", "cccccc"][line % 2])) w('') w('' % (["bbbbbb", "cccccc"][line % 2])) # Local and global vars for nm, varList in ('Locals', localVars), ('Globals', globalVars): w('') w('') line = line + 1 w('' % (html.escape(str(myFailure.type)), html.escape(str(myFailure.value)))) w('
%s: %s
%s, line %s in %s
' % (["bbbbbb", "cccccc"][line % 2], filename, lineno, method)) snippet = '' for snipLineNo in range(lineno-2, lineno+2): snipLine = linecache.getline(filename, snipLineNo) snippet = snippet + snipLine snipLine = string.replace( string.replace(html.escape(string.rstrip(snipLine)), ' ',' '), '\t', '        ') if snipLineNo == lineno: color = 'bgcolor="#ffffff"' else: color = '' w('' % (color, snipLineNo,snipLine)) w('
%s%s
') for name, var in localVars: if name == 'self' and hasattr(var, '__dict__'): for key, value in var.__dict__.items(): if re.search(r'\W'+'self.'+key+r'\W', snippet): w('' '' % (key, htmlrepr(value))) w('
' 'Self' '
%s%s
' % nm) for name, var in varList: if re.search(r'\W'+name+r'\W', snippet): w('' '' % (name, htmlrepr(var))) w('
' '%s' '
%s%s
%s: %s
') return io.getvalue()