diff --git a/src/epub_converter.py b/src/epub_converter.py index 653649d..44bc5bf 100644 --- a/src/epub_converter.py +++ b/src/epub_converter.py @@ -18,7 +18,8 @@ from html_epub_preprocessor import unwrap_structural_tags, get_tags_between_chap update_src_links_in_images, preprocess_footnotes from css_reader import clean_css, add_inline_style_to_html_soup -from livecarta_config import LawCartaConfig, BookLogger +from livecarta_config import LawCartaConfig +from util.helpers import BookLogger class EpubConverter: diff --git a/src/html_docx_preprocessor.py b/src/html_docx_preprocessor.py index 679ad4c..18b688c 100644 --- a/src/html_docx_preprocessor.py +++ b/src/html_docx_preprocessor.py @@ -7,7 +7,8 @@ from typing import List from bs4 import BeautifulSoup, NavigableString, Tag -from livecarta_config import LawCartaConfig, BookLogger, BookStatusWrapper +from livecarta_config import LawCartaConfig +from src.util.helpers import BookLogger, BookStatusWrapper class HTMLDocxPreprocessor: diff --git a/src/livecarta_config.py b/src/livecarta_config.py index 0e3935c..3820ce4 100644 --- a/src/livecarta_config.py +++ b/src/livecarta_config.py @@ -1,51 +1,3 @@ -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 @@ -110,41 +62,3 @@ class LawCartaConfig: '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 - diff --git a/src/solver.py b/src/solver.py index 0e2c04f..ef330a1 100644 --- a/src/solver.py +++ b/src/solver.py @@ -13,7 +13,8 @@ import os import pathlib from abc import abstractmethod, ABCMeta -from livecarta_config import BookLogger, BookStatusWrapper, LawCartaConfig +from livecarta_config import LawCartaConfig +from src.util.helpers import BookLogger, BookStatusWrapper class BookSolver: diff --git a/src/util/helpers.py b/src/util/helpers.py new file mode 100644 index 0000000..9516bf2 --- /dev/null +++ b/src/util/helpers.py @@ -0,0 +1,85 @@ +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__))) + folder_path = os.path.dirname(folder_path) + 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 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 \ No newline at end of file