diff options
| author | Aymeric Augustin | 2013-11-15 22:43:59 +0100 |
|---|---|---|
| committer | Aymeric Augustin | 2013-11-15 22:43:59 +0100 |
| commit | 6334983458abd4380c21275d1229527778cf93a6 (patch) | |
| tree | 1b3e9304f93600fa7b38bfa8a6cc20b3857d7375 /debug_toolbar/utils | |
| parent | f0d0ddbada065ec0ff4fc64aed9d2f9ba48ba5a3 (diff) | |
| download | django-debug-toolbar-6334983458abd4380c21275d1229527778cf93a6.tar.bz2 | |
Continue moving panel-specific code within panels.
Structure the SQL and template panels as packages as they're growing.
Diffstat (limited to 'debug_toolbar/utils')
| -rw-r--r-- | debug_toolbar/utils/sql.py | 37 | ||||
| -rw-r--r-- | debug_toolbar/utils/tracking/__init__.py | 0 | ||||
| -rw-r--r-- | debug_toolbar/utils/tracking/db.py | 168 |
3 files changed, 0 insertions, 205 deletions
diff --git a/debug_toolbar/utils/sql.py b/debug_toolbar/utils/sql.py deleted file mode 100644 index 00728a3..0000000 --- a/debug_toolbar/utils/sql.py +++ /dev/null @@ -1,37 +0,0 @@ -from __future__ import unicode_literals - -import re - -from django.utils.html import escape - -import sqlparse -from sqlparse import tokens as T - - -class BoldKeywordFilter: - """sqlparse filter to bold SQL keywords""" - def process(self, stack, stream): - """Process the token stream""" - for token_type, value in stream: - is_keyword = token_type in T.Keyword - if is_keyword: - yield T.Text, '<strong>' - yield token_type, escape(value) - if is_keyword: - yield T.Text, '</strong>' - - -def reformat_sql(sql): - stack = sqlparse.engine.FilterStack() - stack.preprocess.append(BoldKeywordFilter()) # add our custom filter - stack.postprocess.append(sqlparse.filters.SerializerUnicode()) # tokens -> strings - return swap_fields(''.join(stack.run(sql))) - - -def swap_fields(sql): - expr = r'SELECT</strong> (...........*?) <strong>FROM' - subs = (r'SELECT</strong> ' - r'<a class="djDebugUncollapsed djDebugToggle" href="#">•••</a> ' - r'<a class="djDebugCollapsed djDebugToggle" href="#">\1</a> ' - r'<strong>FROM') - return re.sub(expr, subs, sql) diff --git a/debug_toolbar/utils/tracking/__init__.py b/debug_toolbar/utils/tracking/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/debug_toolbar/utils/tracking/__init__.py +++ /dev/null diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py deleted file mode 100644 index fd56ff9..0000000 --- a/debug_toolbar/utils/tracking/db.py +++ /dev/null @@ -1,168 +0,0 @@ -from __future__ import unicode_literals - -import sys - -import json -from threading import local -from time import time - -from django.template import Node -from django.utils.encoding import force_text -from django.utils import six - -from debug_toolbar.utils import tidy_stacktrace, get_template_info, get_stack -from debug_toolbar.utils import settings as dt_settings - - -class SQLQueryTriggered(Exception): - """Thrown when template panel triggers a query""" - pass - - -class ThreadLocalState(local): - def __init__(self): - self.enabled = True - - @property - def Wrapper(self): - if self.enabled: - return NormalCursorWrapper - return ExceptionCursorWrapper - - def recording(self, v): - self.enabled = v - - -state = ThreadLocalState() -recording = state.recording # export function - - -def CursorWrapper(*args, **kwds): # behave like a class - return state.Wrapper(*args, **kwds) - - -class ExceptionCursorWrapper(object): - """ - Wraps a cursor and raises an exception on any operation. - Used in Templates panel. - """ - def __init__(self, cursor, db, logger): - pass - - def __getattr__(self, attr): - raise SQLQueryTriggered() - - -class NormalCursorWrapper(object): - """ - Wraps a cursor and logs queries. - """ - - def __init__(self, cursor, db, logger): - self.cursor = cursor - # Instance of a BaseDatabaseWrapper subclass - self.db = db - # logger must implement a ``record`` method - self.logger = logger - - def _quote_expr(self, element): - if isinstance(element, six.string_types): - return "'%s'" % force_text(element).replace("'", "''") - else: - return repr(element) - - def _quote_params(self, params): - if not params: - return params - if isinstance(params, dict): - return dict((key, self._quote_expr(value)) - for key, value in params.items()) - return list(map(self._quote_expr, params)) - - def _decode(self, param): - try: - return force_text(param, strings_only=True) - except UnicodeDecodeError: - return '(encoded string)' - - def execute(self, sql, params=()): - start_time = time() - try: - return self.cursor.execute(sql, params) - finally: - stop_time = time() - duration = (stop_time - start_time) * 1000 - if dt_settings.CONFIG['ENABLE_STACKTRACES']: - stacktrace = tidy_stacktrace(reversed(get_stack())) - else: - stacktrace = [] - _params = '' - try: - _params = json.dumps(list(map(self._decode, params))) - except Exception: - pass # object not JSON serializable - - template_info = None - cur_frame = sys._getframe().f_back - try: - while cur_frame is not None: - if cur_frame.f_code.co_name == 'render': - node = cur_frame.f_locals['self'] - if isinstance(node, Node): - template_info = get_template_info(node.source) - break - cur_frame = cur_frame.f_back - except Exception: - pass - del cur_frame - - alias = getattr(self.db, 'alias', 'default') - conn = self.db.connection - # HACK: avoid imports - if conn: - engine = conn.__class__.__module__.split('.', 1)[0] - else: - engine = 'unknown' - - params = { - 'engine': engine, - 'alias': alias, - 'sql': self.db.ops.last_executed_query( - self.cursor, sql, self._quote_params(params)), - 'duration': duration, - 'raw_sql': sql, - 'params': _params, - 'stacktrace': stacktrace, - 'start_time': start_time, - 'stop_time': stop_time, - 'is_slow': duration > dt_settings.CONFIG['SQL_WARNING_THRESHOLD'], - 'is_select': sql.lower().strip().startswith('select'), - 'template_info': template_info, - } - - if engine == 'psycopg2': - # If an erroneous query was ran on the connection, it might - # be in a state where checking isolation_level raises an - # exception. - try: - iso_level = conn.isolation_level - except conn.InternalError: - iso_level = 'unknown' - params.update({ - 'trans_id': self.logger.get_transaction_id(alias), - 'trans_status': conn.get_transaction_status(), - 'iso_level': iso_level, - 'encoding': conn.encoding, - }) - - # We keep `sql` to maintain backwards compatibility - self.logger.record(**params) - - def executemany(self, sql, param_list): - return self.cursor.executemany(sql, param_list) - - def __getattr__(self, attr): - return getattr(self.cursor, attr) - - def __iter__(self): - return iter(self.cursor) |
