Skip to content

Commit

Permalink
Fixed circular import introduced in last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
crocodilestick committed Nov 22, 2024
1 parent fc949d2 commit 69f4560
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
24 changes: 22 additions & 2 deletions scripts/cover_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions scripts/cwa_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from tabulate import tabulate

from cover_enforcer import Book

class CWA_DB:
def __init__(self, verbose=False):
Expand Down Expand Up @@ -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()


Expand Down

0 comments on commit 69f4560

Please sign in to comment.