Skip to content

Commit e42deb6

Browse files
committed
bibtex: Robustify code extracting year and month from date
Previous implementation could lead to errors when an invalid date is present in source codemeta.
1 parent 4cc7fd8 commit e42deb6

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

swh/indexer/bibtex.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Any, Dict, List, Optional
1212
import uuid
1313

14+
import iso8601
1415
from pybtex.database import Entry, Person
1516
from pybtex.database.output.bibtex import Writer
1617
from pybtex.plugin import register_plugin
@@ -130,10 +131,14 @@ def add_affiliations(person: rdflib.term.Node) -> None:
130131
fields["date"] = date
131132
break
132133
if "date" in fields:
133-
(fields["year"], month_number, _) = fields["date"].split("-")
134-
fields["month"] = (
135-
f"{MACRO_PREFIX}:{calendar.month_abbr[int(month_number)].lower()}"
136-
)
134+
try:
135+
parsed_date = iso8601.parse_date(fields["date"])
136+
fields["year"] = str(parsed_date.year)
137+
fields["month"] = (
138+
f"{MACRO_PREFIX}:{calendar.month_abbr[parsed_date.month].lower()}"
139+
)
140+
except iso8601.ParseError:
141+
pass
137142

138143
# identifier, doi, hal_id
139144
entry_key = None

swh/indexer/tests/test_bibtex.py

+23
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,29 @@ def test_affiliation():
276276
)
277277

278278

279+
def test_invalid_date():
280+
assert codemeta_to_bibtex(
281+
{
282+
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
283+
"author": {"name": "Jane Doe"},
284+
"name": "Example Software",
285+
"url": "http://example.org/",
286+
"datePublished": "TBD",
287+
"license": "https://spdx.org/licenses/Apache-2.0",
288+
}
289+
) == textwrap.dedent(
290+
"""\
291+
@software{REPLACEME,
292+
author = "Doe, Jane",
293+
license = "Apache-2.0",
294+
date = "TBD",
295+
title = "Example Software",
296+
url = "http://example.org/"
297+
}
298+
"""
299+
)
300+
301+
279302
def test_cff_empty():
280303
assert cff_to_bibtex("") == textwrap.dedent(
281304
"""\

0 commit comments

Comments
 (0)