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