3 # Copyright (C) 2010-2013 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''In this file is stored the mechanism that manage color prints in the terminal
21 # define constant to use in scripts
23 COLOR_WARNING = 'WARNING'
24 COLOR_SUCCESS = 'SUCCESS'
26 COLOR_HEADER = 'HEADER'
28 COLOR_HIGLIGHT = 'HIGHLIGHT'
30 # the color map to use to print the colors
32 COLOR_ERROR: '\033[1m\033[31m',
33 COLOR_SUCCESS: '\033[1m\033[32m',
34 COLOR_WARNING: '\033[33m',
35 COLOR_HEADER: '\033[34m',
36 COLOR_INFO: '\033[35m',
37 COLOR_LABEL: '\033[36m',
38 COLOR_HIGLIGHT: '\033[97m\033[43m'
41 # list of available codes
42 __code_range__ = [1, 4] + list(range(30, 38)) + list(range(40, 48)) + list(range(90, 98)) + list(range(100, 108))
44 def printc(txt, code=''):
45 '''print a text with colors
47 :param txt str: The text to be printed.
48 :param code str: The color to use.
49 :return: The colored text.
52 # no code means 'auto mode' (works only for OK, KO, NO and ERR*)
54 striptxt = txt.strip().upper()
57 elif striptxt in ["KO", "NO"] or striptxt.startswith("ERR"):
62 # no code => output the originial text
63 if code not in __colormap__.keys() or __colormap__[code] == '':
66 return __colormap__[code] + txt + '\033[0m'
69 '''print a text info color
71 :param txt str: The text to be printed.
72 :return: The colored text.
75 return printc(txt, COLOR_INFO)
78 '''print a text error color
80 :param txt str: The text to be printed.
81 :return: The colored text.
84 return printc(txt, COLOR_ERROR)
86 def printcWarning(txt):
87 '''print a text warning color
89 :param txt str: The text to be printed.
90 :return: The colored text.
93 return printc(txt, COLOR_WARNING)
95 def printcHeader(txt):
96 '''print a text header color
98 :param txt str: The text to be printed.
99 :return: The colored text.
102 return printc(txt, COLOR_HEADER)
104 def printcLabel(txt):
105 '''print a text label color
107 :param txt str: The text to be printed.
108 :return: The colored text.
111 return printc(txt, COLOR_LABEL)
113 def printcSuccess(txt):
114 '''print a text success color
116 :param txt str: The text to be printed.
117 :return: The colored text.
120 return printc(txt, COLOR_SUCCESS)
122 def printcHighlight(txt):
123 '''print a text highlight color
125 :param txt str: The text to be printed.
126 :return: The colored text.
129 return printc(txt, COLOR_HIGLIGHT)
131 def cleancolor(message):
132 '''remove color from a colored text.
134 :param message str: The text to be cleaned.
135 :return: The cleaned text.
138 message = message.replace('\033[0m', '')
139 for i in __code_range__:
140 message = message.replace('\033[%dm' % i, '')
143 def print_value(logger, label, value, level=1, suffix=""):
144 '''shortcut method to print a label and a value with the info color
146 :param logger class logger: the logger instance.
147 :param label int: the label to print.
148 :param value str: the value to print.
149 :param level int: the level of verboseness.
150 :param suffix str: the suffix to add at the end.
153 print(" %s = %s %s" % (label, printcInfo(str(value)), suffix))
155 logger.write(" %s = %s %s\n" % (label, printcInfo(str(value)), suffix), level)
157 def print_color_range(start, end):
158 '''print possible range values for colors
160 :param start int: The smaller value.
161 :param end int: The bigger value.
163 for k in range(start, end+1):
164 print("\033[%dm%3d\033[0m" % (k, k),)
167 # This method prints the color map
168 def print_color_map():
169 '''This method prints the color map
173 for k in sorted(__colormap__.keys()):
174 codes = __colormap__[k].split('\033[')
175 codes = filter(lambda l: len(l) > 0, codes)
176 codes = map(lambda l: l[:-1], codes)
177 print(printc(" %s: '%s', " % (k, ';'.join(codes)), k))