forked from LiveCarta/BookConverter
151 lines
5.3 KiB
Python
151 lines
5.3 KiB
Python
import logging
|
|
import os
|
|
|
|
|
|
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'):
|
|
"""
|
|
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.
|
|
"""
|
|
self.main_logger = main_logger
|
|
|
|
self.logger = logging.getLogger(name)
|
|
folder_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
filename = f'logs/{book_id}_log.log'
|
|
file_path = os.path.join(folder_path, filename)
|
|
file_handler = logging.FileHandler(file_path, mode=filemode)
|
|
file_format = logging.Formatter(fmt=logging_format)
|
|
file_handler.setFormatter(file_format)
|
|
self.logger.addHandler(file_handler)
|
|
self.logger.setLevel(logging_level)
|
|
|
|
def log(self, message, logging_level=20):
|
|
"""
|
|
Method for logging.
|
|
|
|
:param message: body of the message
|
|
:param logging_level: level of logging
|
|
"""
|
|
self.logger.log(msg=message, level=logging_level)
|
|
|
|
def log_error_to_main_log(self, message=''):
|
|
"""
|
|
Method for logging error to main log file.
|
|
"""
|
|
if self.main_logger:
|
|
if not message:
|
|
message = f'Error in book conversion. Check log file.'
|
|
self.main_logger.error(message)
|
|
|
|
|
|
class LawCartaConfig:
|
|
SUPPORTED_LEVELS = 5
|
|
SUPPORTED_HEADERS = {"h1", "h2", "h3", "h4", "h5"}
|
|
HEADERS_LEVELS = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"}
|
|
|
|
# Main constant values
|
|
DEFAULT_FONT_NAME = 'Times New Roman'
|
|
DEFAULT_ALIGN_STYLE = 'left'
|
|
ALIGN_STYLES = ['justify', 'right', 'center', 'left']
|
|
WORD_DEFAULT_FONT_SIZE = 11
|
|
LAWCARTA_DEFAULT_FONT_SIZE = 18
|
|
FONT_CONVERT_RATIO = LAWCARTA_DEFAULT_FONT_SIZE / WORD_DEFAULT_FONT_SIZE
|
|
font_correspondence_table = {
|
|
"Arial": "arial,helvetica,sans-serif",
|
|
"Comic Sans MS": "comic sans ms,cursive",
|
|
"Courier New": "courier new,courier,monospace",
|
|
"Georgia": "georgia,serif",
|
|
"Lucida Sans Unicode": "lucida sans unicode,lucida grande,sans-serif",
|
|
"Tahoma": "tahoma,geneva,sans-serif",
|
|
"Times New Roman": "times new roman,times,serif",
|
|
"Trebuchet MS": "trebuchet ms,helvetica,sans-serif",
|
|
"Verdana": "verdana,geneva,sans-serif",
|
|
"monospace": "courier new,courier,monospace",
|
|
"sans-serif": "arial,helvetica,sans-serif",
|
|
}
|
|
|
|
COLORS_MAP = {
|
|
'#ffff00': 'yellow',
|
|
'#00ff00': 'darkYellow',
|
|
'#00ffff': 'cyan',
|
|
'#ff00ff': 'magenta',
|
|
'#0000ff': 'blue',
|
|
'#ff0000': 'red',
|
|
'#000080': 'darkBlue',
|
|
'#008080': 'darkCyan',
|
|
'#008000': 'green',
|
|
'#800080': 'darkMagenta',
|
|
'#808000': 'darkGreen',
|
|
'#c0c0c0': 'lightGray',
|
|
'#ffffff': 'white',
|
|
'#800000': '#800000',
|
|
'#808080': '#808080'
|
|
}
|
|
|
|
HTML42LIVECARTA_COLORS = {
|
|
'yellow': 'yellow',
|
|
'lime': 'green',
|
|
'aqua': 'cyan',
|
|
'fuchsia': 'magenta',
|
|
'blue': 'blue',
|
|
'red': 'red',
|
|
'navy': 'darkBlue',
|
|
'teal': 'darkCyan',
|
|
'green': 'darkGreen',
|
|
'purple': 'darkMagenta',
|
|
'olive': 'darkYellow',
|
|
'silver': 'lightGray',
|
|
'white': 'white',
|
|
'maroon': 'darkRed', # '#800000',
|
|
'gray': 'darkGray',
|
|
'grey': 'darkGray',
|
|
}
|
|
INDENT = '30px'
|
|
|
|
|
|
class BookStatusWrapper:
|
|
def __init__(self, access, logger_object, book_id=0):
|
|
self.access = access
|
|
self.logger_object = logger_object
|
|
self.book_id = book_id
|
|
|
|
def set_processing(self):
|
|
try:
|
|
if self.access:
|
|
self.access.update_status(self.book_id, self.access.PROCESS)
|
|
self.logger_object.log(f'Status has been updated to [PROCESS].')
|
|
except Exception as exc:
|
|
self.logger_object.log("Can't update status of the book [PROCESS].", logging.ERROR)
|
|
self.logger_object.log_error_to_main_log()
|
|
raise exc
|
|
|
|
def set_generating(self):
|
|
try:
|
|
if self.access:
|
|
self.access.update_status(self.book_id, self.access.GENERATE)
|
|
self.logger_object.log(f'Status has been updated to [GENERATE].')
|
|
except Exception as exc:
|
|
self.logger_object.log("Can't update status of the book [GENERATE].", logging.ERROR)
|
|
self.logger_object.log_error_to_main_log()
|
|
raise exc
|
|
|
|
def set_error(self):
|
|
try:
|
|
if self.access:
|
|
self.access.update_status(self.book_id, self.access.ERROR)
|
|
self.logger_object.log(f'Status has been updated to [ERROR].')
|
|
except Exception as exc:
|
|
self.logger_object.log("Can't update status of the book [ERROR].", logging.ERROR)
|
|
self.logger_object.log_error_to_main_log()
|
|
raise exc
|
|
|