From 4b6d35fd3ac4ec6fc4e47079b02baa5b36f8e0be Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 2 Oct 2022 19:33:12 -0700 Subject: [PATCH] Fix CBL tagging --- comicapi/comicarchive.py | 9 ++++--- comicapi/comicbookinfo.py | 4 +-- comicapi/utils.py | 15 +++++------- tests/comicarchive_test.py | 50 ++++++++++++++++++++++++++++++++++++++ tests/metadata_test.py | 1 - tests/utils_test.py | 13 +++++----- 6 files changed, 70 insertions(+), 22 deletions(-) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 0d850a4..a5dfc49 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -379,14 +379,15 @@ class RarArchiver(UnknownArchiver): def get_comment(self) -> str: rarc = self.get_rar_obj() - return str(rarc.comment) if rarc else "" + return rarc.comment.decode("utf-8") if rarc else "" def set_comment(self, comment: str) -> bool: if rar_support and self.rar_exe_path: try: # write comment to temp file - with tempfile.NamedTemporaryFile() as tmp_file: - tmp_file.write(comment.encode("utf-8")) + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir: + tmp_file = pathlib.Path(tmp_dir) / "rar_comment.txt" + tmp_file.write_text(comment, encoding="utf-8") working_dir = os.path.dirname(os.path.abspath(self.path)) @@ -396,7 +397,7 @@ class RarArchiver(UnknownArchiver): "c", f"-w{working_dir}", "-c-", - f"-z{tmp_file.name}", + f"-z{tmp_file}", str(self.path), ] subprocess.run( diff --git a/comicapi/comicbookinfo.py b/comicapi/comicbookinfo.py index dcb929d..4a7b1f2 100644 --- a/comicapi/comicbookinfo.py +++ b/comicapi/comicbookinfo.py @@ -112,8 +112,8 @@ class ComicBookInfo: metadata.credits = [] # need the language string to be ISO - if metadata.language is not None: - metadata.language = utils.get_language(metadata.language) + if metadata.language: + metadata.language = utils.get_language_iso(metadata.language) metadata.is_empty = False diff --git a/comicapi/utils.py b/comicapi/utils.py index f2cccfe..f308433 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -193,18 +193,15 @@ def get_language_from_iso(iso: str | None) -> str | None: return languages[iso] -def get_language(string: str | None) -> str | None: +def get_language_iso(string: str | None) -> str | None: if string is None: return None - string = string.casefold() + lang = string.casefold() - lang = get_language_from_iso(string) - - if lang is None: - try: - return str(pycountry.languages.lookup(string).name) - except LookupError: - return None + try: + return getattr(pycountry.languages.lookup(string), "alpha_2", None) + except LookupError: + pass return lang diff --git a/tests/comicarchive_test.py b/tests/comicarchive_test.py index 69c4793..7d9499e 100644 --- a/tests/comicarchive_test.py +++ b/tests/comicarchive_test.py @@ -47,6 +47,56 @@ def test_save_cix(tmp_comic): md = tmp_comic.read_cix() +def test_save_cbi(tmp_comic): + md = tmp_comic.read_cix() + md.set_default_page_list(tmp_comic.get_number_of_pages()) + + assert tmp_comic.write_cbi(md) + + md = tmp_comic.read_cbi() + + +def test_save_cix_rar(tmp_path): + cbr_path = datadir / "fake_cbr.cbr" + shutil.copy(cbr_path, tmp_path) + + tmp_comic = comicapi.comicarchive.ComicArchive(tmp_path / cbr_path.name) + assert tmp_comic.write_cix(comicapi.genericmetadata.md_test) + + md = tmp_comic.read_cix() + assert md.replace(pages=[]) == comicapi.genericmetadata.md_test.replace(pages=[]) + + +def test_save_cbi_rar(tmp_path): + cbr_path = datadir / "fake_cbr.cbr" + shutil.copy(cbr_path, tmp_path) + + tmp_comic = comicapi.comicarchive.ComicArchive(tmp_path / cbr_path.name) + assert tmp_comic.write_cbi(comicapi.genericmetadata.md_test) + + md = tmp_comic.read_cbi() + assert md.replace(pages=[]) == comicapi.genericmetadata.md_test.replace( + pages=[], + day=None, + alternate_series=None, + alternate_number=None, + alternate_count=None, + imprint=None, + notes=None, + web_link=None, + format=None, + manga=None, + page_count=None, + maturity_rating=None, + story_arc=None, + series_group=None, + scan_info=None, + characters=None, + teams=None, + locations=None, + ) + + def test_page_type_save(tmp_comic): md = tmp_comic.read_cix() t = md.pages[0] diff --git a/tests/metadata_test.py b/tests/metadata_test.py index 47938f2..6405889 100644 --- a/tests/metadata_test.py +++ b/tests/metadata_test.py @@ -18,7 +18,6 @@ def test_cbi(): md = CBI.metadata_from_string(string) md_test = comicapi.genericmetadata.md_test.replace( day=None, - language="English", page_count=None, maturity_rating=None, story_arc=None, diff --git a/tests/utils_test.py b/tests/utils_test.py index 397e823..960c018 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -63,17 +63,18 @@ def test_xlate(value, result): language_values = [ - ("en", "English"), - ("EN", "English"), - ("En", "English"), - ("", None), + ("english", "en"), + ("ENGLISH", "en"), + ("EnglisH", "en"), + ("", ""), + ("aaa", None), # does not have a 2-letter code (None, None), ] @pytest.mark.parametrize("value, result", language_values) -def test_get_language(value, result): - assert result == comicapi.utils.get_language(value) +def test_get_language_iso(value, result): + assert result == comicapi.utils.get_language_iso(value) def test_unique_file(tmp_path):