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