diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-12-28 06:48:47 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-12-28 06:48:47 +0000 |
| commit | c38d66a216a45e2352ee666eff9e1270082f906b (patch) | |
| tree | bb9c33e1b7cbcfa46c5bb98c189888623321d4b9 /django/utils/termcolors.py | |
| parent | 9319f895478c60717a7cf2cf132ad7fa4440b4f8 (diff) | |
Fixed #12112 -- Made the colors used by syntax highlighting customizable.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12009 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/termcolors.py')
| -rw-r--r-- | django/utils/termcolors.py | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/django/utils/termcolors.py b/django/utils/termcolors.py index 17a600f899..009b69ed03 100644 --- a/django/utils/termcolors.py +++ b/django/utils/termcolors.py @@ -5,7 +5,6 @@ termcolors.py color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') foreground = dict([(color_names[x], '3%s' % x) for x in range(8)]) background = dict([(color_names[x], '4%s' % x) for x in range(8)]) -del color_names RESET = '0' opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'} @@ -66,3 +65,112 @@ def make_style(opts=(), **kwargs): COMMENT = make_style(fg='blue', opts=('bold',)) """ return lambda text: colorize(text, opts, **kwargs) + +NOCOLOR_PALETTE = 'nocolor' +DARK_PALETTE = 'dark' +LIGHT_PALETTE = 'light' + +PALETTES = { + NOCOLOR_PALETTE: { + 'ERROR': {}, + 'NOTICE': {}, + 'SQL_FIELD': {}, + 'SQL_COLTYPE': {}, + 'SQL_KEYWORD': {}, + 'SQL_TABLE': {}, + }, + DARK_PALETTE: { + 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, + 'NOTICE': { 'fg': 'red' }, + 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) }, + 'SQL_COLTYPE': { 'fg': 'green' }, + 'SQL_KEYWORD': { 'fg': 'yellow' }, + 'SQL_TABLE': { 'opts': ('bold',) }, + }, + LIGHT_PALETTE: { + 'ERROR': { 'fg': 'red', 'opts': ('bold',) }, + 'NOTICE': { 'fg': 'red' }, + 'SQL_FIELD': { 'fg': 'green', 'opts': ('bold',) }, + 'SQL_COLTYPE': { 'fg': 'green' }, + 'SQL_KEYWORD': { 'fg': 'blue' }, + 'SQL_TABLE': { 'opts': ('bold',) }, + } +} +DEFAULT_PALETTE = DARK_PALETTE + +def parse_color_setting(config_string): + """Parse a DJANGO_COLORS environment variable to produce the system palette + + The general form of a pallete definition is: + + "palette;role=fg;role=fg/bg;role=fg,option,option;role=fg/bg,option,option" + + where: + palette is a named palette; one of 'light', 'dark', or 'nocolor'. + role is a named style used by Django + fg is a background color. + bg is a background color. + option is a display options. + + Specifying a named palette is the same as manually specifying the individual + definitions for each role. Any individual definitions following the pallete + definition will augment the base palette definition. + + Valid roles: + 'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table' + + Valid colors: + 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white' + + Valid options: + 'bold', 'underscore', 'blink', 'reverse', 'conceal' + + """ + if not config_string: + return PALETTES[DEFAULT_PALETTE] + + # Split the color configuration into parts + parts = config_string.lower().split(';') + palette = PALETTES[NOCOLOR_PALETTE].copy() + for part in parts: + if part in PALETTES: + # A default palette has been specified + palette.update(PALETTES[part]) + elif '=' in part: + # Process a palette defining string + definition = {} + + # Break the definition into the role, + # plus the list of specific instructions. + # The role must be in upper case + role, instructions = part.split('=') + role = role.upper() + + styles = instructions.split(',') + styles.reverse() + + # The first instruction can contain a slash + # to break apart fg/bg. + colors = styles.pop().split('/') + colors.reverse() + fg = colors.pop() + if fg in color_names: + definition['fg'] = fg + if colors and colors[-1] in color_names: + definition['bg'] = colors[-1] + + # All remaining instructions are options + opts = tuple(s for s in styles if s in opt_dict.keys()) + if opts: + definition['opts'] = opts + + # The nocolor palette has all available roles. + # Use that palette as the basis for determining + # if the role is valid. + if role in PALETTES[NOCOLOR_PALETTE] and definition: + palette[role] = definition + + # If there are no colors specified, return the empty palette. + if palette == PALETTES[NOCOLOR_PALETTE]: + return None + return palette |
