Initial structure for the debugtoolbar

This commit is contained in:
mvantellingen
2011-02-01 23:36:32 +01:00
commit a060d30bb7
7 changed files with 101 additions and 0 deletions

0
flaskext/__init__.py Normal file
View File

View File

@@ -0,0 +1,57 @@
import os
from flask import request_finished
from flask.helpers import send_from_directory
from jinja2 import Environment, PackageLoader
from werkzeug.routing import Rule, Submount
from .panels.logger import handler
from .views import views_module
def replace_insensitive(string, target, replacement):
"""
Similar to string.replace() but is case insensitive
Code borrowed from: http://forums.devshed.com/python-programming-11/case-insensitive-string-replace-490921.html
"""
no_case = string.lower()
index = no_case.rfind(target.lower())
if index >= 0:
return string[:index] + replacement + string[index + len(target):]
else: # no results so return the original string
return string
class DebugToolbar(object):
_static_dir = os.path.join(os.path.dirname(__file__), 'static')
def __init__(self, app):
self.jinja_env = Environment(loader=PackageLoader(__name__, 'templates'))
request_finished.connect(self.process_response, app)
app.url_map.add(Submount('/_debug_toolbar', [
Rule('/static/<path:filename>', endpoint='_debug_toolbar.static'),
Rule('/css/main.css', endpoint='_debug_toolbar.example')
]))
app.register_module(views_module)
app.view_functions['_debug_toolbar.static'] = self.send_static_file
def send_static_file(self, filename):
return send_from_directory(self._static_dir, filename)
def render_toolbar(self):
template = self.jinja_env.get_template('base.html')
content = template.render()
return content
def process_response(self, sender, response):
if response.is_sequence:
response_html = response.data
toolbar_html = self.render_toolbar()
response.response = [
replace_insensitive(
response_html,
'</body>',
toolbar_html + '</body>')]

View File

View File

@@ -0,0 +1,35 @@
"""
Logger panel (taken from django-debug-toolbar)
"""
import threading
import logging
class ThreadTrackingHandler(logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
self.records = {} # a dictionary that maps threads to log records
def emit(self, record):
self.get_records().append(record)
def get_records(self, thread=None):
"""
Returns a list of records for the provided thread, of if none is provided,
returns a list for the current thread.
"""
if thread is None:
thread = threading.currentThread()
if thread not in self.records:
self.records[thread] = []
return self.records[thread]
def clear_records(self, thread=None):
if thread is None:
thread = threading.currentThread()
if thread in self.records:
del self.records[thread]
handler = ThreadTrackingHandler()
logging.root.setLevel(logging.NOTSET)
logging.root.addHandler(handler)

View File

@@ -0,0 +1,9 @@
from flask import Module
__all__ = ['views_module']
mod = views_module = Module(__name__)
@mod.endpoint('_debug_toolbar.example')
def example_view():
return "example"