Ticket #7679: colorize_runserver.py.patch

File colorize_runserver.py.patch, 3.1 KB (added by Kevin Hunter, 16 years ago)

patch: colorize certain console messages, v2

  • core/management/color.py

     
    55import sys
    66
    77from django.utils import termcolors
     8from django.conf import settings
    89
    910def supports_color():
    1011    """
     
    2122
    2223def color_style():
    2324    """Returns a Style object with the Django color scheme."""
    24     if not supports_color():
     25    color_enabled = getattr( settings, 'COLORIZE_CONSOLE', False )
     26    if False == color_enabled or not supports_color():
    2527        return no_style()
    2628    class dummy: pass
    2729    style = dummy()
    28     style.ERROR = termcolors.make_style(fg='red', opts=('bold',))
     30    style.ERROR        = termcolors.make_style(fg='red', opts=('bold',))
    2931    style.ERROR_OUTPUT = termcolors.make_style(fg='red', opts=('bold',))
    30     style.NOTICE = termcolors.make_style(fg='red')
    31     style.SQL_FIELD = termcolors.make_style(fg='green', opts=('bold',))
    32     style.SQL_COLTYPE = termcolors.make_style(fg='green')
    33     style.SQL_KEYWORD = termcolors.make_style(fg='yellow')
    34     style.SQL_TABLE = termcolors.make_style(opts=('bold',))
     32    style.NOTICE       = termcolors.make_style(fg='red')
     33    style.SQL_FIELD    = termcolors.make_style(fg='green', opts=('bold',))
     34    style.SQL_COLTYPE  = termcolors.make_style(fg='green')
     35    style.SQL_KEYWORD  = termcolors.make_style(fg='yellow')
     36    style.SQL_TABLE    = termcolors.make_style(opts=('bold',))
     37    style.NOT_FOUND    = termcolors.make_style(fg='yellow', opts=('bold',))
     38    style.UNAUTHORIZED = termcolors.make_style(fg='red', opts=('reverse',))
    3539    return style
    3640
    3741def no_style():
  • core/servers/basehttp.py

     
    1515import urllib
    1616
    1717from django.utils.http import http_date
     18from django.core.management.color import color_style
    1819
    1920__version__ = "0.1"
    2021__all__ = ['WSGIServer','WSGIRequestHandler']
     
    554555        # We set self.path to avoid crashes in log_message() on unsupported
    555556        # requests (like "OPTIONS").
    556557        self.path = ''
     558        self.style = color_style()
    557559        BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
    558560
    559561    def get_environ(self):
     
    605607        # Don't bother logging requests for admin images or the favicon.
    606608        if self.path.startswith(self.admin_media_prefix) or self.path == '/favicon.ico':
    607609            return
    608         sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args))
     610        msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)
    609611
     612        # utilize terminal colors, if available
     613        if '404' == args[1]:
     614            msg = self.style.NOT_FOUND(msg)
     615        if '401' == args[1]:
     616            msg = self.style.UNAUTHORIZED(msg)
     617        elif '500' == args[1]:
     618            msg = self.style.ERROR(msg)
     619
     620        sys.stderr.write(msg)
     621
    610622class AdminMediaHandler(object):
    611623    """
    612624    WSGI middleware that intercepts calls to the admin media directory, as
Back to Top