From 94be266e17ae5ff723fa9b10935555e0c694dad0 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 27 Jul 2022 23:24:34 -0700 Subject: [PATCH] Handle the 'primary' key missing in get_primary_credit Fixes #342 Add better exception handling for the formatter --- comicapi/comicbookinfo.py | 9 +++++++- comicapi/genericmetadata.py | 4 +++- comictaggerlib/cli.py | 8 +++++-- comictaggerlib/renamewindow.py | 16 +++++++++++++- comictaggerlib/settingswindow.py | 37 ++++++++++++++++++++++---------- testing/comicdata.py | 11 ++++++++-- tests/genericmetadata_test.py | 2 +- 7 files changed, 68 insertions(+), 19 deletions(-) diff --git a/comicapi/comicbookinfo.py b/comicapi/comicbookinfo.py index a99bd45..dcb929d 100644 --- a/comicapi/comicbookinfo.py +++ b/comicapi/comicbookinfo.py @@ -97,7 +97,14 @@ class ComicBookInfo: metadata.country = utils.xlate(cbi["country"]) metadata.critical_rating = utils.xlate(cbi["rating"], True) - metadata.credits = cbi["credits"] + metadata.credits = [ + Credits( + person=x["person"] if "person" in x else "", + role=x["role"] if "role" in x else "", + primary=x["primary"] if "primary" in x else False, + ) + for x in cbi["credits"] + ] metadata.tags = set(cbi["tags"]) if cbi["tags"] is not None else set() # make sure credits and tags are at least empty lists and not None diff --git a/comicapi/genericmetadata.py b/comicapi/genericmetadata.py index ccbc1c1..7d0098a 100644 --- a/comicapi/genericmetadata.py +++ b/comicapi/genericmetadata.py @@ -270,8 +270,10 @@ class GenericMetadata: def get_primary_credit(self, role: str) -> str: primary = "" for credit in self.credits: + if "role" not in credit or "person" not in credit: + continue if (primary == "" and credit["role"].casefold() == role.casefold()) or ( - credit["role"].casefold() == role.casefold() and credit["primary"] + credit["role"].casefold() == role.casefold() and "primary" in credit and credit["primary"] ): primary = credit["person"] return primary diff --git a/comictaggerlib/cli.py b/comictaggerlib/cli.py index f95cf45..481d36f 100644 --- a/comictaggerlib/cli.py +++ b/comictaggerlib/cli.py @@ -500,15 +500,19 @@ def process_file_cli( try: new_name = renamer.determine_name(ext=new_ext) - except Exception: + except ValueError: logger.exception( msg_hdr + "Invalid format string!\n" "Your rename template is invalid!\n\n" + "%s\n\n" "Please consult the template help in the settings " "and the documentation on the format at " - "https://docs.python.org/3/library/string.html#format-string-syntax" + "https://docs.python.org/3/library/string.html#format-string-syntax", + settings.rename_template, ) return + except Exception: + logger.exception("Formatter failure: %s metadata: %s", settings.rename_template, renamer.metadata) folder = get_rename_dir(ca, settings.rename_dir if settings.rename_move_dir else None) diff --git a/comictaggerlib/renamewindow.py b/comictaggerlib/renamewindow.py index 808c74c..2fee991 100644 --- a/comictaggerlib/renamewindow.py +++ b/comictaggerlib/renamewindow.py @@ -97,7 +97,8 @@ class RenameWindow(QtWidgets.QDialog): new_ext = self.config_renamer(ca) try: new_name = self.renamer.determine_name(new_ext) - except Exception as e: + except ValueError as e: + logger.exception("Invalid format string: %s", self.settings.rename_template) QtWidgets.QMessageBox.critical( self, "Invalid format string!", @@ -109,6 +110,19 @@ class RenameWindow(QtWidgets.QDialog): "https://docs.python.org/3/library/string.html#format-string-syntax", ) return + except Exception as e: + logger.exception( + "Formatter failure: %s metadata: %s", self.settings.rename_template, self.renamer.metadata + ) + QtWidgets.QMessageBox.critical( + self, + "The formatter had an issue!", + "The formatter has experienced an unexpected error!" + f"

{type(e).__name__}: {e}

" + "Please open an issue at " + "" + "https://github.com/comictagger/comictagger", + ) row = self.twList.rowCount() self.twList.insertRow(row) diff --git a/comictaggerlib/settingswindow.py b/comictaggerlib/settingswindow.py index 7d5f89f..4fbdd68 100644 --- a/comictaggerlib/settingswindow.py +++ b/comictaggerlib/settingswindow.py @@ -269,17 +269,32 @@ class SettingsWindow(QtWidgets.QDialog): def accept(self) -> None: self.rename_test() if self.rename_error is not None: - QtWidgets.QMessageBox.critical( - self, - "Invalid format string!", - "Your rename template is invalid!" - f"

{self.rename_error}

" - "Please consult the template help in the " - "settings and the documentation on the format at " - "" - "https://docs.python.org/3/library/string.html#format-string-syntax", - ) - return + if isinstance(self.rename_error, ValueError): + logger.exception("Invalid format string: %s", self.settings.rename_template) + QtWidgets.QMessageBox.critical( + self, + "Invalid format string!", + "Your rename template is invalid!" + f"

{self.rename_error}

" + "Please consult the template help in the " + "settings and the documentation on the format at " + "" + "https://docs.python.org/3/library/string.html#format-string-syntax", + ) + return + else: + logger.exception( + "Formatter failure: %s metadata: %s", self.settings.rename_template, self.renamer.metadata + ) + QtWidgets.QMessageBox.critical( + self, + "The formatter had an issue!", + "The formatter has experienced an unexpected error!" + f"

{type(self.rename_error).__name__}: {self.rename_error}

" + "Please open an issue at " + "" + "https://github.com/comictagger/comictagger", + ) # Copy values from form to settings and save self.settings.rar_exe_path = str(self.leRarExePath.text()) diff --git a/testing/comicdata.py b/testing/comicdata.py index 9be6477..790b688 100644 --- a/testing/comicdata.py +++ b/testing/comicdata.py @@ -99,8 +99,15 @@ metadata_keys = [ ] credits = [ - ("writer", "Dara Naraghi"), - ("writeR", "Dara Naraghi"), + (comicapi.genericmetadata.md_test, "writer", "Dara Naraghi"), + (comicapi.genericmetadata.md_test, "writeR", "Dara Naraghi"), + ( + comicapi.genericmetadata.md_test.replace( + credits=[{"person": "Dara Naraghi", "role": "writer"}, {"person": "Dara Naraghi", "role": "writer"}] + ), + "writeR", + "Dara Naraghi", + ), ] imprints = [ diff --git a/tests/genericmetadata_test.py b/tests/genericmetadata_test.py index c10574b..5a79e18 100644 --- a/tests/genericmetadata_test.py +++ b/tests/genericmetadata_test.py @@ -37,6 +37,6 @@ def test_add_credit_primary(): assert md.credits == [comicapi.genericmetadata.CreditMetadata(person="test", role="writer", primary=True)] -@pytest.mark.parametrize("role, expected", credits) +@pytest.mark.parametrize("md, role, expected", credits) def test_get_primary_credit(md, role, expected): assert md.get_primary_credit(role) == expected