Ticket #13291: 13291-custom-color-palettes-r12937.diff

File 13291-custom-color-palettes-r12937.diff, 7.0 KB (added by Tai Lee, 14 years ago)
  • django/core/management/color.py

     
    1919        return False
    2020    return True
    2121
    22 def color_style():
     22def color_style(palettes=termcolors.PALETTES):
    2323    """Returns a Style object with the Django color scheme."""
    2424    if not supports_color():
    2525        style = no_style()
    2626    else:
    2727        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)
    2929        if color_settings:
    3030            class dummy: pass
    3131            style = dummy()
    3232            # The nocolor palette has all available roles.
    3333            # Use that pallete as the basis for populating
    3434            # the palette as defined in the environment.
    35             for role in termcolors.PALETTES[termcolors.NOCOLOR_PALETTE]:
     35            for role in palettes[termcolors.NOCOLOR_PALETTE]:
    3636                format = color_settings.get(role,{})
    3737                setattr(style, role, termcolors.make_style(**format))
    3838            # For backwards compatibility,
  • django/utils/termcolors.py

     
    5050        if o in opt_dict:
    5151            code_list.append(opt_dict[o])
    5252    if 'noreset' not in opts:
    53         text = text + '\x1b[%sm' % RESET
    54     return ('\x1b[%sm' % ';'.join(code_list)) + text
     53        text = '%s\x1b[%sm' % (text, RESET)
     54    return '\x1b[%sm%s' % (';'.join(code_list), text)
    5555
    5656def make_style(opts=(), **kwargs):
    5757    """
     
    7171
    7272PALETTES = {
    7373    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':         {},
    8080        'HTTP_INFO':         {},
    8181        'HTTP_SUCCESS':      {},
    8282        'HTTP_REDIRECT':     {},
     
    8686        'HTTP_SERVER_ERROR': {},
    8787    },
    8888    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',) },
    9595        'HTTP_INFO':         { 'opts': ('bold',) },
    9696        'HTTP_SUCCESS':      { },
    9797        'HTTP_REDIRECT':     { 'fg': 'green' },
     
    101101        'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) },
    102102    },
    103103    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',) },
    110110        'HTTP_INFO':         { 'opts': ('bold',) },
    111111        'HTTP_SUCCESS':      { },
    112112        'HTTP_REDIRECT':     { 'fg': 'green', 'opts': ('bold',) },
     
    118118}
    119119DEFAULT_PALETTE = DARK_PALETTE
    120120
    121 def parse_color_setting(config_string):
     121def parse_color_setting(config_string, palettes=PALETTES):
    122122    """Parse a DJANGO_COLORS environment variable to produce the system palette
    123123
    124124    The general form of a pallete definition is:
     
    136136    definitions for each role. Any individual definitions following the pallete
    137137    definition will augment the base palette definition.
    138138
     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
    139142    Valid roles:
    140143        'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table',
    141144        'http_info', 'http_success', 'http_redirect', 'http_bad_request',
  • tests/regressiontests/utils/termcolors.py

     
    147147        self.assertEquals(parse_color_setting('error=green,bLiNk'),
    148148                          dict(PALETTES[NOCOLOR_PALETTE],
    149149                            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

     
    10931093*except* for the colors for errors and notices which would be
    10941094overridden as specified.
    10951095
     1096If you need to define new roles or default colors for use in your own
     1097management commands and still want to retain the ability for users to
     1098customize them with the ``DJANGO_COLORS`` environment variable, you can pass
     1099your 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
    10961108Bash completion
    10971109---------------
    10981110
Back to Top