forked from LiveCarta/BookConverter
Annotations in Epub converter
This commit is contained in:
@@ -1,51 +1,60 @@
|
||||
import os
|
||||
import logging
|
||||
from typing import Union
|
||||
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
""" Class to prettify logger and command line output """
|
||||
MAPPING = {
|
||||
'DEBUG': 37, # white
|
||||
'INFO': 36, # cyan
|
||||
'WARNING': 33, # yellow
|
||||
'ERROR': 31, # red
|
||||
'CRITICAL': 41, # white on red bg
|
||||
"DEBUG": 37, # white
|
||||
"INFO": 36, # cyan
|
||||
"WARNING": 33, # yellow
|
||||
"ERROR": 31, # red
|
||||
"CRITICAL": 41, # white on red bg
|
||||
}
|
||||
|
||||
PREFIX = '\033['
|
||||
SUFFIX = '\033[0m'
|
||||
PREFIX = "\033["
|
||||
SUFFIX = "\033[0m"
|
||||
|
||||
def __init__(self, pattern):
|
||||
logging.Formatter.__init__(self, pattern)
|
||||
|
||||
def format(self, record):
|
||||
seq = self.MAPPING.get(record.levelname, 37) # default white
|
||||
record.levelname = '{0}{1}m{2}{3}' \
|
||||
record.levelname = "{0}{1}m{2}{3}" \
|
||||
.format(self.PREFIX, seq, record.levelname, self.SUFFIX)
|
||||
return logging.Formatter.format(self, record)
|
||||
|
||||
|
||||
class BookLogger:
|
||||
def __init__(self, name, book_id, main_logger=None,
|
||||
filemode='w+', logging_level=logging.INFO,
|
||||
logging_format='%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d in %(funcName)s]'):
|
||||
def __init__(self, name: str, book_id: Union[int, str], main_logger: logging.Logger = None,
|
||||
filemode: str = "w+", logging_level: int = logging.INFO,
|
||||
logging_format: str = "%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d in %(funcName)s]"):
|
||||
"""
|
||||
Method for Logger configuration. Logger will write to file.
|
||||
:param name: name of the Logger.
|
||||
:param attr_name: name of attribute that will be added to self.
|
||||
:param filename: name of the log file.
|
||||
:param filemode: mode of opening log file.
|
||||
:param logging_level: logging level: 10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical.
|
||||
:param logging_format: format of record in log file.
|
||||
Parameters
|
||||
----------
|
||||
name: str
|
||||
name of the Logger
|
||||
book_id: Union[int, str]
|
||||
id of the book
|
||||
main_logger: Logger
|
||||
main logger of the converter
|
||||
filemode: str
|
||||
mode of opening log file.
|
||||
logging_level: int
|
||||
logging level: 10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical
|
||||
logging_format: str
|
||||
format of record in log file
|
||||
|
||||
"""
|
||||
self.main_logger = main_logger
|
||||
|
||||
self.logger = logging.getLogger(name)
|
||||
self.logger.propagate = False
|
||||
folder_path = os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__)))
|
||||
folder_path = os.path.dirname(folder_path)
|
||||
filename = f'logs/{book_id}.log'
|
||||
filename = f"logs/{book_id}.log"
|
||||
file_path = os.path.join(folder_path, filename)
|
||||
file_handler = logging.FileHandler(file_path, mode=filemode)
|
||||
file_format = logging.Formatter(logging_format)
|
||||
@@ -58,42 +67,46 @@ class BookLogger:
|
||||
self.logger.addHandler(stream_handler)
|
||||
self.logger.setLevel(logging_level)
|
||||
|
||||
def log(self, message, logging_level=20):
|
||||
def log(self, message: str, logging_level: int = 20):
|
||||
"""
|
||||
Method for logging.
|
||||
Parameters
|
||||
----------
|
||||
message: str
|
||||
body of the message
|
||||
logging_level: int
|
||||
level of logging
|
||||
|
||||
:param message: body of the message
|
||||
:param logging_level: level of logging
|
||||
"""
|
||||
self.logger.log(msg=message, level=logging_level, stacklevel=2)
|
||||
|
||||
def log_error_to_main_log(self, message=''):
|
||||
def log_error_to_main_log(self, message: str = ""):
|
||||
""" Method for logging error to main log file. """
|
||||
if self.main_logger:
|
||||
if not message:
|
||||
message = f'Error in book conversion. Check log file.'
|
||||
message = f"Error in book conversion. Check log file."
|
||||
self.main_logger.error(message)
|
||||
|
||||
|
||||
class BookStatusWrapper:
|
||||
"""Class sets/updates statuses of Converter on Platform"""
|
||||
|
||||
def __init__(self, access, logger_object, book_id=0):
|
||||
def __init__(self, access, logger_object: BookLogger, book_id: int = 0):
|
||||
self.access = access
|
||||
self.logger_object = logger_object
|
||||
self.book_id = book_id
|
||||
|
||||
def set_status(self, status: str):
|
||||
str_2_status = {
|
||||
'[PROCESS]': self.access.PROCESS,
|
||||
'[GENERATE]': self.access.GENERATE,
|
||||
'[ERROR]': self.access.ERROR
|
||||
"[PROCESS]": self.access.PROCESS,
|
||||
"[GENERATE]": self.access.GENERATE,
|
||||
"[ERROR]": self.access.ERROR
|
||||
}
|
||||
|
||||
try:
|
||||
if self.access:
|
||||
self.access.update_status(self.book_id, str_2_status[status])
|
||||
self.logger_object.log(f'Status has been updated to {status}.')
|
||||
self.logger_object.log(f"Status has been updated to {status}.")
|
||||
except Exception as exc:
|
||||
self.logger_object.log(
|
||||
f"Can't update status of the book {status}.", logging.ERROR)
|
||||
@@ -101,10 +114,10 @@ class BookStatusWrapper:
|
||||
raise exc
|
||||
|
||||
def set_processing(self):
|
||||
self.set_status('[PROCESS]')
|
||||
self.set_status("[PROCESS]")
|
||||
|
||||
def set_generating(self):
|
||||
self.set_status('[GENERATE]')
|
||||
self.set_status("[GENERATE]")
|
||||
|
||||
def set_error(self):
|
||||
self.set_status('[ERROR]')
|
||||
self.set_status("[ERROR]")
|
||||
|
||||
Reference in New Issue
Block a user