~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/utils/termcolors.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
6
6
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
7
7
background = dict([(color_names[x], '4%s' % x) for x in range(8)])
8
 
del color_names
9
8
 
10
9
RESET = '0'
11
10
opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'}
39
38
        print colorize('and so should this')
40
39
        print 'this should not be red'
41
40
    """
42
 
    text = str(text)
43
41
    code_list = []
44
42
    if text == '' and len(opts) == 1 and opts[0] == 'reset':
45
43
        return '\x1b[%sm' % RESET
66
64
        COMMENT = make_style(fg='blue', opts=('bold',))
67
65
    """
68
66
    return lambda text: colorize(text, opts, **kwargs)
 
67
 
 
68
NOCOLOR_PALETTE = 'nocolor'
 
69
DARK_PALETTE = 'dark'
 
70
LIGHT_PALETTE = 'light'
 
71
 
 
72
PALETTES = {
 
73
    NOCOLOR_PALETTE: {
 
74
        'ERROR':        {},
 
75
        'NOTICE':       {},
 
76
        'SQL_FIELD':    {},
 
77
        'SQL_COLTYPE':  {},
 
78
        'SQL_KEYWORD':  {},
 
79
        'SQL_TABLE':    {},
 
80
        'HTTP_INFO':         {},
 
81
        'HTTP_SUCCESS':      {},
 
82
        'HTTP_REDIRECT':     {},
 
83
        'HTTP_NOT_MODIFIED': {},
 
84
        'HTTP_BAD_REQUEST':  {},
 
85
        'HTTP_NOT_FOUND':    {},
 
86
        'HTTP_SERVER_ERROR': {},
 
87
    },
 
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',) },
 
95
        'HTTP_INFO':         { 'opts': ('bold',) },
 
96
        'HTTP_SUCCESS':      { },
 
97
        'HTTP_REDIRECT':     { 'fg': 'green' },
 
98
        'HTTP_NOT_MODIFIED': { 'fg': 'cyan' },
 
99
        'HTTP_BAD_REQUEST':  { 'fg': 'red', 'opts': ('bold',) },
 
100
        'HTTP_NOT_FOUND':    { 'fg': 'yellow' },
 
101
        'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) },
 
102
    },
 
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',) },
 
110
        'HTTP_INFO':         { 'opts': ('bold',) },
 
111
        'HTTP_SUCCESS':      { },
 
112
        'HTTP_REDIRECT':     { 'fg': 'green', 'opts': ('bold',) },
 
113
        'HTTP_NOT_MODIFIED': { 'fg': 'green' },
 
114
        'HTTP_BAD_REQUEST':  { 'fg': 'red', 'opts': ('bold',) },
 
115
        'HTTP_NOT_FOUND':    { 'fg': 'red' },
 
116
        'HTTP_SERVER_ERROR': { 'fg': 'magenta', 'opts': ('bold',) },
 
117
    }
 
118
}
 
119
DEFAULT_PALETTE = DARK_PALETTE
 
120
 
 
121
def parse_color_setting(config_string):
 
122
    """Parse a DJANGO_COLORS environment variable to produce the system palette
 
123
 
 
124
    The general form of a pallete definition is:
 
125
 
 
126
        "palette;role=fg;role=fg/bg;role=fg,option,option;role=fg/bg,option,option"
 
127
 
 
128
    where:
 
129
        palette is a named palette; one of 'light', 'dark', or 'nocolor'.
 
130
        role is a named style used by Django
 
131
        fg is a background color.
 
132
        bg is a background color.
 
133
        option is a display options.
 
134
 
 
135
    Specifying a named palette is the same as manually specifying the individual
 
136
    definitions for each role. Any individual definitions following the pallete
 
137
    definition will augment the base palette definition.
 
138
 
 
139
    Valid roles:
 
140
        'error', 'notice', 'sql_field', 'sql_coltype', 'sql_keyword', 'sql_table',
 
141
        'http_info', 'http_success', 'http_redirect', 'http_bad_request',
 
142
        'http_not_found', 'http_server_error'
 
143
 
 
144
    Valid colors:
 
145
        'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
 
146
 
 
147
    Valid options:
 
148
        'bold', 'underscore', 'blink', 'reverse', 'conceal'
 
149
 
 
150
    """
 
151
    if not config_string:
 
152
        return PALETTES[DEFAULT_PALETTE]
 
153
 
 
154
    # Split the color configuration into parts
 
155
    parts = config_string.lower().split(';')
 
156
    palette = PALETTES[NOCOLOR_PALETTE].copy()
 
157
    for part in parts:
 
158
        if part in PALETTES:
 
159
            # A default palette has been specified
 
160
            palette.update(PALETTES[part])
 
161
        elif '=' in part:
 
162
            # Process a palette defining string
 
163
            definition = {}
 
164
 
 
165
            # Break the definition into the role,
 
166
            # plus the list of specific instructions.
 
167
            # The role must be in upper case
 
168
            role, instructions = part.split('=')
 
169
            role = role.upper()
 
170
 
 
171
            styles = instructions.split(',')
 
172
            styles.reverse()
 
173
 
 
174
            # The first instruction can contain a slash
 
175
            # to break apart fg/bg.
 
176
            colors = styles.pop().split('/')
 
177
            colors.reverse()
 
178
            fg = colors.pop()
 
179
            if fg in color_names:
 
180
                definition['fg'] = fg
 
181
            if colors and colors[-1] in color_names:
 
182
                definition['bg'] = colors[-1]
 
183
 
 
184
            # All remaining instructions are options
 
185
            opts = tuple(s for s in styles if s in opt_dict.keys())
 
186
            if opts:
 
187
                definition['opts'] = opts
 
188
 
 
189
            # The nocolor palette has all available roles.
 
190
            # Use that palette as the basis for determining
 
191
            # if the role is valid.
 
192
            if role in PALETTES[NOCOLOR_PALETTE] and definition:
 
193
                palette[role] = definition
 
194
 
 
195
    # If there are no colors specified, return the empty palette.
 
196
    if palette == PALETTES[NOCOLOR_PALETTE]:
 
197
        return None
 
198
    return palette