Statistics
| Branch: | Tag: | Revision:

root / qa / colors.py @ 2129c5ff

History | View | Annotate | Download (2.5 kB)

1
#!/usr/bin/python -u
2
#
3

    
4
# Copyright (C) 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for adding colorized output to Ganeti.
23

24
Colors are enabled only if the standard output is a proper terminal.
25
(Or call check_for_colors() to make a thorough test using "tput".)
26

27
See http://en.wikipedia.org/wiki/ANSI_escape_code for more possible additions.
28
"""
29

    
30
import os
31
import subprocess
32
import sys
33

    
34
DEFAULT = "0"
35
BOLD = "1"
36
UNDERLINE = "4"
37
REVERSE = "7"
38

    
39
BLACK = "30"
40
RED = "31"
41
GREEN = "32"
42
YELLOW = "33"
43
BLUE = "34"
44
MAGENTA = "35"
45
CYAN = "36"
46
WHITE = "37"
47

    
48
BG_BLACK = "40"
49
BG_RED = "41"
50
BG_GREEN = "42"
51
BG_YELLOW = "43"
52
BG_BLUE = "44"
53
BG_MAGENTA = "45"
54
BG_CYAN = "46"
55
BG_WHITE = "47"
56

    
57
_enabled = sys.stdout.isatty()
58

    
59

    
60
def _escape_one(code):
61
  return "\033[" + code + "m" if code else ""
62

    
63

    
64
def _escape(codes):
65
  if hasattr(codes, "__iter__"):
66
    return _escape_one(";".join(codes))
67
  else:
68
    return _escape_one(codes)
69

    
70

    
71
def _reset():
72
  return _escape([DEFAULT])
73

    
74

    
75
def colorize(line, color=None):
76
  """Wraps a given string into ANSI color codes corresponding to given
77
  color(s).
78

79
  @param line: a string
80
  @param color: a color or a list of colors selected from this module's
81
    constants
82
  """
83
  if _enabled and color:
84
    return _escape(color) + line + _reset()
85
  else:
86
    return line
87

    
88

    
89
def check_for_colors():
90
  """Tries to call 'tput' to properly determine, if the terminal has colors.
91

92
  This functions is meant to be run once at the program's start. If not
93
  invoked, colors are enabled iff standard output is a terminal.
94
  """
95
  colors = 0
96
  if sys.stdout.isatty():
97
    try:
98
      p = subprocess.Popen(["tput", "colors"], stdout=subprocess.PIPE)
99
      output = p.communicate()[0]
100
      if p.returncode == 0:
101
        colors = int(output)
102
    except (OSError, ValueError):
103
      pass
104
  global _enabled
105
  _enabled = (colors >= 2)