From 69f4560fdd1842241999890b5eb6a8cec2a79ef9 Mon Sep 17 00:00:00 2001 From: crocodilestick <105450872+crocodilestick@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:47:34 +0000 Subject: [PATCH] Fixed circular import introduced in last commit --- scripts/cover_enforcer.py | 24 ++++++++++++++++++++++-- scripts/cwa_db.py | 13 ++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/scripts/cover_enforcer.py b/scripts/cover_enforcer.py index 1985950..8ee0731 100644 --- a/scripts/cover_enforcer.py +++ b/scripts/cover_enforcer.py @@ -59,6 +59,20 @@ def get_new_metadata_path(self) -> str: temp_files = [os.path.join(dirpath,f) for (dirpath, dirnames, filenames) in os.walk(metadata_temp_dir) for f in filenames] return [f for f in temp_files if f.endswith('.opf')][0] + def export_as_dict(self) -> dict[str:str]: + return {"book_dir":self.book_dir, + "file_path":self.file_path, + "calibre_library":self.calibre_library, + "file_format":self.file_format, + "timestamp":self.timestamp, + "book_id":self.book_id, + "book_title":self.book_title, + "author_name":self.author_name, + "cover_path":self.cover_path, + "old_metadata_path":self.old_metadata_path, + "self.new_metadata_path":self.new_metadata_path, + "log_info":self.log_info} + class Enforcer: def __init__(self, args): @@ -167,7 +181,10 @@ def enforce_all_covers(self) -> tuple[int, float, int] | tuple[bool, bool, bool] for book_dir in book_dirs: try: book_objects = self.enforce_cover(book_dir) - self.db.enforce_add_entry_from_all(book_objects) + book_dicts = [] + for book in book_objects: + book_dicts.append(book.export_as_dict()) + self.db.enforce_add_entry_from_all(book_dicts) except Exception as e: print(f"[cover-metadata-enforcer]: ERROR: {book_dir}") print(f"[cover-metadata-enforcer]: Skipping book due to following error: {e}") @@ -267,7 +284,10 @@ def main(): args.dir = args.dir[:-1] if os.path.isdir(args.dir): book_objects = enforcer.enforce_cover(args.dir) - enforcer.db.enforce_add_entry_from_dir(book_objects) + book_dicts = [] + for book in book_objects: + book_dicts.append(book.export_as_dict()) + enforcer.db.enforce_add_entry_from_dir(book_dicts) else: print(f"[cover-metadata-enforcer]: ERROR: '{args.dir}' is not a valid directory") elif args.log is not None and args.dir is None and args.all is False and args.list is False and args.history is False: diff --git a/scripts/cwa_db.py b/scripts/cwa_db.py index bebde20..9abf25c 100644 --- a/scripts/cwa_db.py +++ b/scripts/cwa_db.py @@ -7,7 +7,6 @@ from tabulate import tabulate -from cover_enforcer import Book class CWA_DB: def __init__(self, verbose=False): @@ -240,17 +239,17 @@ def enforce_add_entry_from_log(self, log_info: dict): self.con.commit() - def enforce_add_entry_from_dir(self, book_objects: list[Book]): + def enforce_add_entry_from_dir(self, book_dicts: list[dict[str:str]]): """Adds an entry to the db when cover_enforcer is ran with a directory""" - for book in book_objects: - self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, file_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book.timestamp, book.book_id, book.book_title, book.author_name, book.file_path, 'manual -dir')) + for book in book_dicts: + self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, file_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book['timestamp'], book['book_id'], book['book_title'], book['author_name'], book['file_path'], 'manual -dir')) self.con.commit() - def enforce_add_entry_from_all(self, book_objects: list[Book]): + def enforce_add_entry_from_all(self, book_dicts: list[dict[str:str]]): """Adds an entry to the db when cover_enforcer is ran with the -all flag""" - for book in book_objects: - self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, file_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book.timestamp, book.book_id, book.book_title, book.author_name, book.file_path, 'manual -all')) + for book in book_dicts: + self.cur.execute("INSERT INTO cwa_enforcement(timestamp, book_id, book_title, author, file_path, trigger_type) VALUES (?, ?, ?, ?, ?, ?);", (book['timestamp'], book['book_id'], book['book_title'], book['author_name'], book['file_path'], 'manual -all')) self.con.commit()