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