1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
from cStringIO import StringIO
from traceback import print_exc
from pprint import pformat
import pdb
from mako.template import Template
from webob import Response
from .resources import (pecan_image, shcore, shbrushpython, shcorecss,
shthemedefault)
debug_template_raw = '''<html>
<head>
<title>Pecan - Application Error</title>
<link rel="stylesheet" type="text/css" href="${shcorecss}" />
<link rel="stylesheet" type="text/css" href="${shthemedefault}" />
<script type="text/javascript" src="${shcore}">
/**
* SyntaxHighlighter
* http://alexgorbatchev.com/SyntaxHighlighter
*
* SyntaxHighlighter is donationware. If you are using it, please donate.
* http://alexgorbatchev.com/SyntaxHighlighter/donate.html
*
* @version
* 3.0.83 (July 02 2010)
*
* @copyright
* Copyright (C) 2004-2010 Alex Gorbatchev.
*
* @license
* Dual licensed under the MIT and GPL licenses.
*/
</script>
<script type="text/javascript" src="${shbrushpython}">
/**
* SyntaxHighlighter
* http://alexgorbatchev.com/SyntaxHighlighter
*
* SyntaxHighlighter is donationware. If you are using it, please donate.
* http://alexgorbatchev.com/SyntaxHighlighter/donate.html
*
* @version
* 3.0.83 (July 02 2010)
*
* @copyright
* Copyright (C) 2004-2010 Alex Gorbatchev.
*
* @license
* Dual licensed under the MIT and GPL licenses.
*/
</script>
<style type="text/css">
body {
color: #000;
background: #FFF;
font-family: 'Helvetica Neue', 'Helvetica', 'Verdana', sans-serif;
font-size: 12px;
padding: 0;
margin: 0;
}
a {
color: #FAFF78;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Helvetica', sans-serif;
}
h1 {
margin: 0;
padding: .75em 1.5em 1em 1.5em;
color: #F90;
font-size: 14px;
font-weight: bold;
}
h1 img {
padding-right: 5px;
}
h2 {
color: #311F00;
}
header {
width: 100%;
background: #311F00;
}
div#error-content {
padding: 0 2em;
}
.syntaxhighlighter a,
.syntaxhighlighter div,
.syntaxhighlighter code,
.syntaxhighlighter table,
.syntaxhighlighter table td,
.syntaxhighlighter table tr,
.syntaxhighlighter table tbody,
.syntaxhighlighter table thead,
.syntaxhighlighter table caption,
.syntaxhighlighter textarea {
font-family: monospace !important;
}
.syntaxhighlighter .container {
background: #FDF6E3 !important;
padding: 1em !important;
}
.syntaxhighlighter .container .line {
background: #FDF6E3 !important;
}
.syntaxhighlighter .container .line .python.string {
color: #C70 !important;
}
#debug {
background: #FDF6E3;
padding: 10px !important;
margin-top: 10px;
font-family: monospace;
}
</style>
<script type="text/javascript">
SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all()
</script>
<script type="text/javascript">
function get_request() {
/* ajax sans jquery makes me sad */
var request = false;
// Mozilla/Safari
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
return request;
}
function debug_request(btn) {
btn.disabled = true;
request = get_request();
request.open('GET', '/__pecan_initiate_pdb__', true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
btn.disabled = false;
}
}
request.send('');
/* automatically timeout after 5 minutes, re-enabling the button */
setTimeout(function() {
request.abort();
}, 5 * 60 * 1000);
}
</script>
</head>
<body>
<header>
<h1>
<img style="padding-top: 7px"
align="center" alt="pecan logo"
height="25"
src="${pecan_image}" />
application error
</h1>
</header>
<div id="error-content">
<p>
<b>To disable this interface, set </b>
<pre class="brush: python">conf.app.debug = False</pre>
</p>
<h2>Traceback</h2>
<div id="traceback">
<pre class="brush: python">${traceback}</pre>
</div>
% if not debugging:
<b>Want to debug this request?</b>
<div id="debug">
You can <button onclick="debug_request(this)">
repeat this request
</button> with a Python debugger breakpoint.
</div>
% endif
<h2>WSGI Environment</h2>
<div id="environ">
<pre class="brush: python">${environment}</pre>
</div>
</div>
</body>
</html>
'''
debug_template = Template(debug_template_raw)
__debug_environ__ = None
class PdbMiddleware(object):
def __init__(self, app, debugger):
self.app = app
self.debugger = debugger
def __call__(self, environ, start_response):
try:
return self.app(environ, start_response)
except:
self.debugger()
class DebugMiddleware(object):
"""A WSGI middleware that provides debugging assistance for development
environments.
To enable the debugging middleware, simply set the ``debug`` flag to
``True`` in your configuration file::
app = {
...
'debug': True,
...
}
Once enabled, the middleware will automatically catch exceptions raised by
your application, and display the Python stack trace and WSGI environment
in your browser for easy debugging.
To further aid in debugging, the middleware includes the ability to repeat
the offending request, automatically inserting a breakpoint, and dropping
your console into the Python debugger, ``pdb``.
For more information, refer to the `documentation for pdb
<http://docs.python.org/library/pdb.html>`_ available on the Python
website.
:param app: the application to wrap.
:param debugger: a callable to start debugging, defaulting to the Python
debugger, ``pdb``.
"""
def __init__(self, app, debugger=pdb.post_mortem):
self.app = app
self.debugger = debugger
def __call__(self, environ, start_response):
assert not environ['wsgi.multiprocess'], (
"The DebugMiddleware middleware is not usable in a "
"multi-process environment")
if environ.get('paste.testing'):
return self.app(environ, start_response)
# initiate a PDB session if requested
global __debug_environ__
debugging = environ['PATH_INFO'] == '/__pecan_initiate_pdb__'
if debugging:
PdbMiddleware(self.app, self.debugger)(
__debug_environ__, start_response
)
environ = __debug_environ__
try:
return self.app(environ, start_response)
except:
# save the environ for debugging
if not debugging:
__debug_environ__ = environ
# get a formatted exception
out = StringIO()
print_exc(file=out)
# get formatted WSGI environment
formatted_environ = pformat(environ)
# render our template
result = debug_template.render(
traceback=out.getvalue(),
environment=formatted_environ,
pecan_image=pecan_image,
shcore=shcore,
shbrushpython=shbrushpython,
shcorecss=shcorecss,
shthemedefault=shthemedefault,
debugging=debugging
)
# construct and return our response
response = Response()
response.status_int = 400
response.unicode_body = result
return response(environ, start_response)
|