forked from LiveCarta/BookConverter
Annotations in Epub converter
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
||||
import codecs
|
||||
import logging
|
||||
import pathlib
|
||||
from typing import List, Dict, Union
|
||||
from abc import abstractmethod, ABCMeta
|
||||
|
||||
from src.livecarta_config import LiveCartaConfig
|
||||
@@ -20,7 +21,7 @@ class BookSolver:
|
||||
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
def __init__(self, book_id=0, access=None, main_logger=None):
|
||||
def __init__(self, book_id: int = 0, access=None, main_logger=None):
|
||||
self.book_type = None
|
||||
self.book_id = book_id
|
||||
self.access = access
|
||||
@@ -36,22 +37,30 @@ class BookSolver:
|
||||
assert LiveCartaConfig.SUPPORTED_LEVELS == len(LiveCartaConfig.SUPPORTED_HEADERS), \
|
||||
"Length of headers doesn't match allowed levels."
|
||||
|
||||
def save_file(self, content: bytes, path_to_save, file_type):
|
||||
def save_file(self, content: bytes, path_to_save: str, file_type: str) -> str:
|
||||
"""
|
||||
Function saves binary content of file to folder(path_to_save)
|
||||
Parameters
|
||||
----------
|
||||
|
||||
content: bytes str
|
||||
binary content of the file
|
||||
path_to_save: str
|
||||
path to the folder
|
||||
file_type: str
|
||||
|
||||
Returns
|
||||
----------
|
||||
file_path: str
|
||||
path to file on local
|
||||
"""
|
||||
folder_path = os.path.dirname(
|
||||
folder_path: str = os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__)))
|
||||
folder_path = os.path.join(
|
||||
folder_path, path_to_save)
|
||||
pathlib.Path(folder_path).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
file_path = os.path.join(
|
||||
file_path: str = os.path.join(
|
||||
folder_path, f"{self.book_id}.{file_type}")
|
||||
try:
|
||||
with open(file_path, "wb+") as file:
|
||||
@@ -116,7 +125,7 @@ class BookSolver:
|
||||
parents=True, exist_ok=True)
|
||||
self.book_output_path.touch(exist_ok=True)
|
||||
|
||||
def write_to_json(self, content: dict):
|
||||
def write_to_json(self, content: Dict[str, List[Dict[str, Union[List, str]]]]):
|
||||
self.check_output_directory()
|
||||
try:
|
||||
with codecs.open(self.book_output_path, "w", encoding="utf-8") as f:
|
||||
@@ -127,7 +136,7 @@ class BookSolver:
|
||||
self.logger_object.log(
|
||||
"Error has occurred while writing .json file." + str(exc), logging.ERROR)
|
||||
|
||||
def send_json_content_to_server(self, content: dict):
|
||||
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)
|
||||
@@ -140,7 +149,7 @@ class BookSolver:
|
||||
raise exc
|
||||
|
||||
@abstractmethod
|
||||
def get_converted_book(self):
|
||||
def get_converted_book(self) -> Dict[str, List[Dict[str, Union[List, str]]]]:
|
||||
self.logger_object.log("Beginning of processing .json output.")
|
||||
self.status_wrapper.set_generating()
|
||||
return {}
|
||||
@@ -158,7 +167,7 @@ class BookSolver:
|
||||
self.logger_object.log(
|
||||
f"Beginning of conversion from .{self.book_type} to .json.")
|
||||
self.status_wrapper.set_processing()
|
||||
content_dict = self.get_converted_book()
|
||||
content_dict: Dict[str, List[Dict[Union[str, List]]]] = self.get_converted_book()
|
||||
[os.remove(path) for path in [self.preset_path, self.book_path]]
|
||||
self.logger_object.log("Beginning of processing .json output.")
|
||||
self.status_wrapper.set_generating()
|
||||
|
||||
Reference in New Issue
Block a user