From 943b1f0754050ced69ab3951234a5ca874de3f57 Mon Sep 17 00:00:00 2001 From: Kiryl Date: Tue, 5 Oct 2021 14:01:05 +0300 Subject: [PATCH] rewrite imports --- consumer.py | 54 ++++++++----------- src/docx_converter/docx_solver.py | 7 ++- src/docx_converter/html_docx_preprocessor.py | 6 +-- .../libra_html2json_converter.py | 2 +- src/epub_converter/css_reader.py | 8 +-- src/epub_converter/epub_converter.py | 19 +++---- src/epub_converter/epub_solver.py | 5 +- src/epub_converter/html_epub_preprocessor.py | 2 +- src/util/check_dirs.py | 3 +- src/util/check_packs.py | 5 +- src/util/color_reader.py | 3 +- src/util/helpers.py | 3 +- 12 files changed, 53 insertions(+), 64 deletions(-) diff --git a/consumer.py b/consumer.py index 7cc53c0..006aaae 100644 --- a/consumer.py +++ b/consumer.py @@ -1,19 +1,17 @@ -import json -import logging import os import sys -from functools import partial -from pathlib import Path -from threading import Thread, active_count -from threading import Event - +import json import pika +import logging +from pathlib import Path +from threading import Event +from functools import partial +from threading import Thread, active_count from src.access import Access from src.docx_converter.docx_solver import DocxBook from src.epub_converter.epub_solver import EpubBook - def configure_file_logger(name, filename='logs/converter_log.log', filemode='w+', logging_level=logging.INFO, logging_format='%(asctime)s - %(levelname)s - %(message)s'): @@ -27,9 +25,24 @@ def configure_file_logger(name, filename='logs/converter_log.log', filemode='w+' file_format = logging.Formatter(fmt=logging_format) file_handler.setFormatter(file_format) logger.setLevel(logging_level) - return logger +def local_convert_book(filename, locker): + logger = configure_file_logger('consumer') + logger.info(f'Start processing book-{filename}.') + + folder_path = os.path.dirname(os.path.abspath("__file__")) + output_path = Path(os.path.join(folder_path, f'json/{filename}.json')) + try: + book = DocxBook(book_id=filename, + main_logger=logger, + libra_locker=locker) + book.test_conversion() + + except Exception as exc: + raise exc + + logger.info(f'Book-{filename} has been proceeded.') def convert_book(book_type: [DocxBook, EpubBook], params: dict, logger, book_id): logger.info(f'Start processing book-{book_id}.') @@ -79,25 +92,6 @@ def callback(ch, method, properties, body, logger, libra_locker): finally: pass - -def local_convert_book(filename, locker): - logger = configure_file_logger('consumer', logging_format='%(asctime)s - %(levelname)s - %(message)s') - logger.info(f'Start processing book-{filename}.') - - folder_path = os.path.dirname(os.path.abspath("__file__")) - output_path = Path(os.path.join(folder_path, f'json/{filename}.json')) - try: - book = DocxBook(book_id=filename, - main_logger=logger, - libra_locker=locker) - book.test_conversion() - - except Exception as exc: - raise exc - - logger.info(f'Book-{filename} has been proceeded.') - - def local_run(books): locker = Event() locker.set() @@ -110,7 +104,6 @@ def local_run(books): [x.start() for x in threads] - def server_run(): logger = configure_file_logger('consumer') @@ -150,5 +143,4 @@ def server_run(): if __name__ == '__main__': - server_run() - + server_run() \ No newline at end of file diff --git a/src/docx_converter/docx_solver.py b/src/docx_converter/docx_solver.py index d83417a..f166caa 100644 --- a/src/docx_converter/docx_solver.py +++ b/src/docx_converter/docx_solver.py @@ -1,14 +1,13 @@ -import logging import os +import logging import pathlib import subprocess from subprocess import PIPE from threading import Event - from bs4 import BeautifulSoup + from src.docx_converter.html_docx_preprocessor import HTMLDocxPreprocessor from src.docx_converter.libra_html2json_converter import LibraHTML2JSONConverter - from src.book_solver import BookSolver @@ -155,4 +154,4 @@ if __name__ == "__main__": out_path = pathlib.Path(os.path.join(folder, 'json/ch13.json')) book = DocxBook(html_path=file) - book.convert_from_html() + book.convert_from_html() \ No newline at end of file diff --git a/src/docx_converter/html_docx_preprocessor.py b/src/docx_converter/html_docx_preprocessor.py index 904a30d..59f677e 100644 --- a/src/docx_converter/html_docx_preprocessor.py +++ b/src/docx_converter/html_docx_preprocessor.py @@ -1,9 +1,9 @@ -import logging import os -import pathlib import re -from shutil import copyfile +import logging +import pathlib from typing import List +from shutil import copyfile from bs4 import BeautifulSoup, NavigableString, Tag diff --git a/src/docx_converter/libra_html2json_converter.py b/src/docx_converter/libra_html2json_converter.py index 91747eb..cafbb39 100644 --- a/src/docx_converter/libra_html2json_converter.py +++ b/src/docx_converter/libra_html2json_converter.py @@ -1,5 +1,5 @@ -import logging import re +import logging from copy import copy from src.livecarta_config import LiveCartaConfig diff --git a/src/epub_converter/css_reader.py b/src/epub_converter/css_reader.py index 0a8f1f3..9a5870b 100644 --- a/src/epub_converter/css_reader.py +++ b/src/epub_converter/css_reader.py @@ -1,17 +1,17 @@ import re +import cssutils from typing import List -import cssutils - -from bs4 import BeautifulSoup from ebooklib import epub +from logging import CRITICAL +from bs4 import BeautifulSoup from premailer import transform from itertools import takewhile -from logging import CRITICAL from src.livecarta_config import LiveCartaConfig from src.util.color_reader import str2hex + cssutils.log.setLevel(CRITICAL) sizes_pr = [-1, 0.5, 0.56, 0.63, 0.69, 0.75, 0.81, 0.88, 0.94, 1.0, 1.06, 1.13, 1.19, 1.25, 1.31, 1.38, 1.44, 1.5, 1.56, diff --git a/src/epub_converter/epub_converter.py b/src/epub_converter/epub_converter.py index 61c3397..c1e8827 100644 --- a/src/epub_converter/epub_converter.py +++ b/src/epub_converter/epub_converter.py @@ -1,24 +1,25 @@ -import codecs -import json -import logging import os import re -from os.path import dirname, normpath, join +import json +import codecs +import logging +from itertools import chain from collections import defaultdict from typing import Dict, Union, List -from itertools import chain +from os.path import dirname, normpath, join import ebooklib -from bs4 import BeautifulSoup, Tag from ebooklib import epub +from bs4 import BeautifulSoup, Tag from ebooklib.epub import Link, Section +from src.util.helpers import BookLogger +from src.livecarta_config import LiveCartaConfig from src.data_objects import ChapterItem, NavPoint +from src.epub_converter.css_reader import build_css_content, convert_html_soup_with_css_style from src.epub_converter.html_epub_preprocessor import unwrap_structural_tags, get_tags_between_chapter_marks, prepare_title_and_content, \ update_src_links_in_images, preprocess_footnotes -from src.epub_converter.css_reader import build_css_content, convert_html_soup_with_css_style -from src.livecarta_config import LiveCartaConfig -from src.util.helpers import BookLogger + class EpubConverter: diff --git a/src/epub_converter/epub_solver.py b/src/epub_converter/epub_solver.py index 583f3fd..4aaddb4 100644 --- a/src/epub_converter/epub_solver.py +++ b/src/epub_converter/epub_solver.py @@ -1,5 +1,5 @@ -from src.epub_converter.epub_converter import EpubConverter from src.book_solver import BookSolver +from src.epub_converter.epub_converter import EpubConverter class EpubBook(BookSolver): @@ -12,5 +12,4 @@ class EpubBook(BookSolver): json_converter = EpubConverter(self.file_path, access=self.access, logger=self.logger_object) content_dict = json_converter.convert_to_dict() self.status_wrapper.set_generating() - return content_dict - + return content_dict \ No newline at end of file diff --git a/src/epub_converter/html_epub_preprocessor.py b/src/epub_converter/html_epub_preprocessor.py index 1791726..b3d46b6 100644 --- a/src/epub_converter/html_epub_preprocessor.py +++ b/src/epub_converter/html_epub_preprocessor.py @@ -1,6 +1,6 @@ import os -import pathlib import re +import pathlib from typing import Tuple from bs4 import BeautifulSoup, NavigableString, Tag, Comment diff --git a/src/util/check_dirs.py b/src/util/check_dirs.py index b11b6f1..1348865 100644 --- a/src/util/check_dirs.py +++ b/src/util/check_dirs.py @@ -1,6 +1,5 @@ -import argparse import os - +import argparse def parse_args(): parser = argparse.ArgumentParser(description="Utility for folders's clean up.") diff --git a/src/util/check_packs.py b/src/util/check_packs.py index bbea11d..a0f1f6c 100644 --- a/src/util/check_packs.py +++ b/src/util/check_packs.py @@ -1,8 +1,7 @@ -import argparse import os -import subprocess import sys - +import argparse +import subprocess def parse_args(): parser = argparse.ArgumentParser(description="Utility for checking installed packages.") diff --git a/src/util/color_reader.py b/src/util/color_reader.py index 4ae5941..8da83a7 100644 --- a/src/util/color_reader.py +++ b/src/util/color_reader.py @@ -1,7 +1,8 @@ import re -from webcolors import html4_hex_to_names, hex_to_rgb, rgb_to_name, rgb_percent_to_hex, rgb_to_hex, css3_names_to_hex from colorsys import hls_to_rgb +from webcolors import html4_hex_to_names, hex_to_rgb, rgb_to_name, rgb_percent_to_hex, rgb_to_hex, css3_names_to_hex + def closest_colour_rgb(requested_color): diff --git a/src/util/helpers.py b/src/util/helpers.py index 9516bf2..8ca5bad 100644 --- a/src/util/helpers.py +++ b/src/util/helpers.py @@ -1,5 +1,5 @@ -import logging import os +import logging class BookLogger: @@ -8,7 +8,6 @@ class BookLogger: 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.