Mejoras a logger y limpieza de email
This commit is contained in:
@ -5,6 +5,12 @@ from threading import Thread
|
||||
import queue
|
||||
|
||||
|
||||
def get_today(tz):
|
||||
today = datetime.datetime.now(tz=tz)
|
||||
locale.setlocale(locale.LC_TIME, 'es_ES')
|
||||
return today
|
||||
|
||||
|
||||
class Logger:
|
||||
"""
|
||||
Clase que lleva el diario de actividades de la secretaria
|
||||
@ -45,25 +51,31 @@ class Logger:
|
||||
self.log(msg)
|
||||
|
||||
def log_action(self, action):
|
||||
today = datetime.datetime.now(tz=self.tz)
|
||||
locale.setlocale(locale.LC_TIME, 'es_ES')
|
||||
msg = 'A las {0} del {1}, he realizado {2}'.format(today.strftime('%H:%M:%S'),
|
||||
today.strftime('%d de %B de %Y'), action)
|
||||
self.log(msg)
|
||||
msg = 'he realizado {0}'.format(action)
|
||||
self.log_msg(msg)
|
||||
|
||||
def log_not_action(self, action):
|
||||
today = datetime.datetime.now(tz=self.tz)
|
||||
locale.setlocale(locale.LC_TIME, 'es_ES')
|
||||
msg = 'A las {0} del {1}, no he podido realizar {2}'.format(today.strftime('%H:%M:%S'),
|
||||
today.strftime('%d de %B de %Y'), action)
|
||||
self.log(msg)
|
||||
msg = 'no he podido realizar {0}'.format(action)
|
||||
self.log_msg(msg)
|
||||
|
||||
def start_turn(self, action):
|
||||
self.log_msg('Inicio de turno de {0}'.format(action))
|
||||
|
||||
def end_turn(self, action):
|
||||
self.log_msg('Termino de turno de {0}'.format(action))
|
||||
|
||||
def log_msg(self, msg):
|
||||
today = get_today(self.tz)
|
||||
line = 'A las {0} del {1}, {2}'.format(today.strftime('%H:%M:%S'), today.strftime('%d de %B de %Y'), msg)
|
||||
self.log(line)
|
||||
|
||||
def log(self, message):
|
||||
line = message.rstrip('.') + '.'
|
||||
line = message
|
||||
if line[-1] != '.' and line != '--------':
|
||||
line = line.rstrip('.') + '.'
|
||||
self.messages.append(line)
|
||||
if len(self.messages) > 1000:
|
||||
self.start_new()
|
||||
self.load_last()
|
||||
with open(self.get_filename(), 'a') as f:
|
||||
f.write(line + "\n")
|
||||
|
||||
@ -78,21 +90,40 @@ class Worker(Thread):
|
||||
self.logging = params['logging']
|
||||
self.queue.put({'is_start': True})
|
||||
|
||||
def parse_message(self, message):
|
||||
if 'is_start' in message and message['is_start']:
|
||||
self.logger.start_log()
|
||||
return
|
||||
if 'action' in message:
|
||||
if 'not' in message and message['not']:
|
||||
self.logger.log_not_action(message['action'])
|
||||
return
|
||||
self.logger.log_action(message['action'])
|
||||
return
|
||||
if 'start_turn' in message:
|
||||
self.logger.start_turn(message['start_turn'])
|
||||
return
|
||||
if 'end_turn' in message:
|
||||
self.logger.end_turn(message['end_turn'])
|
||||
return
|
||||
self.logger.log_msg(message['message'])
|
||||
|
||||
def run(self):
|
||||
self.logging.log('Starting', caller=type(self))
|
||||
while not self.event.is_set():
|
||||
try:
|
||||
message = self.queue.get(timeout=self.wait)
|
||||
self.logging.log('Logger received message', caller=type(self))
|
||||
if 'is_start' in message and message['is_start']:
|
||||
self.logger.start_log()
|
||||
continue
|
||||
if 'not' in message and message['not']:
|
||||
self.logger.log_not_action(message['action'])
|
||||
continue
|
||||
self.logger.log_action(message['action'])
|
||||
self.parse_message(message)
|
||||
except queue.Empty:
|
||||
pass
|
||||
continue
|
||||
while True:
|
||||
try:
|
||||
message = self.queue.get(timeout=self.wait)
|
||||
self.logging.log('Logger received message', caller=type(self))
|
||||
self.parse_message(message)
|
||||
except queue.Empty:
|
||||
break
|
||||
self.logger.stop_log()
|
||||
self.logging.log('Exiting', caller=type(self))
|
||||
return
|
||||
|
Reference in New Issue
Block a user