From 77e27e7de73333b8610c5e8967a3681636f844b0 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 4 Jan 2010 12:16:09 +0000 Subject: [PATCH] Fixed #7679 -- Added (configurable) highlighting colors to the development server. Thanks to Rob Hudson, hunteke, and Bastian Kleineidam for the various patches that contributed to the final result. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12085 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/servers/basehttp.py | 23 ++++++++++++++++++++++- django/utils/termcolors.py | 22 +++++++++++++++++++++- docs/ref/django-admin.txt | 6 ++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index f7c0a77f7c..415756c403 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -15,6 +15,7 @@ import stat import sys import urllib +from django.core.management.color import color_style from django.utils.http import http_date from django.utils._os import safe_join @@ -557,6 +558,7 @@ class WSGIRequestHandler(BaseHTTPRequestHandler): # We set self.path to avoid crashes in log_message() on unsupported # requests (like "OPTIONS"). self.path = '' + self.style = color_style() BaseHTTPRequestHandler.__init__(self, *args, **kwargs) def get_environ(self): @@ -608,7 +610,26 @@ class WSGIRequestHandler(BaseHTTPRequestHandler): # Don't bother logging requests for admin images or the favicon. if self.path.startswith(self.admin_media_prefix) or self.path == '/favicon.ico': return - sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args)) + + msg = "[%s] %s\n" % (self.log_date_time_string(), format % args) + + # Utilize terminal colors, if available + if args[1][0] == '2': + # Put 2XX first, since it should be the common case + msg = self.style.HTTP_SUCCESS(msg) + elif args[1][0] == '1': + msg = self.style.HTTP_INFO(msg) + elif args[1][0] == '3': + msg = self.style.HTTP_REDIRECT(msg) + elif args[1] == '404': + msg = self.style.HTTP_NOT_FOUND(msg) + elif args[1][0] == '4': + msg = self.style.HTTP_BAD_REQUEST(msg) + else: + # Any 5XX, or any other response + msg = self.style.HTTP_SERVER_ERROR(msg) + + sys.stderr.write(msg) class AdminMediaHandler(object): """ diff --git a/django/utils/termcolors.py b/django/utils/termcolors.py index 009b69ed03..4bcf634061 100644 --- a/django/utils/termcolors.py +++ b/django/utils/termcolors.py @@ -78,6 +78,12 @@ PALETTES = { 'SQL_COLTYPE': {}, 'SQL_KEYWORD': {}, 'SQL_TABLE': {}, + 'HTTP_INFO': {}, + 'HTTP_SUCCESS': {}, + 'HTTP_REDIRECT': {}, + 'HTTP_BAD_REQUEST': {}, + 'HTTP_NOT_FOUND': {}, + 'HTTP_SERVER_ERROR': {}, }, DARK_PALETTE: { 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, @@ -86,6 +92,12 @@ PALETTES = { 'SQL_COLTYPE': { 'fg': 'green' }, 'SQL_KEYWORD': { 'fg': 'yellow' }, 'SQL_TABLE': { 'opts': ('bold',) }, + 'HTTP_INFO': { 'opts': ('bold',) }, + 'HTTP_SUCCESS': { }, + 'HTTP_REDIRECT': { 'fg': 'green' }, + 'HTTP_BAD_REQUEST': { 'fg': 'red', 'opts': ('bold',) }, + 'HTTP_NOT_FOUND': { 'fg': 'yellow' }, + 'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) }, }, LIGHT_PALETTE: { 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, @@ -94,6 +106,12 @@ PALETTES = { 'SQL_COLTYPE': { 'fg': 'green' }, 'SQL_KEYWORD': { 'fg': 'blue' }, 'SQL_TABLE': { 'opts': ('bold',) }, + 'HTTP_INFO': { 'opts': ('bold',) }, + 'HTTP_SUCCESS': { }, + 'HTTP_REDIRECT': { 'fg': 'green', 'opts': ('bold',) }, + 'HTTP_BAD_REQUEST': { 'fg': 'red', 'opts': ('bold',) }, + 'HTTP_NOT_FOUND': { 'fg': 'red' }, + 'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) }, } } DEFAULT_PALETTE = DARK_PALETTE @@ -117,7 +135,9 @@ def parse_color_setting(config_string): definition will augment the base palette definition. Valid roles: - 'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table' + 'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table', + 'http_info', 'http_success', 'http_redirect', 'http_bad_request', + 'http_not_found', 'http_server_error' Valid colors: 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white' diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 88715ebdc7..af47759745 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -1026,6 +1026,12 @@ number of roles in which color is used: * ``sql_coltype`` - The type of a model field in SQL. * ``sql_keyword`` - A SQL keyword. * ``sql_table`` - The name of a model in SQL. + * ``http_info`` - A 1XX HTTP Informational server response. + * ``http_success`` - A 2XX HTTP Success server response. + * ``http_redirect`` - A 3XX HTTP Redirect server response. + * ``http_not_found`` - A 404 HTTP Not Found server response. + * ``http_bad_request`` - A 4XX HTTP Bad Request server response other than 404. + * ``http_server_error`` - A 5XX HTTP Server Error response. Each of these roles can be assigned a specific foreground and background color, from the following list: