Salome HOME
sat #20679 : maj doc pdf
[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__ = ([1, 4] + list(range(30, 38)) + list(range(40, 48))
44                 + list(range(90, 98)) + list(range(100, 108)))
45
46 def printc(txt, code=''):
47     '''print a text with colors
48     
49     :param txt str: The text to be printed.
50     :param code str: The color to use.
51     :return: The colored text.
52     :rtype: str
53     '''
54     # no code means 'auto mode' (works only for OK, KO, NO and ERR*)
55     if code == '':
56         striptxt = txt.strip().upper()
57         if striptxt == "OK":
58             code = COLOR_SUCCESS
59         elif striptxt in ["KO", "NO"] or striptxt.startswith("ERR"):
60             code = COLOR_ERROR
61         else:
62             return txt
63
64     # no code => output the originial text
65     if code not in __colormap__.keys() or __colormap__[code] == '':
66         return txt
67
68     return __colormap__[code] + txt + '\033[0m'
69
70 def printcInfo(txt):
71     '''print a text info color
72     
73     :param txt str: The text to be printed.
74     :return: The colored text.
75     :rtype: str
76     '''
77     return printc(txt, COLOR_INFO)
78
79 def printcError(txt):
80     '''print a text error color
81     
82     :param txt str: The text to be printed.
83     :return: The colored text.
84     :rtype: str
85     '''
86     return printc(txt, COLOR_ERROR)
87
88 def printcWarning(txt):
89     '''print a text warning color
90     
91     :param txt str: The text to be printed.
92     :return: The colored text.
93     :rtype: str
94     '''
95     return printc(txt, COLOR_WARNING)
96
97 def printcHeader(txt):
98     '''print a text header 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_HEADER)
105
106 def printcLabel(txt):
107     '''print a text label color
108     
109     :param txt str: The text to be printed.
110     :return: The colored text.
111     :rtype: str
112     '''
113     return printc(txt, COLOR_LABEL)
114
115 def printcSuccess(txt):
116     '''print a text success color
117     
118     :param txt str: The text to be printed.
119     :return: The colored text.
120     :rtype: str
121     '''
122     return printc(txt, COLOR_SUCCESS)
123
124 def printcHighlight(txt):
125     '''print a text highlight color
126     
127     :param txt str: The text to be printed.
128     :return: The colored text.
129     :rtype: str
130     '''
131     return printc(txt, COLOR_HIGLIGHT)
132
133 def cleancolor(message):
134     '''remove color from a colored text.
135     
136     :param message str: The text to be cleaned.
137     :return: The cleaned text.
138     :rtype: str
139     '''
140     if message == None:
141         return message
142     
143     message = message.replace('\033[0m', '')
144     for i in __code_range__:
145         message = message.replace('\033[%dm' % i, '')
146     return message
147
148 def print_value(logger, label, value, level=1, suffix=""):
149     '''shortcut method to print a label and a value with the info color
150     
151     :param logger class logger: the logger instance.
152     :param label int: the label to print.
153     :param value str: the value to print.
154     :param level int: the level of verboseness.
155     :param suffix str: the suffix to add at the end.
156     '''
157     if type(value) is list:
158         skip = "\n     "
159         strValue = ""
160         i = 0
161         for v in value:
162           strValue += "%15s, " % str(v)
163           i += 1
164           if i >= 5:
165             strValue += skip
166             i = 0
167         if len(value) > 5:
168             strValue = skip + strValue
169     else:
170         strValue = str(value)
171     strValue = printcInfo(strValue)
172     if logger is None:
173         print("  %s = %s %s" % (label, strValue, suffix))
174     else:
175         logger.write("  %s = %s %s\n" % (label, strValue, suffix), level)
176
177 def print_color_range(start, end):
178     '''print possible range values for colors
179     
180     :param start int: The smaller value.
181     :param end int: The bigger value.
182     '''
183     for k in range(start, end+1):
184         print("\033[%dm%3d\033[0m" % (k, k),)
185     print
186
187 # This method prints the color map
188 def print_color_map():
189     '''This method prints the color map
190     '''
191     print("colormap:")
192     print("{")
193     for k in sorted(__colormap__.keys()):
194         codes = __colormap__[k].split('\033[')
195         codes = filter(lambda l: len(l) > 0, codes)
196         codes = map(lambda l: l[:-1], codes)
197         print(printc("  %s: '%s', " % (k, ';'.join(codes)), k))
198     print("}")
199
200