/usr/lib/python2.7/dist-packages/debug_toolbar/panels/sql/utils.py is in python-django-debug-toolbar 1:1.0.1-0+nmu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from __future__ import absolute_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)
|