Revision 2129c5ff

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

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

  
29 30
import os
30 31
import subprocess
31 32
import sys
32 33

  
33
DEFAULT = '\033[0m'
34
RED = '\033[91m'
35
GREEN = '\033[92m'
36
BLUE = '\033[94m'
37
CYAN = '\033[96m'
38
WHITE = '\033[97m'
39
YELLOW = '\033[93m'
40
MAGENTA = '\033[95m'
41
GREY = '\033[90m'
42
BLACK = '\033[90m'
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"
43 56

  
44 57
_enabled = sys.stdout.isatty()
45 58

  
46 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

  
47 75
def colorize(line, color=None):
48
  if _enabled and color is not None:
49
    return color + line + DEFAULT
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()
50 85
  else:
51 86
    return line
52 87

  
b/qa/ganeti-qa.py
66 66
  line = (mark * 4) + " " + line + " "
67 67
  line += "-" * (end - len(line))
68 68
  line = line.rstrip()
69
  line = colors.colorize(line, color)
69
  line = colors.colorize(line, color=color)
70 70
  return line
71 71

  
72 72

  

Also available in: Unified diff