Belle II Software  release-08-01-10
colours.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ------------------------------------------------------------------------
13 # Class holding colours for printing coloured text to terminal and
14 # printing functions, also handles the general log
15 # ------------------------------------------------------------------------
16 
17 import settings
18 import os
19 
20 # settings imported from the settings file
21 logfile = settings.logfile
22 
23 # handle the log filename
24 if not settings.overwrite:
25  if os.path.exists(logfile):
26  for i in range(10000):
27  if not os.path.exists(logfile + '.' + str(i)):
28  logfile = logfile + '.' + str(i)
29  break
30 
31 # open the log
32 log = open(logfile, 'w')
33 failed = False
34 
35 
36 # --------------------------------------
37 # Class holding colours for printing coloured text to terminal
38 # --------------------------------------
39 
40 class bcolours:
41  """
42  Class to define colour format of output message
43  Parameters:
44  HEADER colour code for header
45  OKBLUE colour code for ok blue message
46  OKGREEN colour code for ok green message
47  WARNING colour code for warning message
48  FAIL colour code for fail message
49  ENDC colour code for end message
50  """
51 
52  HEADER = '\033[95m'
53 
54  OKBLUE = '\033[94m'
55 
56  OKGREEN = '\033[92m'
57 
58  WARNING = '\033[100;93m'
59 
60  FAIL = '\033[100;91m'
61 
62  ENDC = '\033[0m'
63 
64  def disable(self):
65  """
66  Disable colour output format
67  """
68 
69  self.HEADERHEADERHEADER = ''
70 
71  self.OKBLUEOKBLUEOKBLUE = ''
72 
73  self.OKGREENOKGREENOKGREEN = ''
74 
75  self.WARNINGWARNINGWARNING = ''
76 
77  self.FAILFAILFAIL = ''
78 
79  self.ENDCENDCENDC = ''
80 
81  def enable(self):
82  """
83  Enable colour output format
84  """
85 
86  self.HEADERHEADERHEADER = ''
87 
88  self.OKBLUEOKBLUEOKBLUE = ''
89 
90  self.OKGREENOKGREENOKGREEN = ''
91 
92  self.WARNINGWARNINGWARNING = ''
93 
94  self.FAILFAILFAIL = ''
95 
96  self.ENDCENDCENDC = ''
97 
98 
99 if not settings.enable_colours:
100  bcolours.disable()
101 
102 
103 # --------------------------------------
104 # Text Functions
105 # --------------------------------------
106 
107 def query(string):
108  """
109  print a query, wait, at the end of the dots for either fail() or done()
110  """
111 
112  string = str(string) + (60 - len(str(string))) * '.'
113  print(string, end=' ')
114 
115  # try to log it
116  try:
117  log.write(string)
118  except BaseException:
119  pass
120 
121  failed = False # noqa
122 
123 
124 def openlog():
125  """
126  open the logfile manually again after it has been closed
127  """
128 
129  globals()['log'] = open(logfile, 'a')
130  query('Opening logfile <' + logfile + '>')
131  done()
132 
133 
134 query('Opening logfile <' + logfile + '> and loading colours')
135 
136 
137 def mesg(string):
138  """
139  Simple announcing information debug function, in white
140  """
141 
142  string = str(string)
143  print(string)
144  log.write(string + '\n')
145 
146 
147 def stop(string):
148  """
149  Most serious error announcing function, in red
150  """
151 
152  string = str(string)
153  print(bcolours.FAIL + string + bcolours.ENDC)
154  # log it
155  log.write(string + '\n')
156 
157 
158 def done():
159  """
160  Prints DONE in green letters - to be used in conjunction with query() above
161  """
162 
163  print(bcolours.OKGREEN + 'DONE' + bcolours.ENDC)
164 
165  # try to log it
166  try:
167  log.write('DONE\n')
168  except BaseException:
169  pass
170 
171 
172 def fail(list):
173  """
174  Prints FAIL in red letters and follows with debug information if they were
175  passed in the list[strings] as an argument, to be used in conjunction with
176  query() above
177  """
178 
179  print(bcolours.FAIL + 'FAIL' + bcolours.ENDC)
180  log.write('FAIL\n')
181 
182  # print the debug info
183  if list != []:
184  print(bcolours.WARNING + 'Printing debug info:\n')
185 
186  # log it
187  log.write('Debug info:\n')
188  for foo in list:
189  print(foo)
190  log.write(str(foo) + '\n')
191  print(bcolours.ENDC)
192 
193 
194 def warning(string):
195  """
196  Prints a yellow warning string that is passed as the argument, moderate problem
197  """
198 
199  string = str(string)
200  # print if runlevel is 1 or higher
201  print(bcolours.WARNING + string + bcolours.ENDC)
202  log.write(string + '\n')
203 
204 
205 def closeLog():
206  """
207  closes log after it's been opened manually
208  """
209  log.close()
210 
211 
212 done()
WARNING
colour code for warning message
Definition: colours.py:75
string HEADER
colour code for header
Definition: colours.py:52
OKBLUE
colour code for ok blue message
Definition: colours.py:71
HEADER
colour code for header
Definition: colours.py:69
string OKGREEN
colour code for ok green message
Definition: colours.py:56
string WARNING
colour code for warning message
Definition: colours.py:58
def enable(self)
Definition: colours.py:81
OKGREEN
colour code for ok green message
Definition: colours.py:73
string ENDC
colour code for end message
Definition: colours.py:62
FAIL
colour code for fail message
Definition: colours.py:77
ENDC
colour code for end message
Definition: colours.py:79
string FAIL
colour code for fail message
Definition: colours.py:60
def disable(self)
Definition: colours.py:64
string OKBLUE
colour code for ok blue message
Definition: colours.py:54