Skip to content
Snippets Groups Projects
Commit e42deb6d authored by Antoine Lambert's avatar Antoine Lambert
Browse files

bibtex: Robustify code extracting year and month from date

Previous implementation could lead to errors when an invalid date
is present in source codemeta.
parent 4cc7fd82
No related branches found
No related tags found
1 merge request!527bibtex: Robustify code extracting year and month from date
Pipeline #11967 passed
......@@ -11,6 +11,7 @@ import sys
from typing import Any, Dict, List, Optional
import uuid
import iso8601
from pybtex.database import Entry, Person
from pybtex.database.output.bibtex import Writer
from pybtex.plugin import register_plugin
......@@ -130,10 +131,14 @@ def codemeta_to_bibtex(
fields["date"] = date
break
if "date" in fields:
(fields["year"], month_number, _) = fields["date"].split("-")
fields["month"] = (
f"{MACRO_PREFIX}:{calendar.month_abbr[int(month_number)].lower()}"
)
try:
parsed_date = iso8601.parse_date(fields["date"])
fields["year"] = str(parsed_date.year)
fields["month"] = (
f"{MACRO_PREFIX}:{calendar.month_abbr[parsed_date.month].lower()}"
)
except iso8601.ParseError:
pass
# identifier, doi, hal_id
entry_key = None
......
......@@ -276,6 +276,29 @@ def test_affiliation():
)
def test_invalid_date():
assert codemeta_to_bibtex(
{
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"author": {"name": "Jane Doe"},
"name": "Example Software",
"url": "http://example.org/",
"datePublished": "TBD",
"license": "https://spdx.org/licenses/Apache-2.0",
}
) == textwrap.dedent(
"""\
@software{REPLACEME,
author = "Doe, Jane",
license = "Apache-2.0",
date = "TBD",
title = "Example Software",
url = "http://example.org/"
}
"""
)
def test_cff_empty():
assert cff_to_bibtex("") == textwrap.dedent(
"""\
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment