add logs for every step

This commit is contained in:
Jeniamakarchik
2020-04-06 14:08:10 +03:00
parent fdcd7dcb65
commit c79486c934

View File

@@ -105,6 +105,7 @@ class Book:
try: try:
with open(file_path, 'wb+') as file: with open(file_path, 'wb+') as file:
file.write(content) file.write(content)
self.log(f'File was saved to folder: {folder_path}.')
except Exception as exc: except Exception as exc:
self.log("Error in writing docx file.", logging.ERROR) self.log("Error in writing docx file.", logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -118,6 +119,7 @@ class Book:
""" """
try: try:
content = self.access.get_doc(self.book_id) content = self.access.get_doc(self.book_id)
self.log('File was received from server.')
self.save_docx(content) self.save_docx(content)
except FileNotFoundError as ferr: except FileNotFoundError as ferr:
self.log("Can't get docx from server.", logging.ERROR) self.log("Can't get docx from server.", logging.ERROR)
@@ -130,6 +132,7 @@ class Book:
try: try:
if self.access: if self.access:
self.access.update_status(self.book_id, self.access.PROCESS) self.access.update_status(self.book_id, self.access.PROCESS)
self.log(f'Status has been updated to [PROCESS].')
except Exception as exc: except Exception as exc:
self.log("Can't update status of the book [PROCESS].", logging.ERROR) self.log("Can't update status of the book [PROCESS].", logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -139,6 +142,7 @@ class Book:
try: try:
if self.access: if self.access:
self.access.update_status(self.book_id, self.access.GENERATE) self.access.update_status(self.book_id, self.access.GENERATE)
self.log(f'Status has been updated to [GENERATE].')
except Exception as exc: except Exception as exc:
self.log("Can't update status of the book [GENERATE].", logging.ERROR) self.log("Can't update status of the book [GENERATE].", logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -148,6 +152,7 @@ class Book:
try: try:
if self.access: if self.access:
self.access.update_status(self.book_id, self.access.ERROR) self.access.update_status(self.book_id, self.access.ERROR)
self.log(f'Status has been updated to [ERROR].')
except Exception as exc: except Exception as exc:
self.log("Can't update status of the book [ERROR].", logging.ERROR) self.log("Can't update status of the book [ERROR].", logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -214,6 +219,7 @@ class Book:
""" """
try: try:
html_text = open(self.file_path, 'r', encoding='utf8').read() html_text = open(self.file_path, 'r', encoding='utf8').read()
self.log('HTML for book has been loaded.')
except FileNotFoundError as exc: except FileNotFoundError as exc:
self.log('There is no html to process. Conversion went wrong or you specified wrong paths.', logging.ERROR) self.log('There is no html to process. Conversion went wrong or you specified wrong paths.', logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -448,6 +454,7 @@ class Book:
if self.access is not None: if self.access is not None:
link = self.access.send_image(img_path, self.book_id) link = self.access.send_image(img_path, self.book_id)
img.attrs['src'] = link img.attrs['src'] = link
self.log(f'{img_name} successfully uploaded.')
else: else:
img_size = os.path.getsize(img_path) img_size = os.path.getsize(img_path)
print(f'{img_name} successfully loaded. Image size: {img_size}.') print(f'{img_name} successfully loaded. Image size: {img_size}.')
@@ -544,6 +551,7 @@ class Book:
self.clean_trash() self.clean_trash()
# process main elements of the .html doc # process main elements of the .html doc
self.log(f'Processing main elements of html.')
self._process_paragraph() self._process_paragraph()
self._process_two_columns() self._process_two_columns()
self._process_quotes() self._process_quotes()
@@ -560,6 +568,7 @@ class Book:
self.content = self.body_tag.find_all(recursive=False) self.content = self.body_tag.find_all(recursive=False)
self.log(f'Processing TOC and headers.')
self._process_toc_links() self._process_toc_links()
self._process_headings() self._process_headings()
@@ -646,6 +655,7 @@ class Book:
json_strc = [] json_strc = []
ind = 0 ind = 0
ch_num = 0 ch_num = 0
ch_amt = 0
try: try:
while ind < len(self.content): while ind < len(self.content):
@@ -665,6 +675,8 @@ class Book:
ch_num += 1 ch_num += 1
if res: if res:
json_strc.append(res) json_strc.append(res)
ch_amt += 1
self.log(f'Chapter {ch_amt} has been added to structure.')
except Exception as exc: except Exception as exc:
self.log('Error has occurred while making json structure.', logging.ERROR) self.log('Error has occurred while making json structure.', logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -680,6 +692,7 @@ class Book:
try: try:
with codecs.open(self.output_path, 'w', encoding='utf-8') as f: with codecs.open(self.output_path, 'w', encoding='utf-8') as f:
json.dump(self.content_dict, f, ensure_ascii=False) json.dump(self.content_dict, f, ensure_ascii=False)
self.log('Data has been saved to .json file.')
except Exception as exc: except Exception as exc:
self.log('Error has occurred while writing json file.', logging.ERROR) self.log('Error has occurred while writing json file.', logging.ERROR)
# self.log_error_to_main_log() # self.log_error_to_main_log()
@@ -689,6 +702,7 @@ class Book:
def send_json_content(self): def send_json_content(self):
try: try:
self.access.send_book(self.book_id, self.content_dict) self.access.send_book(self.book_id, self.content_dict)
self.log(f'JSON data has been sent to server.')
except Exception as exc: except Exception as exc:
self.log('Error has occurred while sending json content.', logging.ERROR) self.log('Error has occurred while sending json content.', logging.ERROR)
self.log_error_to_main_log() self.log_error_to_main_log()
@@ -702,6 +716,17 @@ class Book:
self.convert_to_json() self.convert_to_json()
self.write_json() self.write_json()
def test_conversion(self):
self.configure_file_logger(self.book_id, filemode='w+')
self.log('Beginning of the test.')
self.convert_doc_to_html()
self.check_output_directory()
self.read_html()
self.process_html()
self.convert_to_json()
self.write_json()
self.log('End of the test.')
def conversion(self, logging_format, filemode='w+'): def conversion(self, logging_format, filemode='w+'):
self.configure_file_logger(f'{__name__}_{self.book_id}', logging_format=logging_format, filemode=filemode) self.configure_file_logger(f'{__name__}_{self.book_id}', logging_format=logging_format, filemode=filemode)
self.log('Beginning of conversion from .docx to .json.') self.log('Beginning of conversion from .docx to .json.')