aboutsummaryrefslogtreecommitdiffstats
path: root/debug_toolbar/utils/__init__.py
diff options
context:
space:
mode:
authorDavid Cramer2011-03-31 11:42:23 -0700
committerDavid Cramer2011-03-31 11:42:23 -0700
commit3f578cf684b7e3b9a20d9c777950c28c44db074a (patch)
treed7573707ccdfbd0627d85c9a2b6f90d62f7537fe /debug_toolbar/utils/__init__.py
parentf492b56c8200eebb77b8023ab386c9ef412cc06b (diff)
downloaddjango-debug-toolbar-3f578cf684b7e3b9a20d9c777950c28c44db074a.tar.bz2
Add utilities to inject and monitor functions. Change DB tracking to use new injection method on BaseDatabaseWrapper.cursor
Diffstat (limited to 'debug_toolbar/utils/__init__.py')
-rw-r--r--debug_toolbar/utils/__init__.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/debug_toolbar/utils/__init__.py b/debug_toolbar/utils/__init__.py
index e69de29..61bb717 100644
--- a/debug_toolbar/utils/__init__.py
+++ b/debug_toolbar/utils/__init__.py
@@ -0,0 +1,68 @@
+import os.path
+import django
+import SocketServer
+
+from django.conf import settings
+from django.views.debug import linebreak_iter
+
+# Figure out some paths
+django_path = os.path.realpath(os.path.dirname(django.__file__))
+socketserver_path = os.path.realpath(os.path.dirname(SocketServer.__file__))
+
+def ms_from_timedelta(td):
+ """
+ Given a timedelta object, returns a float representing milliseconds
+ """
+ return (td.seconds * 1000) + (td.microseconds / 1000.0)
+
+def tidy_stacktrace(strace):
+ """
+ Clean up stacktrace and remove all entries that:
+ 1. Are part of Django (except contrib apps)
+ 2. Are part of SocketServer (used by Django's dev server)
+ 3. Are the last entry (which is part of our stacktracing code)
+ """
+ trace = []
+ for s in strace[:-1]:
+ s_path = os.path.realpath(s[0])
+ if getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('HIDE_DJANGO_SQL', True) \
+ and django_path in s_path and not 'django/contrib' in s_path:
+ continue
+ if socketserver_path in s_path:
+ continue
+ trace.append((s[0], s[1], s[2], s[3]))
+ return trace
+
+def get_template_info(source, context_lines=3):
+ line = 0
+ upto = 0
+ source_lines = []
+ before = during = after = ""
+
+ origin, (start, end) = source
+ template_source = origin.reload()
+
+ for num, next in enumerate(linebreak_iter(template_source)):
+ if start >= upto and end <= next:
+ line = num
+ before = template_source[upto:start]
+ during = template_source[start:end]
+ after = template_source[end:next]
+ source_lines.append((num, template_source[upto:next]))
+ upto = next
+
+ top = max(1, line - context_lines)
+ bottom = min(len(source_lines), line + 1 + context_lines)
+
+ context = []
+ for num, content in source_lines[top:bottom]:
+ context.append({
+ 'num': num,
+ 'content': content,
+ 'highlight': (num == line),
+ })
+
+ return {
+ 'name': origin.name,
+ 'context': context,
+ } \ No newline at end of file