Statistics
| Branch: | Tag: | Revision:

root / qa / colors.py @ 82ce55fa

History | View | Annotate | Download (2.5 kB)

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

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

27 2129c5ff Petr Pudlak
See http://en.wikipedia.org/wiki/ANSI_escape_code for more possible additions.
28 d5a9b556 Petr Pudlak
"""
29 d5a9b556 Petr Pudlak
30 d5a9b556 Petr Pudlak
import os
31 1490a90c Petr Pudlak
import subprocess
32 d5a9b556 Petr Pudlak
import sys
33 d5a9b556 Petr Pudlak
34 2129c5ff Petr Pudlak
DEFAULT = "0"
35 2129c5ff Petr Pudlak
BOLD = "1"
36 2129c5ff Petr Pudlak
UNDERLINE = "4"
37 2129c5ff Petr Pudlak
REVERSE = "7"
38 2129c5ff Petr Pudlak
39 2129c5ff Petr Pudlak
BLACK = "30"
40 2129c5ff Petr Pudlak
RED = "31"
41 2129c5ff Petr Pudlak
GREEN = "32"
42 2129c5ff Petr Pudlak
YELLOW = "33"
43 2129c5ff Petr Pudlak
BLUE = "34"
44 2129c5ff Petr Pudlak
MAGENTA = "35"
45 2129c5ff Petr Pudlak
CYAN = "36"
46 2129c5ff Petr Pudlak
WHITE = "37"
47 2129c5ff Petr Pudlak
48 2129c5ff Petr Pudlak
BG_BLACK = "40"
49 2129c5ff Petr Pudlak
BG_RED = "41"
50 2129c5ff Petr Pudlak
BG_GREEN = "42"
51 2129c5ff Petr Pudlak
BG_YELLOW = "43"
52 2129c5ff Petr Pudlak
BG_BLUE = "44"
53 2129c5ff Petr Pudlak
BG_MAGENTA = "45"
54 2129c5ff Petr Pudlak
BG_CYAN = "46"
55 2129c5ff Petr Pudlak
BG_WHITE = "47"
56 d5a9b556 Petr Pudlak
57 d5a9b556 Petr Pudlak
_enabled = sys.stdout.isatty()
58 d5a9b556 Petr Pudlak
59 d5a9b556 Petr Pudlak
60 2129c5ff Petr Pudlak
def _escape_one(code):
61 2129c5ff Petr Pudlak
  return "\033[" + code + "m" if code else ""
62 2129c5ff Petr Pudlak
63 2129c5ff Petr Pudlak
64 2129c5ff Petr Pudlak
def _escape(codes):
65 2129c5ff Petr Pudlak
  if hasattr(codes, "__iter__"):
66 2129c5ff Petr Pudlak
    return _escape_one(";".join(codes))
67 2129c5ff Petr Pudlak
  else:
68 2129c5ff Petr Pudlak
    return _escape_one(codes)
69 2129c5ff Petr Pudlak
70 2129c5ff Petr Pudlak
71 2129c5ff Petr Pudlak
def _reset():
72 2129c5ff Petr Pudlak
  return _escape([DEFAULT])
73 2129c5ff Petr Pudlak
74 2129c5ff Petr Pudlak
75 d5a9b556 Petr Pudlak
def colorize(line, color=None):
76 2129c5ff Petr Pudlak
  """Wraps a given string into ANSI color codes corresponding to given
77 2129c5ff Petr Pudlak
  color(s).
78 2129c5ff Petr Pudlak

79 2129c5ff Petr Pudlak
  @param line: a string
80 2129c5ff Petr Pudlak
  @param color: a color or a list of colors selected from this module's
81 2129c5ff Petr Pudlak
    constants
82 2129c5ff Petr Pudlak
  """
83 2129c5ff Petr Pudlak
  if _enabled and color:
84 2129c5ff Petr Pudlak
    return _escape(color) + line + _reset()
85 d5a9b556 Petr Pudlak
  else:
86 d5a9b556 Petr Pudlak
    return line
87 1490a90c Petr Pudlak
88 1490a90c Petr Pudlak
89 1490a90c Petr Pudlak
def check_for_colors():
90 1490a90c Petr Pudlak
  """Tries to call 'tput' to properly determine, if the terminal has colors.
91 1490a90c Petr Pudlak

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