Skip to content

Handle multiple affiliations in Zenodo for CFF 1.3.0+ #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/cffconvert/lib/cff_1_3_x/authors/zenodo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from cffconvert.lib.cff_1_x_x.authors.zenodo import ZenodoAuthor

def _get_id_from_ror_url(affiliation):
return affiliation.get("ror").replace("https://ror.org/", "")

# transform the input json
def _parse_affiliations(json_dict, affiliations):
# handle multiple affiliations, and output a list
if type(affiliations) == list:
new_affil = []
for affil in affiliations:
# only string
if type(affil) == str:
new_affil.append({'name': affil})
else:
# if 'ror' is present replace 'ror' with 'id' in the json
if 'ror' in affil:
affil['id'] = _get_id_from_ror_url(affil)
del affil['ror']
new_affil.append(affil)
json_dict["affiliations"] = new_affil
# otherwise default to handling it as plain string
elif type(affiliations) == str:
json_dict["affiliation"] = affiliations

return json_dict

# pylint: disable=too-few-public-methods
class ZenodoAuthorAffiliation(ZenodoAuthor):

def __init__(self, author):
super().__init__(author)

def _from_affiliation(self):
return _parse_affiliations({}, self._author.get("affiliation"))

def _from_affiliation_and_orcid(self):
return _parse_affiliations(
{
"affiliation": self._author.get("affiliation"),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation"))

def _from_alias_and_affiliation(self):
return parse_affilations(
{
"name": self._author.get("alias")
},
self._author.get("affiliation"))

def _from_alias_and_affiliation_and_orcid(self):
return _parse_affiliations(
{
"name": self._author.get("alias"),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation"))

def _from_given_and_affiliation(self):
return _parse_affiliations(
{
"name": self._author.get("given-names")
},
self._author.get("affiliation"))

def _from_given_and_affiliation_and_orcid(self):
return _parse_affiliations(
{
"name": self._author.get("given-names"),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation"))

def _from_given_and_last_and_affiliation(self):
return _parse_affiliations(
{
"name": self._get_full_last_name() + ", " + self._author.get("given-names")
},
self._author.get("affiliation"))

def _from_given_and_last_and_affiliation_and_orcid(self):
return _parse_affiliations(
{
"name": self._get_full_last_name() + ", " + self._author.get("given-names"),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation")
)

def _from_last_and_affiliation(self):
return _parse_affiliations(
{
"name": self._get_full_last_name()
},
self._author.get("affiliation"))

def _from_last_and_affiliation_and_orcid(self):
return _parse_affiliations(
{
"name": self._get_full_last_name(),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation"))

def _from_name_and_affiliation(self):
return _parse_affiliations(
{
"name": self._author.get("name")
},
self._author.get("affiliation")
)

def _from_name_and_affiliation_and_orcid(self):
return _parse_affiliations(
{
"name": self._author.get("name"),
"orcid": self._get_id_from_orcid_url()
},
self._author.get("affiliation"))
6 changes: 3 additions & 3 deletions src/cffconvert/lib/cff_1_3_x/zenodo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cffconvert.lib.cff_1_x_x.authors.zenodo import ZenodoAuthor
from cffconvert.lib.cff_1_3_x.authors.zenodo import ZenodoAuthorAffiliation
from cffconvert.lib.cff_1_x_x.zenodo import ZenodoObjectShared as Shared


Expand All @@ -13,7 +13,7 @@ def add_contributors(self):
for c in self.cffobj.get("contributors", []):
# contributors are generated in the same way as authors, hence just
# call ZenodoAuthor's contructor with the cff contributor object
contributor = ZenodoAuthor(c).as_dict()
contributor = ZenodoAuthorAffiliation(c).as_dict()
contributor.update({"type": "Other"})
contributors.append(contributor)
# filter out contributors that had no data associated with them
Expand All @@ -24,7 +24,7 @@ def add_contributors(self):

def add_creators(self):
authors_cff = self.cffobj.get("authors", [])
creators_zenodo = [ZenodoAuthor(a).as_dict() for a in authors_cff]
creators_zenodo = [ZenodoAuthorAffiliation(a).as_dict() for a in authors_cff]
self.creators = [c for c in creators_zenodo if c is not None]
return self

Expand Down
29 changes: 27 additions & 2 deletions src/cffconvert/schemas/1.3.0/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@
"$ref": "#/$defs/strictish-string",
"description": "An address."
},
"affiliation": {
"oneOf": [
{
"$ref": "#/$defs/strictish-string"
},
{
"$ref": "#/$defs/entity"
},
{
"items": {
"oneOf": [
{
"$ref": "#/$defs/strictish-string"
},
{
"$ref": "#/$defs/entity"
}
]
},
"minItems": 1,
"type": "array",
"uniqueItems": true
}
]
},
"alias": {
"$ref": "#/$defs/strictish-string",
"description": "An alias."
Expand Down Expand Up @@ -304,7 +329,7 @@
"description": "The entity's address."
},
"affiliation": {
"$ref": "#/$defs/strictish-string",
"$ref": "#/$defs/affiliation",
"description": "The entity's affiliation."
},
"alias": {
Expand Down Expand Up @@ -1199,7 +1224,7 @@
"description": "The person's address."
},
"affiliation": {
"$ref": "#/$defs/strictish-string",
"$ref": "#/$defs/affiliation",
"description": "The person's affiliation."
},
"alias": {
Expand Down