Skip to content

Instantly share code, notes, and snippets.

@vmagamedov
Created May 14, 2018 16:13
Show Gist options
  • Save vmagamedov/870357a08fc6b95cdb3cc0d30ca1e5d5 to your computer and use it in GitHub Desktop.
Save vmagamedov/870357a08fc6b95cdb3cc0d30ca1e5d5 to your computer and use it in GitHub Desktop.
Werkzeug debugger for Pylons/Paste apps
import pylons
from webob import Request
from paste.registry import RegistryManager
from werkzeug.debug import DebuggedApplication
from werkzeug.serving import run_simple
def pylons_controller(environ, start_response):
1/0
def pylons_app(app):
def wrapper(environ, start_response):
environ['paste.registry'].register(pylons.request, Request(environ))
return app(environ, start_response)
return wrapper
class PylonsDebugger(object):
def __init__(self, app):
self.app = app
self.debug_app = DebuggedApplication(self.catch_errors, evalex=True)
self.cache = {}
def catch_errors(self, environ, start_response):
try:
return self.app(environ, start_response)
except Exception:
state = environ['paste.registry'].reglist[-1]
environ['restoration.vars'].extend(state.values())
raise
def __call__(self, environ, start_response):
request = Request(environ)
if request.GET.get('__debugger__') == 'yes':
frm = request.GET.get('frm')
if frm:
frame_id = int(frm)
if frame_id in self.cache:
registry = environ['paste.registry']
for var, obj in self.cache[frame_id]:
registry.register(var, obj)
return self.debug_app(environ, start_response)
rest_vars = environ['restoration.vars'] = []
old_frame_ids = list(self.debug_app.frames.keys())
app_iter = self.debug_app(environ, start_response)
chunks = list(app_iter)
if hasattr(app_iter, 'close'):
app_iter.close()
new_frame_ids = set(self.debug_app.frames).difference(old_frame_ids)
for frame_id in new_frame_ids:
self.cache[frame_id] = rest_vars
return chunks
def main():
app = pylons_app(pylons_controller)
app = PylonsDebugger(app)
app = RegistryManager(app, streaming=True)
run_simple('', 5000, app)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment