forked from LiveCarta/BookConverter
Fix logs output
This commit is contained in:
@@ -28,10 +28,10 @@ class BookSolver:
|
||||
self.preset_path = None
|
||||
self.book_path = None # path to book file, appears after downloading from server
|
||||
self.book_output_path = None # path to json file
|
||||
self.logger_object = BookLogger(name=f"{__name__}_{self.book_id}")
|
||||
self.logger_object.configure_book_logger(book_id=book_id)
|
||||
self.book_logger = BookLogger(name=f"{__name__}_{self.book_id}")
|
||||
self.book_logger.configure_book_logger(book_id=book_id)
|
||||
self.status_wrapper = BookStatusWrapper(
|
||||
access, self.logger_object, book_id)
|
||||
access, self.book_logger, book_id)
|
||||
|
||||
assert LiveCartaConfig.SUPPORTED_LEVELS == len(LiveCartaConfig.SUPPORTED_HEADERS), \
|
||||
"Length of headers doesn't match allowed levels."
|
||||
@@ -64,12 +64,12 @@ class BookSolver:
|
||||
try:
|
||||
with open(file_path, "wb+") as file:
|
||||
file.write(content)
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"File was saved to folder: {folder_path}.")
|
||||
except Exception as exc:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"Error in writing {self.book_type} file.", logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log()
|
||||
self.book_logger.log_error_to_main_log()
|
||||
raise exc
|
||||
return file_path
|
||||
|
||||
@@ -86,9 +86,9 @@ class BookSolver:
|
||||
# self.preset_path = pathlib.Path(
|
||||
# str(self.save_file(content, path_to_save="preset", file_type="json")))
|
||||
except FileNotFoundError as f_err:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Can't get preset file from server.", logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log()
|
||||
self.book_logger.log_error_to_main_log()
|
||||
raise f_err
|
||||
except Exception as exc:
|
||||
raise exc
|
||||
@@ -96,17 +96,17 @@ class BookSolver:
|
||||
def get_book_file(self):
|
||||
"""Method for getting and saving book from server"""
|
||||
try:
|
||||
self.logger_object.log(f"Start receiving book file from server. URL:"
|
||||
self.book_logger.log(f"Start receiving book file from server. URL:"
|
||||
f" {self.access.url}/doc-convert/{self.book_id}/file")
|
||||
content = self.access.get_file(
|
||||
file_path=f"{self.access.url}/doc-convert/{self.book_id}/file")
|
||||
self.logger_object.log("Book file was received from server.")
|
||||
self.book_logger.log("Book file was received from server.")
|
||||
self.book_path = pathlib.Path(self.save_file(
|
||||
content, path_to_save=f"books/{self.book_type}", file_type=self.book_type))
|
||||
except FileNotFoundError as f_err:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Can't get book file from server.", logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log()
|
||||
self.book_logger.log_error_to_main_log()
|
||||
raise f_err
|
||||
except Exception as exc:
|
||||
raise exc
|
||||
@@ -120,7 +120,7 @@ class BookSolver:
|
||||
self.book_output_path = output_path
|
||||
|
||||
self.book_output_path = pathlib.Path(self.book_output_path)
|
||||
self.logger_object.log(f"Output file path: {self.book_output_path}")
|
||||
self.book_logger.log(f"Output file path: {self.book_output_path}")
|
||||
|
||||
pathlib.Path(self.book_output_path).parent.mkdir(
|
||||
parents=True, exist_ok=True)
|
||||
@@ -131,27 +131,27 @@ class BookSolver:
|
||||
try:
|
||||
with codecs.open(self.book_output_path, "w", encoding="utf-8") as f:
|
||||
json.dump(content, f, ensure_ascii=False)
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"Data has been saved to .json file: {self.book_output_path}")
|
||||
except Exception as exc:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Error has occurred while writing .json file." + str(exc), logging.ERROR)
|
||||
|
||||
def send_json_content_to_server(self, content: Dict[str, List[Dict[str, Union[List, str]]]]):
|
||||
"""Function sends json_content to site"""
|
||||
try:
|
||||
self.access.send_book(self.book_id, content)
|
||||
self.logger_object.log(f"JSON data has been sent to server.")
|
||||
self.book_logger.log(f"JSON data has been sent to server.")
|
||||
except Exception as exc:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Error has occurred while sending json content.", logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log()
|
||||
self.book_logger.log_error_to_main_log()
|
||||
self.status_wrapper.set_error()
|
||||
raise exc
|
||||
|
||||
@abstractmethod
|
||||
def get_converted_book(self) -> Dict[str, List[Dict[str, Union[List, str]]]]:
|
||||
self.logger_object.log("Beginning of processing .json output.")
|
||||
self.book_logger.log("Beginning of processing .json output.")
|
||||
self.status_wrapper.set_generating()
|
||||
return {}
|
||||
|
||||
@@ -165,23 +165,23 @@ class BookSolver:
|
||||
try:
|
||||
self.get_preset_file()
|
||||
self.get_book_file()
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"Beginning of conversion from .{self.book_type} to .json.")
|
||||
self.status_wrapper.set_processing()
|
||||
content_dict: Dict[str, List[Dict[Union[str, List]]]] = self.get_converted_book()
|
||||
# todo add delete of preset path
|
||||
[os.remove(path) for path in [self.book_path]]
|
||||
self.logger_object.log("Beginning of processing .json output.")
|
||||
self.book_logger.log("Beginning of processing .json output.")
|
||||
self.status_wrapper.set_generating()
|
||||
self.write_to_json(content_dict)
|
||||
self.send_json_content_to_server(content_dict)
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"End of the conversion to LiveCarta format. Check {self.book_output_path}.")
|
||||
except Exception as exc:
|
||||
self.status_wrapper.set_error()
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Error has occurred while conversion.", logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log(str(exc))
|
||||
self.book_logger.log_error_to_main_log(str(exc))
|
||||
raise exc
|
||||
|
||||
def conversion_local(self, file_path: str):
|
||||
@@ -192,17 +192,17 @@ class BookSolver:
|
||||
|
||||
"""
|
||||
try:
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
f"Data has been downloaded from {file_path} file")
|
||||
self.status_wrapper.set_processing()
|
||||
with codecs.open(file_path, "r", encoding="utf-8") as f_json:
|
||||
content_dict = json.load(f_json)
|
||||
self.logger_object.log("Beginning of processing .json output.")
|
||||
self.book_logger.log("Beginning of processing .json output.")
|
||||
self.status_wrapper.set_generating()
|
||||
self.send_json_content_to_server(content_dict)
|
||||
self.logger_object.log(f"Sent a file to server. Check LiveCarta.")
|
||||
self.book_logger.log(f"Sent a file to server. Check LiveCarta.")
|
||||
except Exception as exc:
|
||||
self.status_wrapper.set_error()
|
||||
self.logger_object.log(
|
||||
self.book_logger.log(
|
||||
"Error has occurred while reading json file." + str(exc), logging.ERROR)
|
||||
self.logger_object.log_error_to_main_log(str(exc))
|
||||
self.book_logger.log_error_to_main_log(str(exc))
|
||||
|
||||
Reference in New Issue
Block a user