From 0677929ff159e43c70abdf2ffdd2c30132ab8826 Mon Sep 17 00:00:00 2001 From: Brian Pepple Date: Sat, 2 Mar 2024 15:41:00 -0500 Subject: [PATCH] Use a comma as a delimiter for resource lists (#82) We also needed to double quote any string in the list containing a comma --- darkseid/comicinfo.py | 21 ++++++++++++++++++--- darkseid/utils.py | 6 +++--- tests/conftest.py | 4 ++-- tests/test_metadata.py | 4 ++-- tests/test_utils.py | 18 +++++++++++++----- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/darkseid/comicinfo.py b/darkseid/comicinfo.py index 30a452a..110e8bf 100644 --- a/darkseid/comicinfo.py +++ b/darkseid/comicinfo.py @@ -256,15 +256,30 @@ def get(txt: str) -> str | int | None: tag = root.find(txt) return None if tag is None else tag.text + def clean_resource_list(string: str) -> list[str]: + res = list(map(str.strip, (filter(None, split(r',|"(.*?)"', string))))) + # Remove empty values + for item in res: + if not item: + res.remove(item) + return res + def string_to_resource(string: str) -> list[Basic] | None: if string is not None: - # TODO: Make the delimiter also check for ',' - return [Basic(x.strip()) for x in string.split(";")] + res: list[str | Basic] = clean_resource_list(string) + # Now let's add the dataclass + for count, item in enumerate(res): + res[count] = Basic(item) + return res return None def string_to_arc(string: str) -> list[Arc] | None: if string is not None: - return [Arc(x.strip()) for x in string.split(";")] + res: list[str | Arc] = clean_resource_list(string) + # Now let's add the dataclass + for count, item in enumerate(res): + res[count] = Arc(item) + return res return None md = Metadata() diff --git a/darkseid/utils.py b/darkseid/utils.py index 8473fce..be874ff 100644 --- a/darkseid/utils.py +++ b/darkseid/utils.py @@ -27,13 +27,13 @@ def get_recursive_filelist(pathlist: list[Path]) -> list[Path]: def list_to_string(list_of_strings: list[str]) -> str: """Function that takes a list of string and converts it to a string. - For example: ["apple", "banana", "cherry"] is changed to "apple; banana; cherry". + For example: ["apple", "banana", "cherry, inc"] is changed + to 'apple; banana; "cherry, inc"'. :param list_of_strings: A list of strings. :type list_of_strings: list of str """ - # TODO: Use a comma for the delimiter since a lot of servers don't recognize the semicolon. - return "; ".join(map(str, list_of_strings)) + return ", ".join((f'"{item}"' if "," in item else item) for item in list_of_strings) def remove_articles(text: str) -> str: diff --git a/tests/conftest.py b/tests/conftest.py index 5dd2ab7..7c05e99 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,13 +27,13 @@ def fake_metadata() -> Metadata: md.stories = [Basic("A Crash of Symbols")] md.publisher = Basic("DC Comics") md.cover_date = date(1994, 12, 1) - md.story_arcs = [Arc("Final Crisis")] + md.story_arcs = [Arc("Final Crisis, Inc")] md.characters = [ Basic("Aquaman"), Basic("Mera"), Basic("Garth"), ] - md.teams = [Basic("Justice League"), Basic("Teen Titans")] + md.teams = [Basic("Justice League"), Basic("Infinity, Inc")] md.comments = "Just some sample metadata." md.black_and_white = True md.is_empty = False diff --git a/tests/test_metadata.py b/tests/test_metadata.py index a75eab8..97a3b31 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -24,9 +24,9 @@ def test_metadata_print_str(fake_metadata: Metadata) -> None: genres = [], comments = 'Just some sample metadata.', black_and_white = True, - story_arcs = [Arc(name='Final Crisis', id_=None, number=None)], + story_arcs = [Arc(name='Final Crisis, Inc', id_=None, number=None)], characters = [Basic(name='Aquaman', id_=None), Basic(name='Mera', id_=None), Basic(name='Garth', id_=None)], - teams = [Basic(name='Justice League', id_=None), Basic(name='Teen Titans', id_=None)], + teams = [Basic(name='Justice League', id_=None), Basic(name='Infinity, Inc', id_=None)], locations = [], credits = [], reprints = [], diff --git a/tests/test_utils.py b/tests/test_utils.py index ffaaa61..74e6e57 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -22,12 +22,20 @@ def test_file_name_for_articles( assert result == expected -def test_list_to_string() -> None: - thislist = ["apple", "banana", "cherry"] - expected_result = "apple; banana; cherry" +test_string_lists = [ + pytest.param(["apple", "banana", "cherry"], "Normal string list", "apple, banana, cherry"), + pytest.param( + ["Outsiders", "Infinity, Inc.", "Teen Titans"], + "String list with comma value", + 'Outsiders, "Infinity, Inc.", Teen Titans', + ), +] - result = utils.list_to_string(thislist) - assert result == expected_result + +@pytest.mark.parametrize(("test_list", "reason", "expected"), test_string_lists) +def test_list_to_string(test_list: list[str], reason: str, expected: str) -> None: # noqa: ARG001 + result = utils.list_to_string(test_list) + assert result == expected def test_unique_name(tmp_path: Path) -> None: