From 14fa70e60814a6524422702ef81872e808ce95f9 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Tue, 25 Apr 2023 00:55:23 -0700 Subject: [PATCH] Separate xlate into separate functions based on return type fixes #454 --- comicapi/comet.py | 6 +-- comicapi/comicarchive.py | 14 +++---- comicapi/comicbookinfo.py | 24 +++++------ comicapi/comicinfoxml.py | 16 +++---- comicapi/genericmetadata.py | 2 +- comicapi/utils.py | 47 ++++++++++++--------- comictaggerlib/seriesselectionwindow.py | 4 +- comictaggerlib/taggerwindow.py | 20 ++++----- comictalker/talker_utils.py | 8 ++-- comictalker/talkers/comicvine.py | 4 +- tests/utils_test.py | 56 +++++++++++++++---------- 11 files changed, 112 insertions(+), 89 deletions(-) diff --git a/comicapi/comet.py b/comicapi/comet.py index ee7deb0..02e52d0 100644 --- a/comicapi/comet.py +++ b/comicapi/comet.py @@ -141,14 +141,14 @@ class CoMet: md.series = utils.xlate(get("series")) md.title = utils.xlate(get("title")) md.issue = utils.xlate(get("issue")) - md.volume = utils.xlate(get("volume"), True) + md.volume = utils.xlate_int(get("volume")) md.comments = utils.xlate(get("description")) md.publisher = utils.xlate(get("publisher")) md.language = utils.xlate(get("language")) md.format = utils.xlate(get("format")) - md.page_count = utils.xlate(get("pages"), True) + md.page_count = utils.xlate_int(get("pages")) md.maturity_rating = utils.xlate(get("rating")) - md.price = utils.xlate(get("price"), is_float=True) + md.price = utils.xlate_float(get("price")) md.is_version_of = utils.xlate(get("isVersionOf")) md.rights = utils.xlate(get("rights")) md.identifier = utils.xlate(get("identifier")) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index b225bba..ca089a2 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -561,13 +561,13 @@ class ComicArchive: ) metadata.alternate_number = utils.xlate(p.filename_info["alternate"]) metadata.issue = utils.xlate(p.filename_info["issue"]) - metadata.issue_count = utils.xlate(p.filename_info["issue_count"]) + metadata.issue_count = utils.xlate_int(p.filename_info["issue_count"]) metadata.publisher = utils.xlate(p.filename_info["publisher"]) metadata.series = utils.xlate(p.filename_info["series"]) metadata.title = utils.xlate(p.filename_info["title"]) - metadata.volume = utils.xlate(p.filename_info["volume"]) - metadata.volume_count = utils.xlate(p.filename_info["volume_count"]) - metadata.year = utils.xlate(p.filename_info["year"]) + metadata.volume = utils.xlate_int(p.filename_info["volume"]) + metadata.volume_count = utils.xlate_int(p.filename_info["volume_count"]) + metadata.year = utils.xlate_int(p.filename_info["year"]) metadata.scan_info = utils.xlate(p.filename_info["remainder"]) metadata.format = "FCBD" if p.filename_info["fcbd"] else None @@ -582,11 +582,11 @@ class ComicArchive: if fnp.series: metadata.series = fnp.series if fnp.volume: - metadata.volume = utils.xlate(fnp.volume, True) + metadata.volume = utils.xlate_int(fnp.volume) if fnp.year: - metadata.year = utils.xlate(fnp.year, True) + metadata.year = utils.xlate_int(fnp.year) if fnp.issue_count: - metadata.issue_count = utils.xlate(fnp.issue_count, True) + metadata.issue_count = utils.xlate_int(fnp.issue_count) if fnp.remainder: metadata.scan_info = fnp.remainder diff --git a/comicapi/comicbookinfo.py b/comicapi/comicbookinfo.py index ef282ac..cad1bb8 100644 --- a/comicapi/comicbookinfo.py +++ b/comicapi/comicbookinfo.py @@ -85,16 +85,16 @@ class ComicBookInfo: metadata.title = utils.xlate(cbi["title"]) metadata.issue = utils.xlate(cbi["issue"]) metadata.publisher = utils.xlate(cbi["publisher"]) - metadata.month = utils.xlate(cbi["publicationMonth"], True) - metadata.year = utils.xlate(cbi["publicationYear"], True) - metadata.issue_count = utils.xlate(cbi["numberOfIssues"], True) + metadata.month = utils.xlate_int(cbi["publicationMonth"]) + metadata.year = utils.xlate_int(cbi["publicationYear"]) + metadata.issue_count = utils.xlate_int(cbi["numberOfIssues"]) metadata.comments = utils.xlate(cbi["comments"]) metadata.genre = utils.xlate(cbi["genre"]) - metadata.volume = utils.xlate(cbi["volume"], True) - metadata.volume_count = utils.xlate(cbi["numberOfVolumes"], True) + metadata.volume = utils.xlate_int(cbi["volume"]) + metadata.volume_count = utils.xlate_int(cbi["numberOfVolumes"]) metadata.language = utils.xlate(cbi["language"]) metadata.country = utils.xlate(cbi["country"]) - metadata.critical_rating = utils.xlate(cbi["rating"], True) + metadata.critical_rating = utils.xlate_int(cbi["rating"]) metadata.credits = [ Credits( @@ -152,16 +152,16 @@ class ComicBookInfo: assign("title", utils.xlate(metadata.title)) assign("issue", utils.xlate(metadata.issue)) assign("publisher", utils.xlate(metadata.publisher)) - assign("publicationMonth", utils.xlate(metadata.month, True)) - assign("publicationYear", utils.xlate(metadata.year, True)) - assign("numberOfIssues", utils.xlate(metadata.issue_count, True)) + assign("publicationMonth", utils.xlate_int(metadata.month)) + assign("publicationYear", utils.xlate_int(metadata.year)) + assign("numberOfIssues", utils.xlate_int(metadata.issue_count)) assign("comments", utils.xlate(metadata.comments)) assign("genre", utils.xlate(metadata.genre)) - assign("volume", utils.xlate(metadata.volume, True)) - assign("numberOfVolumes", utils.xlate(metadata.volume_count, True)) + assign("volume", utils.xlate_int(metadata.volume)) + assign("numberOfVolumes", utils.xlate_int(metadata.volume_count)) assign("language", utils.xlate(utils.get_language_from_iso(metadata.language))) assign("country", utils.xlate(metadata.country)) - assign("rating", utils.xlate(metadata.critical_rating, True)) + assign("rating", utils.xlate_int(metadata.critical_rating)) assign("credits", metadata.credits) assign("tags", list(metadata.tags)) diff --git a/comicapi/comicinfoxml.py b/comicapi/comicinfoxml.py index aa50ee0..124b6dc 100644 --- a/comicapi/comicinfoxml.py +++ b/comicapi/comicinfoxml.py @@ -190,16 +190,16 @@ class ComicInfoXml: md.series = utils.xlate(get("Series")) md.title = utils.xlate(get("Title")) md.issue = IssueString(utils.xlate(get("Number"))).as_string() - md.issue_count = utils.xlate(get("Count"), True) - md.volume = utils.xlate(get("Volume"), True) + md.issue_count = utils.xlate_int(get("Count")) + md.volume = utils.xlate_int(get("Volume")) md.alternate_series = utils.xlate(get("AlternateSeries")) md.alternate_number = IssueString(utils.xlate(get("AlternateNumber"))).as_string() - md.alternate_count = utils.xlate(get("AlternateCount"), True) + md.alternate_count = utils.xlate_int(get("AlternateCount")) md.comments = utils.xlate(get("Summary")) md.notes = utils.xlate(get("Notes")) - md.year = utils.xlate(get("Year"), True) - md.month = utils.xlate(get("Month"), True) - md.day = utils.xlate(get("Day"), True) + md.year = utils.xlate_int(get("Year")) + md.month = utils.xlate_int(get("Month")) + md.day = utils.xlate_int(get("Day")) md.publisher = utils.xlate(get("Publisher")) md.imprint = utils.xlate(get("Imprint")) md.genre = utils.xlate(get("Genre")) @@ -210,12 +210,12 @@ class ComicInfoXml: md.characters = utils.xlate(get("Characters")) md.teams = utils.xlate(get("Teams")) md.locations = utils.xlate(get("Locations")) - md.page_count = utils.xlate(get("PageCount"), True) + md.page_count = utils.xlate_int(get("PageCount")) md.scan_info = utils.xlate(get("ScanInformation")) md.story_arc = utils.xlate(get("StoryArc")) md.series_group = utils.xlate(get("SeriesGroup")) md.maturity_rating = utils.xlate(get("AgeRating")) - md.critical_rating = utils.xlate(get("CommunityRating"), is_float=True) + md.critical_rating = utils.xlate_float(get("CommunityRating")) tmp = utils.xlate(get("BlackAndWhite")) if tmp is not None and tmp.casefold() in ["yes", "true", "1"]: diff --git a/comicapi/genericmetadata.py b/comicapi/genericmetadata.py index 243eee9..e5a4e4b 100644 --- a/comicapi/genericmetadata.py +++ b/comicapi/genericmetadata.py @@ -122,7 +122,7 @@ class GenericMetadata: pages: list[ImageMetadata] = dataclasses.field(default_factory=list) # Some CoMet-only items - price: str | None = None + price: float | None = None is_version_of: str | None = None rights: str | None = None identifier: str | None = None diff --git a/comicapi/utils.py b/comicapi/utils.py index 699e6d4..9cb579c 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -68,17 +68,17 @@ def combine_notes(existing_notes: str | None, new_notes: str | None, split: str) return (untouched_notes + "\n" + (new_notes or "")).strip() -def parse_date_str(date_str: str) -> tuple[int | None, int | None, int | None]: +def parse_date_str(date_str: str | None) -> tuple[int | None, int | None, int | None]: day = None month = None year = None if date_str: parts = date_str.split("-") - year = xlate(parts[0], True) + year = xlate_int(parts[0]) if len(parts) > 1: - month = xlate(parts[1], True) + month = xlate_int(parts[1]) if len(parts) > 2: - day = xlate(parts[2], True) + day = xlate_int(parts[2]) return day, month, year @@ -107,23 +107,32 @@ def add_to_path(dirname: str) -> None: os.environ["PATH"] = os.pathsep.join(paths) -def xlate(data: Any, is_int: bool = False, is_float: bool = False) -> Any: +def xlate_int(data: Any) -> int | None: + data = xlate_float(data) + if data is None: + return None + return int(data) + + +def xlate_float(data: Any) -> float | None: + if data is None or data == "": + return None + i: str | int | float + if isinstance(data, (int, float)): + i = data + else: + i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890."))) + if i == "": + return None + try: + return float(i) + except ValueError: + return None + + +def xlate(data: Any) -> str | None: if data is None or data == "": return None - if is_int or is_float: - i: str | int | float - if isinstance(data, (int, float)): - i = data - else: - i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890."))) - if i == "": - return None - try: - if is_float: - return float(i) - return int(float(i)) - except ValueError: - return None return str(data) diff --git a/comictaggerlib/seriesselectionwindow.py b/comictaggerlib/seriesselectionwindow.py index 56c8706..2da9f59 100644 --- a/comictaggerlib/seriesselectionwindow.py +++ b/comictaggerlib/seriesselectionwindow.py @@ -103,7 +103,7 @@ class SeriesSelectionWindow(QtWidgets.QDialog): series_name: str, issue_number: str, year: int | None, - issue_count: int, + issue_count: int | None, cover_index_list: list[int], comic_archive: ComicArchive | None, config: settngs.Namespace, @@ -303,7 +303,7 @@ class SeriesSelectionWindow(QtWidgets.QDialog): if found_match is not None: self.iddialog.accept() - self.series_id = utils.xlate(found_match["series_id"]) + self.series_id = utils.xlate(found_match["series_id"]) or "" self.issue_number = found_match["issue_number"] self.select_by_id() self.show_issues() diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index 2b5acdc..0363392 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -898,13 +898,13 @@ class TaggerWindow(QtWidgets.QMainWindow): md.is_empty = False md.alternate_number = IssueString(self.leAltIssueNum.text()).as_string() md.issue = IssueString(self.leIssueNum.text()).as_string() - md.issue_count = utils.xlate(self.leIssueCount.text(), True) - md.volume = utils.xlate(self.leVolumeNum.text(), True) - md.volume_count = utils.xlate(self.leVolumeCount.text(), True) - md.month = utils.xlate(self.lePubMonth.text(), True) - md.year = utils.xlate(self.lePubYear.text(), True) - md.day = utils.xlate(self.lePubDay.text(), True) - md.alternate_count = utils.xlate(self.leAltIssueCount.text(), True) + md.issue_count = utils.xlate_int(self.leIssueCount.text()) + md.volume = utils.xlate_int(self.leVolumeNum.text()) + md.volume_count = utils.xlate_int(self.leVolumeCount.text()) + md.month = utils.xlate_int(self.lePubMonth.text()) + md.year = utils.xlate_int(self.lePubYear.text()) + md.day = utils.xlate_int(self.lePubDay.text()) + md.alternate_count = utils.xlate_int(self.leAltIssueCount.text()) md.series = self.leSeries.text() md.title = self.leTitle.text() @@ -915,7 +915,7 @@ class TaggerWindow(QtWidgets.QMainWindow): md.notes = self.teNotes.toPlainText() md.maturity_rating = self.cbMaturityRating.currentText() - md.critical_rating = utils.xlate(self.dsbCriticalRating.cleanText(), is_float=True) + md.critical_rating = utils.xlate_float(self.dsbCriticalRating.cleanText()) if md.critical_rating == 0.0: md.critical_rating = None @@ -1027,9 +1027,9 @@ class TaggerWindow(QtWidgets.QMainWindow): QtWidgets.QMessageBox.information(self, "Online Search", "Need to enter a series name to search.") return - year = utils.xlate(self.lePubYear.text(), True) + year = utils.xlate_int(self.lePubYear.text()) - issue_count = utils.xlate(self.leIssueCount.text(), True) + issue_count = utils.xlate_int(self.leIssueCount.text()) cover_index_list = self.metadata.get_cover_page_index_list() selector = SeriesSelectionWindow( diff --git a/comictalker/talker_utils.py b/comictalker/talker_utils.py index c397d79..a965bf2 100644 --- a/comictalker/talker_utils.py +++ b/comictalker/talker_utils.py @@ -56,7 +56,7 @@ def map_comic_issue_to_metadata( if issue_results.cover_date: metadata.day, metadata.month, metadata.year = utils.parse_date_str(issue_results.cover_date) elif issue_results.series.start_year: - metadata.year = utils.xlate(issue_results.series.start_year, True) + metadata.year = utils.xlate_int(issue_results.series.start_year) metadata.comments = cleanup_html(issue_results.description, remove_html_tables) if use_year_volume: @@ -91,11 +91,11 @@ def parse_date_str(date_str: str) -> tuple[int | None, int | None, int | None]: year = None if date_str: parts = date_str.split("-") - year = utils.xlate(parts[0], True) + year = utils.xlate_int(parts[0]) if len(parts) > 1: - month = utils.xlate(parts[1], True) + month = utils.xlate_int(parts[1]) if len(parts) > 2: - day = utils.xlate(parts[2], True) + day = utils.xlate_int(parts[2]) return day, month, year diff --git a/comictalker/talkers/comicvine.py b/comictalker/talkers/comicvine.py index 3e43e05..6338758 100644 --- a/comictalker/talkers/comicvine.py +++ b/comictalker/talkers/comicvine.py @@ -393,7 +393,7 @@ class ComicVineTalker(ComicTalker): series_filter += str(vid) + "|" flt = f"volume:{series_filter},issue_number:{issue_number}" # CV uses volume to mean series - int_year = utils.xlate(year, True) + int_year = utils.xlate_int(year) if int_year is not None: flt += f",cover_date:{int_year}-1-1|{int_year + 1}-1-1" @@ -501,7 +501,7 @@ class ComicVineTalker(ComicTalker): else: image_url = record["image"].get("super_url", "") - start_year = utils.xlate(record.get("start_year", ""), True) + start_year = utils.xlate_int(record.get("start_year", "")) aliases = record.get("aliases") or "" diff --git a/tests/utils_test.py b/tests/utils_test.py index a9bcda4..43ebc94 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -77,32 +77,46 @@ def test_recursive_list_with_file(tmp_path) -> None: xlate_values = [ - ({"data": "", "is_int": False, "is_float": False}, None), - ({"data": None, "is_int": False, "is_float": False}, None), - ({"data": None, "is_int": True, "is_float": False}, None), - ({"data": " ", "is_int": True, "is_float": False}, None), - ({"data": "", "is_int": True, "is_float": False}, None), - ({"data": "9..", "is_int": True, "is_float": False}, None), - ({"data": "9", "is_int": False, "is_float": False}, "9"), - ({"data": 9, "is_int": False, "is_float": False}, "9"), - ({"data": 9, "is_int": True, "is_float": False}, 9), - ({"data": "9", "is_int": True, "is_float": False}, 9), - ({"data": 9.3, "is_int": True, "is_float": False}, 9), - ({"data": "9.3", "is_int": True, "is_float": False}, 9), - ({"data": "9.", "is_int": True, "is_float": False}, 9), - ({"data": " 9 . 3 l", "is_int": True, "is_float": False}, 9), - ({"data": 9, "is_int": False, "is_float": True}, 9.0), - ({"data": "9", "is_int": False, "is_float": True}, 9.0), - ({"data": 9.3, "is_int": False, "is_float": True}, 9.3), - ({"data": "9.3", "is_int": False, "is_float": True}, 9.3), - ({"data": "9.", "is_int": False, "is_float": True}, 9.0), - ({"data": " 9 . 3 l", "is_int": False, "is_float": True}, 9.3), + ("", None), + (None, None), + ("9", "9"), + (9, "9"), +] +xlate_int_values = [ + (None, None), + (" ", None), + ("", None), + ("9..", None), + (9, 9), + ("9", 9), + (9.3, 9), + ("9.3", 9), + ("9.", 9), + (" 9 . 3 l", 9), +] +xlate_float_values = [ + (9, 9.0), + ("9", 9.0), + (9.3, 9.3), + ("9.3", 9.3), + ("9.", 9.0), + (" 9 . 3 l", 9.3), ] @pytest.mark.parametrize("value, result", xlate_values) def test_xlate(value, result): - assert comicapi.utils.xlate(**value) == result + assert comicapi.utils.xlate(value) == result + + +@pytest.mark.parametrize("value, result", xlate_float_values) +def test_xlate_float(value, result): + assert comicapi.utils.xlate_float(value) == result + + +@pytest.mark.parametrize("value, result", xlate_int_values) +def test_xlate_int(value, result): + assert comicapi.utils.xlate_int(value) == result language_values = [