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

« back to all changes in this revision

Viewing changes to django/core/management/color.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Sets up the terminal color scheme.
 
3
"""
 
4
 
 
5
import sys
 
6
 
 
7
from django.utils import termcolors
 
8
 
 
9
def supports_color():
 
10
    """
 
11
    Returns True if the running system's terminal supports color, and False
 
12
    otherwise.
 
13
    """
 
14
    unsupported_platform = (sys.platform in ('win32', 'Pocket PC'))
 
15
    # isatty is not always implemented, #6223.
 
16
    is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
 
17
    if unsupported_platform or not is_a_tty:
 
18
        return False
 
19
    return True
 
20
 
 
21
def color_style():
 
22
    """Returns a Style object with the Django color scheme."""
 
23
    if not supports_color():
 
24
        return no_style()
 
25
    class dummy: pass
 
26
    style = dummy()
 
27
    style.ERROR = termcolors.make_style(fg='red', opts=('bold',))
 
28
    style.ERROR_OUTPUT = termcolors.make_style(fg='red', opts=('bold',))
 
29
    style.NOTICE = termcolors.make_style(fg='red')
 
30
    style.SQL_FIELD = termcolors.make_style(fg='green', opts=('bold',))
 
31
    style.SQL_COLTYPE = termcolors.make_style(fg='green')
 
32
    style.SQL_KEYWORD = termcolors.make_style(fg='yellow')
 
33
    style.SQL_TABLE = termcolors.make_style(opts=('bold',))
 
34
    return style
 
35
 
 
36
def no_style():
 
37
    """Returns a Style object that has no colors."""
 
38
    class dummy:
 
39
        def __getattr__(self, attr):
 
40
            return lambda x: x
 
41
    return dummy()