1
# -*- test-case-name: twisted.web.test.test_web -*-
3
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
4
# See LICENSE for details.
7
from cStringIO import StringIO
9
from twisted.python import failure
20
def redirectTo(URL, request):
25
<meta http-equiv=\"refresh\" content=\"0;URL=%(url)s\">
27
<body bgcolor=\"#FFFFFF\" text=\"#000000\">
28
<a href=\"%(url)s\">click here</a>
33
class Redirect(resource.Resource):
37
def __init__(self, url):
38
resource.Resource.__init__(self)
41
def render(self, request):
42
return redirectTo(self.url, request)
44
def getChild(self, name, request):
47
class ChildRedirector(Redirect):
49
def __init__(self, url):
51
if ((url.find('://') == -1)
52
and (not url.startswith('..'))
53
and (not url.startswith('/'))):
54
raise ValueError("It seems you've given me a redirect (%s) that is a child of myself! That's not good, it'll cause an infinite redirect." % url)
55
Redirect.__init__(self, url)
57
def getChild(self, name, request):
59
if not newUrl.endswith('/'):
62
return ChildRedirector(newUrl)
65
from twisted.python import urlpath
67
class ParentRedirect(resource.Resource):
69
I redirect to URLPath.here().
72
def render(self, request):
73
return redirectTo(urlpath.URLPath.fromRequest(request).here(), request)
75
def getChild(self, request):
79
class DeferredResource(resource.Resource):
81
I wrap up a Deferred that will eventually result in a Resource
86
def __init__(self, d):
87
resource.Resource.__init__(self)
90
def getChild(self, name, request):
93
def render(self, request):
94
self.d.addCallback(self._cbChild, request).addErrback(
95
self._ebChild,request)
96
from twisted.web.server import NOT_DONE_YET
99
def _cbChild(self, child, request):
100
result = resource.getChildForRequest(child, request).render(request)
101
from twisted.web.server import NOT_DONE_YET
102
if result == NOT_DONE_YET:
105
request.write(result)
108
def _ebChild(self, reason, request):
109
request.processingFailed(reason)
114
<style type="text/css">
117
font-family: Verdana, Arial, helvetica, sans-serif;
122
font-family: Verdana, Arial, helvetica, sans-serif;
131
border-bottom: thin black dashed;
137
border-top: thin black dashed;
138
border-bottom: thin black dashed;
145
margin-bottom: 0.5em;
150
div.snippetHighlightLine {
155
font-family: "Courier New", courier, monotype;
160
font-family: "Courier New", courier, monotype;
164
border-collapse: collapse;
172
padding-right: 0.5em;
177
padding-right: 0.5em;
181
margin-bottom: 0.5em;
226
font-family: "Courier New", courier, monotype;
239
return htmlReprTypes.get(type(x), htmlUnknown)(x)
245
rx = "<repr failed! %s instance at %s>" % (x.__class__, id(x))
249
return '<code>'+html.escape(saferepr(x))+'</code>'
254
w('<div class="dict"><span class="heading">Dictionary instance @ %s</span>' % hex(id(d)))
255
w('<table class="dict">')
256
for k, v in d.items():
258
if k == '__builtins__':
259
v = 'builtin dictionary'
260
w('<tr><td class="dictKey">%s</td><td class="dictValue">%s</td></tr>' % (htmlrepr(k), htmlrepr(v)))
267
w('<div class="list"><span class="heading">List instance @ %s</span>' % hex(id(l)))
269
w('<div class="listItem">%s</div>' % htmlrepr(i))
274
if hasattr(i, "__html__"):
277
s = html.escape(saferepr(i))
278
return '''<div class="instance"><span class="instanceName">%s instance @ %s</span>
279
<span class="instanceRepr">%s</span></div>
280
''' % (i.__class__, hex(id(i)), s)
283
return html.escape(saferepr(s))
286
return ('<div class="function">' +
287
html.escape("function %s in file %s at line %s" %
288
(f.__name__, f.func_code.co_filename,
289
f.func_code.co_firstlineno))+
292
htmlReprTypes = {types.DictType: htmlDict,
293
types.ListType: htmlList,
294
types.InstanceType: htmlInst,
295
types.StringType: htmlString,
296
types.FunctionType: htmlFunc}
300
def htmlIndent(snippetLine):
301
ret = string.replace(string.replace(html.escape(string.rstrip(snippetLine)),
303
'\t', ' ')
306
def formatFailure(myFailure):
309
<p class="error">%s: %s</p>
313
<div class="location">%s, line %s in <span class="function">%s</span></div>
316
snippetLineHTML = """
317
<div class="snippetLine"><span class="lineno">%s</span><span class="code">%s</span></div>
320
snippetHighlightLineHTML = """
321
<div class="snippetHighlightLine"><span class="lineno">%s</span><span class="code">%s</span></div>
325
<tr class="varRow"><td class="varName">%s</td><td class="varValue">%s</td></tr>
328
if not isinstance(myFailure, failure.Failure):
329
return html.PRE(str(myFailure))
333
w('<a href="#tbend">')
334
w(exceptionHTML % (html.escape(str(myFailure.type)),
335
html.escape(str(myFailure.value))))
337
w('<div class="stackTrace">')
339
for method, filename, lineno, localVars, globalVars in myFailure.frames:
340
if filename == '<string>':
343
w('<div class="firstFrame">')
346
w('<div class="frame">')
347
w(frameHTML % (filename, lineno, method))
349
w('<div class="snippet">')
351
for snipLineNo in range(lineno-2, lineno+2):
352
snipLine = linecache.getline(filename, snipLineNo)
353
textSnippet += snipLine
354
snipLine = htmlIndent(snipLine)
355
if snipLineNo == lineno:
356
w(snippetHighlightLineHTML % (snipLineNo, snipLine))
358
w(snippetLineHTML % (snipLineNo, snipLine))
362
for name, var in localVars:
363
if name == 'self' and hasattr(var, '__dict__'):
364
usedVars = [ (key, value) for (key, value) in var.__dict__.items()
365
if re.search(r'\W'+'self.'+key+r'\W', textSnippet) ]
367
w('<div class="variables"><b>Self</b>')
368
w('<table class="variables">')
369
for key, value in usedVars:
370
w(variableHTML % (key, htmlrepr(value)))
374
# Local and global vars
375
for nm, varList in ('Locals', localVars), ('Globals', globalVars):
376
usedVars = [ (name, var) for (name, var) in varList
377
if re.search(r'\W'+name+r'\W', textSnippet) ]
379
w('<div class="variables"><b>%s</b><table class="variables">' % nm)
380
for name, var in usedVars:
381
w(variableHTML % (name, htmlrepr(var)))
385
w('</div>') # stacktrace
386
w('<a name="tbend"> </a>')
387
w(exceptionHTML % (html.escape(str(myFailure.type)),
388
html.escape(str(myFailure.value))))