22import curses
33from sys import version_info
44import logging
5+ import threading
56
67logger = logging .getLogger (__name__ )
78
@@ -19,7 +20,7 @@ class Log(object):
1920
2021 _color_change = False
2122
22- lock = None
23+ lock = threading . Lock ()
2324
2425 def __init__ (self ):
2526 self .width = None
@@ -41,110 +42,111 @@ def _do_i_print_last_char(self, first_print):
4142 pass
4243 return first_print
4344
44- def write (self , msg = None , suffix = None , counter = None , help_msg = False , notify_function = None ):
45+ def write (self , msg = None , suffix = None , counter = None , help_msg = False , error_msg = False , notify_function = None ):
4546 if self .asked_to_stop :
4647 self .counter = None
4748 return
4849 if self .cursesScreen :
49- #if logger.isEnabledFor(logging.DEBUG):
50- # logger.debug('before ----------------------------')
51- # logger.debug('msg = "{}"'.format(msg))
52- # logger.debug('self.msg = "{}"'.format(self.msg))
53- # logger.debug('suffix = "{}"'.format(suffix))
54- # logger.debug('self.suffix = "{}"'.format(self.suffix))
55- # logger.debug('counter = "{}"'.format(counter))
56- # logger.debug('self.counter = "{}"'.format(self.counter))
57-
58- first_print = True
59- if msg is not None : self .msg = msg
60- if suffix is not None : self .suffix = suffix
61- if counter is not None : self .counter = counter
62-
63- #if logger.isEnabledFor(logging.DEBUG):
64- # logger.debug('after ----------------------------')
65- # logger.debug('msg = "{}"'.format(msg))
66- # logger.debug('self.msg = "{}"'.format(self.msg))
67- # logger.debug('suffix = "{}"'.format(suffix))
68- # logger.debug('self.suffix = "{}"'.format(self.suffix))
69- # logger.debug('counter = "{}"'.format(counter))
70- # logger.debug('self.counter = "{}"'.format(self.counter))
71-
72- """ update main message """
73- if self .msg :
74- if self .lock is not None :
75- self .lock .acquire ()
76- self .cursesScreen .erase ()
77- try :
78- d_msg = self .msg .strip ()[0 : self .width ].replace ("\r " , "" ).replace ("\n " , "" )
79- self .cursesScreen .addstr (0 , 1 , d_msg )
80- except :
50+ with self .lock :
51+ #if logger.isEnabledFor(logging.DEBUG):
52+ # logger.debug('before ----------------------------')
53+ # logger.debug('msg = "{}"'.format(msg))
54+ # logger.debug('self.msg = "{}"'.format(self.msg))
55+ # logger.debug('suffix = "{}"'.format(suffix))
56+ # logger.debug('self.suffix = "{}"'.format(self.suffix))
57+ # logger.debug('counter = "{}"'.format(counter))
58+ # logger.debug('self.counter = "{}"'.format(self.counter))
59+
60+ first_print = True
61+ if msg is not None : self .msg = msg
62+ if suffix is not None : self .suffix = suffix
63+ if counter is not None : self .counter = counter
64+ self .error_msg = True if error_msg else False
65+
66+ #if logger.isEnabledFor(logging.DEBUG):
67+ # logger.debug('after ----------------------------')
68+ # logger.debug('msg = "{}"'.format(msg))
69+ # logger.debug('self.msg = "{}"'.format(self.msg))
70+ # logger.debug('suffix = "{}"'.format(suffix))
71+ # logger.debug('self.suffix = "{}"'.format(self.suffix))
72+ # logger.debug('counter = "{}"'.format(counter))
73+ # logger.debug('self.counter = "{}"'.format(self.counter))
74+
75+ if self .asked_to_stop :
76+ self .counter = None
77+ return
78+ """ update main message """
79+ if self .msg :
80+ self .cursesScreen .erase ()
8181 try :
82- d_msg = self .msg .encode ( 'utf-8' , 'replace' ). strip ()[0 : self .width ].replace ("\r " , "" ).replace ("\n " , "" )
82+ d_msg = self .msg .strip ()[0 : self .width ].replace ("\r " , "" ).replace ("\n " , "" )
8383 self .cursesScreen .addstr (0 , 1 , d_msg )
8484 except :
85- if logger .isEnabledFor (logging .ERROR ):
86- logger .error ('Cannot update the Status Bar...' )
87- if logger .isEnabledFor (logging .DEBUG ):
88- try :
89- logger .debug ('Status: "{}"' .format (self .msg ))
90- except :
91- pass
92- self .cursesScreen .refresh ()
93- if self .lock is not None :
94- self .lock .release ()
95-
96- self ._active_width = self .width
97-
98- """ display suffix """
99- if self .suffix :
100- d_msg = ' [' + self .suffix + ']'
101- if self .lock is not None :
102- self .lock .acquire ()
103- self .cursesScreen .addstr (0 , self ._active_width - len (d_msg ), d_msg )
104- self .cursesScreen .chgat (0 , self ._active_width - len (d_msg ) + 1 , len (d_msg ) - 1 , curses .color_pair (1 ))
105- first_print = self ._do_i_print_last_char (first_print )
106- self .cursesScreen .refresh ()
107- if self .lock is not None :
108- self .lock .release ()
109- self ._active_width -= len (d_msg )
110- if logger .isEnabledFor (logging .DEBUG ):
111- logger .debug ('Suffix: {}' .format (self .suffix ))
112-
113- """ display counter """
114- if self .counter :
115- if self .counter == '0' :
85+ try :
86+ d_msg = self .msg .encode ('utf-8' , 'replace' ).strip ()[0 : self .width ].replace ("\r " , "" ).replace ("\n " , "" )
87+ self .cursesScreen .addstr (0 , 1 , d_msg )
88+ except :
89+ if logger .isEnabledFor (logging .ERROR ):
90+ logger .error ('Cannot update the Status Bar...' )
91+ if logger .isEnabledFor (logging .DEBUG ):
92+ try :
93+ logger .debug ('Status: "{}"' .format (self .msg ))
94+ except :
95+ pass
96+
97+ self ._active_width = self .width
98+
99+ if self .asked_to_stop :
116100 self .counter = None
117- if self .counter :
118- if self .suffix :
119- self ._active_width += 1
120- d_msg = ' [' + self .counter + ']'
121- if self .lock is not None :
122- self .lock .acquire ()
101+ return
102+ """ display suffix """
103+ if self .suffix :
104+ d_msg = ' [' + self .suffix + ']'
123105 self .cursesScreen .addstr (0 , self ._active_width - len (d_msg ), d_msg )
106+ self .cursesScreen .chgat (0 , self ._active_width - len (d_msg ) + 1 , len (d_msg ) - 1 , curses .color_pair (1 ))
124107 first_print = self ._do_i_print_last_char (first_print )
125108 self .cursesScreen .refresh ()
126- if self .lock is not None :
127- self .lock .release ()
128109 self ._active_width -= len (d_msg )
129- if logger .isEnabledFor (logging .DEBUG ):
130- logger .debug ('Counter: {}' .format (self .counter ))
131-
132- """ display press ? """
133- if help_msg or self .display_help_message :
134- self .counter = None
135- suffix_string = ' Press ? for help'
136- if self .lock is not None :
137- self .lock .acquire ()
138- self .cursesScreen .addstr (0 , self ._active_width - len (suffix_string ), suffix_string )
139- self .cursesScreen .refresh ()
140- if self .lock is not None :
141- self .lock .release ()
142- self .display_help_message = True
143110 if logger .isEnabledFor (logging .DEBUG ):
144- logger .debug ('Press ? for help: yes' )
145- else :
111+ logger .debug ('Suffix: {}' .format (self .suffix ))
112+
113+ """ display counter """
114+ if self .counter :
115+ if self .counter == '0' :
116+ self .counter = None
117+ if self .counter :
118+ if self .suffix :
119+ self ._active_width += 1
120+ d_msg = ' [' + self .counter + ']'
121+ self .cursesScreen .addstr (0 , self ._active_width - len (d_msg ), d_msg )
122+ first_print = self ._do_i_print_last_char (first_print )
123+ self .cursesScreen .refresh ()
124+ self ._active_width -= len (d_msg )
125+ self .display_help_message = False
146126 if logger .isEnabledFor (logging .DEBUG ):
147- logger .debug ('Press ? for help: no' )
127+ logger .debug ('Counter: {}' .format (self .counter ))
128+
129+ if self .asked_to_stop :
130+ self .counter = None
131+ return
132+ """ display press ? """
133+ if help_msg or self .display_help_message :
134+ if not self .error_msg :
135+ self .counter = None
136+ suffix_string = ' Press ? for help'
137+ self .cursesScreen .addstr (0 , self ._active_width - len (suffix_string ), suffix_string )
138+ self .cursesScreen .refresh ()
139+ self .display_help_message = True
140+ if logger .isEnabledFor (logging .DEBUG ):
141+ logger .debug ('Press ? for help: yes' )
142+ else :
143+ if logger .isEnabledFor (logging .DEBUG ):
144+ logger .debug ('Press ? for help: no' )
145+
146+ if self .asked_to_stop :
147+ self .counter = None
148+ return
149+ self .cursesScreen .refresh ()
148150
149151 def readline (self ):
150152 pass
0 commit comments