forked from LiveCarta/BookConverter
epub converter: separate BookLogger, BookStatusWrapper
This commit is contained in:
@@ -18,7 +18,8 @@ from html_epub_preprocessor import unwrap_structural_tags, get_tags_between_chap
|
|||||||
update_src_links_in_images, preprocess_footnotes
|
update_src_links_in_images, preprocess_footnotes
|
||||||
|
|
||||||
from css_reader import clean_css, add_inline_style_to_html_soup
|
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:
|
class EpubConverter:
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ from typing import List
|
|||||||
|
|
||||||
from bs4 import BeautifulSoup, NavigableString, Tag
|
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:
|
class HTMLDocxPreprocessor:
|
||||||
|
|||||||
@@ -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:
|
class LawCartaConfig:
|
||||||
SUPPORTED_LEVELS = 5
|
SUPPORTED_LEVELS = 5
|
||||||
@@ -110,41 +62,3 @@ class LawCartaConfig:
|
|||||||
'grey': 'darkGray',
|
'grey': 'darkGray',
|
||||||
}
|
}
|
||||||
INDENT = '30px'
|
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
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import os
|
|||||||
import pathlib
|
import pathlib
|
||||||
from abc import abstractmethod, ABCMeta
|
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:
|
class BookSolver:
|
||||||
|
|||||||
85
src/util/helpers.py
Normal file
85
src/util/helpers.py
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user