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/panels/sql/utils.py | |
| 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/panels/sql/utils.py')
| -rw-r--r-- | debug_toolbar/panels/sql/utils.py | 37 | 
1 files changed, 37 insertions, 0 deletions
| diff --git a/debug_toolbar/panels/sql/utils.py b/debug_toolbar/panels/sql/utils.py new file mode 100644 index 0000000..00728a3 --- /dev/null +++ b/debug_toolbar/panels/sql/utils.py @@ -0,0 +1,37 @@ +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) | 
