From 46dcb5b2a06e6bb6e15012811525202ddcf43277 Mon Sep 17 00:00:00 2001 From: vlorentz <vlorentz@softwareheritage.org> Date: Mon, 15 Jul 2024 11:21:21 +0000 Subject: [PATCH] bibtex: Fix crash on empty author list --- swh/indexer/bibtex.py | 7 +++++-- swh/indexer/tests/test_bibtex.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/swh/indexer/bibtex.py b/swh/indexer/bibtex.py index 9fd828dc..dba65dcb 100644 --- a/swh/indexer/bibtex.py +++ b/swh/indexer/bibtex.py @@ -13,7 +13,7 @@ from pybtex.database import Entry, Person import rdflib from swh.indexer.codemeta import compact, expand -from swh.indexer.namespaces import SCHEMA, SPDX_LICENSES +from swh.indexer.namespaces import RDF, SCHEMA, SPDX_LICENSES TMP_ROOT_URI_PREFIX = "https://www.softwareheritage.org/schema/2022/indexer/tmp-node/" """IRI used for `skolemization <https://www.w3.org/TR/rdf11-concepts/#section-skolemization>`_; @@ -55,7 +55,7 @@ def codemeta_to_bibtex(doc: Dict[str, Any]) -> str: def add_person(persons: List[Person], person_id: rdflib.term.Node) -> None: for _, _, name in g.triples((person_id, SCHEMA.name, None)): - if (person_id, rdflib.RDF.type, SCHEMA.Organization) in g: + if (person_id, RDF.type, SCHEMA.Organization) in g: # prevent interpreting the name as "Firstname Lastname" and reformatting # it to "Lastname, Firstname" person = Person(last=name) @@ -75,6 +75,9 @@ def codemeta_to_bibtex(doc: Dict[str, Any]) -> str: # authors, which are an ordered list for _, _, author_list in g.triples((id_, SCHEMA.author, None)): + if author_list == RDF.nil: + # Workaround for https://github.com/RDFLib/rdflib/pull/2818 + continue for author in rdflib.collection.Collection(g, author_list): add_person(persons["author"], author) add_affiliations(author) diff --git a/swh/indexer/tests/test_bibtex.py b/swh/indexer/tests/test_bibtex.py index 7d270baf..e0220d71 100644 --- a/swh/indexer/tests/test_bibtex.py +++ b/swh/indexer/tests/test_bibtex.py @@ -46,6 +46,28 @@ def test_minimal(): ) +def test_empty_author_list(): + assert codemeta_to_bibtex( + { + "@context": "https://doi.org/10.5063/schema/codemeta-2.0", + "author": [], + "name": "Example Software", + "url": "http://example.org/", + "datePublished": "2023-10-10", + } + ) == textwrap.dedent( + """\ + @software{REPLACEME, + date = "2023-10-10", + year = "2023", + month = "10", + title = "Example Software", + url = "http://example.org/" + } + """ + ) + + @pytest.mark.parametrize("key", ["version", "softwareVersion"]) def test_version_minimal(key): assert codemeta_to_bibtex( -- GitLab