Salome HOME
style: black format
[tools/sat.git] / src / printcolors.py
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
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.
9 #
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.
14 #
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
19 """
20
21
22 # define constant to use in scripts
23 COLOR_ERROR = "ERROR"
24 COLOR_WARNING = "WARNING"
25 COLOR_SUCCESS = "SUCCESS"
26 COLOR_LABEL = "LABEL"
27 COLOR_HEADER = "HEADER"
28 COLOR_INFO = "INFO"
29 COLOR_HIGLIGHT = "HIGHLIGHT"
30
31 # the color map to use to print the colors
32 __colormap__ = {
33     COLOR_ERROR: "\033[1m\033[31m",
34     COLOR_SUCCESS: "\033[1m\033[32m",
35     COLOR_WARNING: "\033[33m",
36     COLOR_HEADER: "\033[34m",
37     COLOR_INFO: "\033[35m",
38     COLOR_LABEL: "\033[36m",
39     COLOR_HIGLIGHT: "\033[97m\033[43m",
40 }
41
42 # list of available codes
43 __code_range__ = (
44     [1, 4]
45     + list(range(30, 38))
46     + list(range(40, 48))
47     + list(range(90, 98))
48     + list(range(100, 108))
49 )
50
51
52 def printc(txt, code=""):
53     """print a text with colors
54
55     :param txt str: The text to be printed.
56     :param code str: The color to use.
57     :return: The colored text.
58     :rtype: str
59     """
60     # no code means 'auto mode' (works only for OK, KO, NO and ERR*)
61     if code == "":
62         striptxt = txt.strip().upper()
63         if striptxt == "OK":
64             code = COLOR_SUCCESS
65         elif striptxt in ["KO", "NO"] or striptxt.startswith("ERR"):
66             code = COLOR_ERROR
67         else:
68             return txt
69
70     # no code => output the originial text
71     if code not in __colormap__.keys() or __colormap__[code] == "":
72         return txt
73
74     return __colormap__[code] + txt + "\033[0m"
75
76
77 def printcInfo(txt):
78     """print a text info color
79
80     :param txt str: The text to be printed.
81     :return: The colored text.
82     :rtype: str
83     """
84     return printc(txt, COLOR_INFO)
85
86
87 def printcError(txt):
88     """print a text error color
89
90     :param txt str: The text to be printed.
91     :return: The colored text.
92     :rtype: str
93     """
94     return printc(txt, COLOR_ERROR)
95
96
97 def printcWarning(txt):
98     """print a text warning color
99
100     :param txt str: The text to be printed.
101     :return: The colored text.
102     :rtype: str
103     """
104     return printc(txt, COLOR_WARNING)
105
106
107 def printcHeader(txt):
108     """print a text header color
109
110     :param txt str: The text to be printed.
111     :return: The colored text.
112     :rtype: str
113     """
114     return printc(txt, COLOR_HEADER)
115
116
117 def printcLabel(txt):
118     """print a text label color
119
120     :param txt str: The text to be printed.
121     :return: The colored text.
122     :rtype: str
123     """
124     return printc(txt, COLOR_LABEL)
125
126
127 def printcSuccess(txt):
128     """print a text success color
129
130     :param txt str: The text to be printed.
131     :return: The colored text.
132     :rtype: str
133     """
134     return printc(txt, COLOR_SUCCESS)
135
136
137 def printcHighlight(txt):
138     """print a text highlight color
139
140     :param txt str: The text to be printed.
141     :return: The colored text.
142     :rtype: str
143     """
144     return printc(txt, COLOR_HIGLIGHT)
145
146
147 def cleancolor(message):
148     """remove color from a colored text.
149
150     :param message str: The text to be cleaned.
151     :return: The cleaned text.
152     :rtype: str
153     """
154     if message == None:
155         return message
156
157     message = message.replace("\033[0m", "")
158     for i in __code_range__:
159         message = message.replace("\033[%dm" % i, "")
160     return message
161
162
163 def print_value(logger, label, value, level=1, suffix=""):
164     """shortcut method to print a label and a value with the info color
165
166     :param logger class logger: the logger instance.
167     :param label int: the label to print.
168     :param value str: the value to print.
169     :param level int: the level of verboseness.
170     :param suffix str: the suffix to add at the end.
171     """
172     if type(value) is list:
173         skip = "\n     "
174         strValue = ""
175         i = 0
176         for v in value:
177             strValue += "%15s, " % str(v)
178             i += 1
179             if i >= 5:
180                 strValue += skip
181                 i = 0
182         if len(value) > 5:
183             strValue = skip + strValue
184     else:
185         strValue = str(value)
186     strValue = printcInfo(strValue)
187     if logger is None:
188         print("  %s = %s %s" % (label, strValue, suffix))
189     else:
190         logger.write("  %s = %s %s\n" % (label, strValue, suffix), level)
191
192
193 def print_color_range(start, end):
194     """print possible range values for colors
195
196     :param start int: The smaller value.
197     :param end int: The bigger value.
198     """
199     for k in range(start, end + 1):
200         print(
201             "\033[%dm%3d\033[0m" % (k, k),
202         )
203     print
204
205
206 # This method prints the color map
207 def print_color_map():
208     """This method prints the color map"""
209     print("colormap:")
210     print("{")
211     for k in sorted(__colormap__.keys()):
212         codes = __colormap__[k].split("\033[")
213         codes = filter(lambda l: len(l) > 0, codes)
214         codes = map(lambda l: l[:-1], codes)
215         print(printc("  %s: '%s', " % (k, ";".join(codes)), k))
216     print("}")