"""
Heidi: Helpers related to visuals.
"""
import logging

__all__ = ['ColorizingStreamHandler', ]


#
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
# https://gist.github.com/758430
#


class ColorizingStreamHandler(logging.StreamHandler):
    # color names to indices
    color_map = {
        'black': 0,
        'red': 1,
        'green': 2,
        'yellow': 3,
        'blue': 4,
        'magenta': 5,
        'cyan': 6,
        'white': 7,
    }

    #levels to (background, foreground, bold/intense)
    level_map = {
        logging.DEBUG: (None, 'blue', False),
        logging.INFO: (None, 'white', False),
        logging.WARNING: (None, 'yellow', False),
        logging.ERROR: (None, 'red', False),
        logging.CRITICAL: ('red', 'white', True),
    }
    csi = '\x1b['
    reset = '\x1b[0m'

    @property
    def is_tty(self):
        isatty = getattr(self.stream, 'isatty', None)
        return isatty and isatty()

    def emit(self, record):
        try:
            message = self.format(record)
            stream = self.stream
            if not self.is_tty:
                stream.write(message)
            else:
                self.output_colorized(message)
            stream.write(getattr(self, 'terminator', '\n'))
            self.flush()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

    def output_colorized(self, message):
        self.stream.write(message)

    def colorize(self, message, record):
        if record.levelno in self.level_map:
            bg, fg, bold = self.level_map[record.levelno]
            params = []
            if bg in self.color_map:
                params.append(str(self.color_map[bg] + 40))
            if fg in self.color_map:
                params.append(str(self.color_map[fg] + 30))
            if bold:
                params.append('1')
            if params:
                message = ''.join((self.csi, ';'.join(params),
                                   'm', message, self.reset))
        return message

    def format(self, record):
        message = logging.StreamHandler.format(self, record)
        if self.is_tty:
            # Don't colorize any traceback
            parts = message.split('\n', 1)
            parts[0] = self.colorize(parts[0], record)
            message = '\n'.join(parts)
        return message

@@ -90,16 +90,35 @@
 ndler', 
+'ReadableSqlFilter'
 %5D%0A%0A%0A#%0A# 
@@ -2418,28 +2418,1444 @@
 rts)%0A        return message%0A
+%0A%0A###########%0A# FILTERS #%0A###########%0A%0Aclass ReadableSqlFilter(logging.Filter):%0A    %22%22%22%0A    A filter for more readable sql by stripping out the SELECT ... columns.%0A%0A    Modeled after how debug toolbar displays SQL. This code should be optimized%0A    for performance. For example, I don't check to make sure record.name is%0A    'django.db.backends' because I assume you put this filter alongside it. I%0A    also assume there is going to be a 'FROM' if there's a 'SELECT' and don't%0A    catch the ValueError that would result from that.%0A%0A    Sample Usage in Django's %60settings.py%60:%0A%0A        LOGGING = %7B%0A            ...%0A            'filters': %7B%0A                'readable_sql': %7B%0A                    '()': 'project_runpy.ReadableSqlFilter',%0A                %7D,%0A            %7D,%0A            'loggers': %7B%0A                'django.db.backends': %7B%0A                    'filters': %5B'readable_sql'%5D,%0A                    ...%0A                %7D%0A            %7D,%0A        %7D%0A    %22%22%22%0A%0A    def filter(self, record):%0A        if record.sql%5B:6%5D != 'SELECT':%0A            return True%0A%0A        # unfortunately, record.msg has already been rendered so we have to%0A        # modify .msg in-place instead of .sql%0A        begin = record.msg.index('SELECT')%0A        end = record.msg.index('FROM')%0A        if end - begin %3E 100:%0A            record.msg = u'%7B0%7D ... %7B1%7D'.format(%0A                    record.msg%5B:begin + 6%5D, record.msg%5Bend:%5D)%0A        return True%0A

add my sql logging filter