forked from LiveCarta/BookConverter
- split book class into html-preprocessor, json-converter, book(with main flow) classes. - pick out logging, law carta setup, updating status via api into separate objects - in html-preprocesser: add cleaning hrefs
112 lines
4.2 KiB
Python
112 lines
4.2 KiB
Python
import logging
|
|
import os
|
|
|
|
from access import Access
|
|
|
|
|
|
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 in 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 = 4
|
|
SUPPORTED_HEADERS = {"h1", "h2", "h3", "h4"}
|
|
HEADERS_LEVELS = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9"}
|
|
|
|
# Main constant values
|
|
DEFAULT_FONT_NAME = 'Times New Roman'
|
|
DEFAULT_ALIGN_STYLE = '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"
|
|
}
|
|
|
|
|
|
class BookApiWrapper:
|
|
def __init__(self, access, logger_object, book_id=0):
|
|
self.access: Access = access
|
|
self.logger_object = logger_object
|
|
self.book_id = book_id
|
|
|
|
def set_process_status(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_generate_status(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_status(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
|
|
|