Skip to content

Commit

Permalink
Use a comma as a delimiter for resource lists (#82)
Browse files Browse the repository at this point in the history
We also needed to double quote any string in the list containing a comma
  • Loading branch information
bpepple authored Mar 2, 2024
1 parent 6abc582 commit 0677929
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
21 changes: 18 additions & 3 deletions darkseid/comicinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions darkseid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down
18 changes: 13 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0677929

Please sign in to comment.