Ticket #13291: 13291-custom-color-palettes-r12937.diff
File 13291-custom-color-palettes-r12937.diff, 7.0 KB (added by , 15 years ago) |
---|
-
django/core/management/color.py
19 19 return False 20 20 return True 21 21 22 def color_style( ):22 def color_style(palettes=termcolors.PALETTES): 23 23 """Returns a Style object with the Django color scheme.""" 24 24 if not supports_color(): 25 25 style = no_style() 26 26 else: 27 27 DJANGO_COLORS = os.environ.get('DJANGO_COLORS', '') 28 color_settings = termcolors.parse_color_setting(DJANGO_COLORS )28 color_settings = termcolors.parse_color_setting(DJANGO_COLORS, palettes) 29 29 if color_settings: 30 30 class dummy: pass 31 31 style = dummy() 32 32 # The nocolor palette has all available roles. 33 33 # Use that pallete as the basis for populating 34 34 # the palette as defined in the environment. 35 for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]:35 for role in palettes[termcolors.NOCOLOR_PALETTE]: 36 36 format = color_settings.get(role,{}) 37 37 setattr(style, role, termcolors.make_style(**format)) 38 38 # For backwards compatibility, -
django/utils/termcolors.py
50 50 if o in opt_dict: 51 51 code_list.append(opt_dict[o]) 52 52 if 'noreset' not in opts: 53 text = text + '\x1b[%sm' % RESET54 return ('\x1b[%sm' % ';'.join(code_list)) + text53 text = '%s\x1b[%sm' % (text, RESET) 54 return '\x1b[%sm%s' % (';'.join(code_list), text) 55 55 56 56 def make_style(opts=(), **kwargs): 57 57 """ … … 71 71 72 72 PALETTES = { 73 73 NOCOLOR_PALETTE: { 74 'ERROR': {},75 'NOTICE': {},76 'SQL_FIELD': {},77 'SQL_COLTYPE': {},78 'SQL_KEYWORD': {},79 'SQL_TABLE': {},74 'ERROR': {}, 75 'NOTICE': {}, 76 'SQL_FIELD': {}, 77 'SQL_COLTYPE': {}, 78 'SQL_KEYWORD': {}, 79 'SQL_TABLE': {}, 80 80 'HTTP_INFO': {}, 81 81 'HTTP_SUCCESS': {}, 82 82 'HTTP_REDIRECT': {}, … … 86 86 'HTTP_SERVER_ERROR': {}, 87 87 }, 88 88 DARK_PALETTE: { 89 'ERROR': { 'fg': 'red', 'opts': ('bold',) },90 'NOTICE': { 'fg': 'red' },91 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) },92 'SQL_COLTYPE': { 'fg': 'green' },93 'SQL_KEYWORD': { 'fg': 'yellow' },94 'SQL_TABLE': { 'opts': ('bold',) },89 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, 90 'NOTICE': { 'fg': 'red' }, 91 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) }, 92 'SQL_COLTYPE': { 'fg': 'green' }, 93 'SQL_KEYWORD': { 'fg': 'yellow' }, 94 'SQL_TABLE': { 'opts': ('bold',) }, 95 95 'HTTP_INFO': { 'opts': ('bold',) }, 96 96 'HTTP_SUCCESS': { }, 97 97 'HTTP_REDIRECT': { 'fg': 'green' }, … … 101 101 'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) }, 102 102 }, 103 103 LIGHT_PALETTE: { 104 'ERROR': { 'fg': 'red', 'opts': ('bold',) },105 'NOTICE': { 'fg': 'red' },106 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) },107 'SQL_COLTYPE': { 'fg': 'green' },108 'SQL_KEYWORD': { 'fg': 'blue' },109 'SQL_TABLE': { 'opts': ('bold',) },104 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, 105 'NOTICE': { 'fg': 'red' }, 106 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) }, 107 'SQL_COLTYPE': { 'fg': 'green' }, 108 'SQL_KEYWORD': { 'fg': 'blue' }, 109 'SQL_TABLE': { 'opts': ('bold',) }, 110 110 'HTTP_INFO': { 'opts': ('bold',) }, 111 111 'HTTP_SUCCESS': { }, 112 112 'HTTP_REDIRECT': { 'fg': 'green', 'opts': ('bold',) }, … … 118 118 } 119 119 DEFAULT_PALETTE = DARK_PALETTE 120 120 121 def parse_color_setting(config_string ):121 def parse_color_setting(config_string, palettes=PALETTES): 122 122 """Parse a DJANGO_COLORS environment variable to produce the system palette 123 123 124 124 The general form of a pallete definition is: … … 136 136 definitions for each role. Any individual definitions following the pallete 137 137 definition will augment the base palette definition. 138 138 139 An alternate dictionary of palettes may be provided to extend or override 140 Django's default palettes (e.g. with new roles or different colours). 141 139 142 Valid roles: 140 143 'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table', 141 144 'http_info', 'http_success', 'http_redirect', 'http_bad_request', -
tests/regressiontests/utils/termcolors.py
147 147 self.assertEquals(parse_color_setting('error=green,bLiNk'), 148 148 dict(PALETTES[NOCOLOR_PALETTE], 149 149 ERROR={'fg':'green', 'opts': ('blink',)})) 150 151 def test_custom_palette(self): 152 custom_palettes = PALETTES.copy() 153 custom_palettes[NOCOLOR_PALETTE].update({'WARNING': {}, 'NOTICE': {}}) 154 custom_palettes[DARK_PALETTE].update({'WARNING': {'fg': 'yellow'}, 'NOTICE': {'fg': 'cyan'}}) 155 custom_palettes[LIGHT_PALETTE].update({'WARNING': {'fg': 'black', 'bg': 'yellow'}, 'NOTICE': {'fg': 'blue'}}) 156 # empty string. 157 self.assertEquals(parse_color_setting('', custom_palettes), custom_palettes[DEFAULT_PALETTE]) 158 # simple palette. 159 self.assertEquals(parse_color_setting('light', custom_palettes), custom_palettes[LIGHT_PALETTE]) 160 # override palette. 161 self.assertEquals(parse_color_setting('light;error=green', custom_palettes), 162 dict(custom_palettes[LIGHT_PALETTE], 163 ERROR={'fg':'green'})) -
docs/ref/django-admin.txt
1093 1093 *except* for the colors for errors and notices which would be 1094 1094 overridden as specified. 1095 1095 1096 If you need to define new roles or default colors for use in your own 1097 management commands and still want to retain the ability for users to 1098 customize them with the ``DJANGO_COLORS`` environment variable, you can pass 1099 your own dictionary of color palettes to the ``color_style()`` function:: 1100 1101 from django.core.management.color import color_style 1102 from django.utils.termcolors import PALETTES 1103 PALETTES['nocolor'].update(WARNING={}) 1104 PALETTES['dark'].update(WARNING={'fg': 'yellow'}) 1105 PALETTES['light'].update(WARNING={'fg': 'black', 'bg': 'yellow'}) 1106 style = color_style(PALETTES) 1107 1096 1108 Bash completion 1097 1109 --------------- 1098 1110