1919from hermes .commands .deposit .base import BaseDepositPlugin
2020from hermes .commands .deposit .error import DepositionUnauthorizedError
2121from hermes .error import MisconfigurationError
22- from hermes .model import SoftwareMetadata
2322from hermes .model .error import HermesValidationError
2423from hermes .utils import hermes_doi , hermes_user_agent
2524
@@ -320,7 +319,12 @@ def prepare(self) -> None:
320319 record_id = rec_id , doi = doi , codemeta_identifier = codemeta_identifier
321320 )
322321
323- version = self .metadata ["version" ]
322+ if len (self .metadata .get ("version" , [])) > 1 :
323+ raise HermesValidationError ("Too many licenses for invenio deposit." )
324+ if len (self .metadata .get ("version" , [])) == 1 :
325+ version = self .metadata ["version" ][0 ]
326+ else :
327+ version = None
324328 if rec_meta and (version == rec_meta .get ("version" )):
325329 raise ValueError (f"Version { version } already deposited." )
326330
@@ -336,10 +340,10 @@ def prepare(self) -> None:
336340
337341 self .invenio_ctx = deposition_data
338342
339- def map_metadata (self ) -> SoftwareMetadata :
343+ def map_metadata (self ) -> dict :
340344 """Map the harvested metadata onto the Invenio schema and return it."""
341345 self .invenio_ctx ["depositionMetadata" ] = self ._codemeta_to_invenio_deposition ()
342- return SoftwareMetadata ( self .invenio_ctx ["depositionMetadata" ])
346+ return self .invenio_ctx ["depositionMetadata" ]
343347
344348 def is_initial_publication (self ) -> bool :
345349 latest_record_id = self .invenio_ctx .get ("latestRecord" , {}).get ("id" )
@@ -398,7 +402,7 @@ def related_identifiers(self):
398402 },
399403 ]
400404
401- def update_metadata (self ) -> SoftwareMetadata :
405+ def update_metadata (self ) -> dict :
402406 """Update the metadata of a draft and return it."""
403407
404408 draft_url = self .links ["latest_draft" ]
@@ -418,7 +422,7 @@ def update_metadata(self) -> SoftwareMetadata:
418422 self .links .update (deposit ["links" ])
419423
420424 _log .debug ("Created new version deposit: %s" , self .links ["html" ])
421- return SoftwareMetadata ( deposit . get ( "metadata" , {}))
425+ return deposit
422426
423427 def delete_artifacts (self ) -> None :
424428 """Delete existing file artifacts.
@@ -508,21 +512,25 @@ def _codemeta_to_invenio_deposition(self) -> dict:
508512 access_conditions = self .invenio_ctx ["access_conditions" ]
509513
510514 creators = []
511- for author in metadata [ "author" ] :
515+ for author in metadata . get ( "author" , []) :
512516 creator = {}
513- if len (affils := [name for affil in author ["affiliation" ] for name in affil ["legalname" ]]) != 0 :
517+ if len (
518+ affils := [
519+ name for affil in author .get ("affiliation" , []) for name in affil .get ("legalname" , [])
520+ ]
521+ ) != 0 :
514522 creator ["affiliation" ] = affils
515- if len (author ["familyName" ]) > 1 :
516- raise HermesValidationError (f"Author has too many family names: { author .to_python ()} " )
517- if len (author ["familyName" ]) == 1 :
518- given_names_str = " " .join (author ["givenName" ])
523+
524+ if len (author .get ("familyName" , [])) > 1 :
525+ raise HermesValidationError (f"Author has too many family names: { author } " )
526+ if len (author .get ("familyName" , [])) == 1 :
527+ given_names_str = " " .join (author .get ("givenName" , []))
519528 name = f"{ author ["familyName" ][0 ]} , { given_names_str } "
520- elif len (author [ "name" ] ) != 1 :
521- raise HermesValidationError (f"Author has too many names: { author . to_python () } " )
529+ elif len (author . get ( "name" , []) ) != 1 :
530+ raise HermesValidationError (f"Author has too many or no names: { author } " )
522531 else :
523532 name = author ["name" ][0 ]
524- if len (name ) != 0 :
525- creator ["name" ] = name
533+ creator ["name" ] = name
526534 if (id := author .get ("@id" , None )) is not None :
527535 creator ["orcid" ] = id .replace ("https://orcid.org/" , "" )
528536 if creator :
@@ -545,6 +553,7 @@ def _codemeta_to_invenio_deposition(self) -> dict:
545553 for author in metadata["author"]
546554 ]"""
547555
556+ # TODO: reimplement with new api
548557 # This is not used at the moment. See comment below in `deposition_metadata` dict.
549558 contributors = [ # noqa: F841
550559 # TODO: Distinguish between @type "Person" and others
@@ -566,27 +575,33 @@ def _codemeta_to_invenio_deposition(self) -> dict:
566575 for contributor in metadata .get ("contributor" , []) if contributor .get ("name" ) != "GitHub"
567576 ]
568577
569- if len (metadata [ "name" ] ) != 1 :
578+ if len (metadata . get ( "name" , []) ) != 1 :
570579 _log .error ("More than one or zero names for the Software are given." )
571580 raise HermesValidationError ("More than one or zerno names for the Software." )
572581 name = metadata ["name" ][0 ]
573582
574- if len (metadata [ "schema:description" ] ) > 1 :
583+ if len (metadata . get ( "schema:description" , []) ) > 1 :
575584 _log .error ("More than one descriptions of the Software are given." )
576585 raise HermesValidationError ("More than one descriptions of the Software are given." )
577- if len (metadata [ "schema:description" ] ) == 1 :
586+ if len (metadata . get ( "schema:description" , []) ) == 1 :
578587 description = metadata ["schema:description" ][0 ]
579588 else :
580589 description = None
581590
582- if len (metadata [ "schema:version" ] ) > 1 :
591+ if len (metadata . get ( "schema:version" , []) ) > 1 :
583592 _log .error ("More than one version of the Software are given." )
584593 raise HermesValidationError ("More than one version of the Software are given." )
585- if len (metadata [ "schema:version" ] ) == 1 :
594+ if len (metadata . get ( "schema:version" , []) ) == 1 :
586595 version = metadata ["schema:version" ][0 ]
587596 else :
588597 version = None
589598
599+ keywords = metadata .get ("schema:keywords" , [])
600+ if len (keywords ) == 0 :
601+ keywords = None
602+ else :
603+ keywords = keywords .to_python ()
604+
590605 # TODO: Use the fields currently set to `None`.
591606 # Some more fields are available but they most likely don't relate to software
592607 # publications targeted by hermes.
@@ -602,9 +617,6 @@ def _codemeta_to_invenio_deposition(self) -> dict:
602617 "publication_date" : date .today ().isoformat (),
603618 "title" : name ,
604619 "creators" : creators ,
605- # TODO: Use a real description here. Possible sources could be
606- # `tool.poetry.description` from pyproject.toml or `abstract` from
607- # CITATION.cff. This should then be stored in codemeta description field.
608620 "description" : description ,
609621 "access_right" : access_right ,
610622 "license" : license ,
@@ -618,8 +630,8 @@ def _codemeta_to_invenio_deposition(self) -> dict:
618630 # them.
619631 # TODO: Use the DOI we get back from this.
620632 "prereserve_doi" : True ,
621- # TODO: A good source for this could be `tool.poetry. keywords` in pyproject.toml.
622- "keywords" : None ,
633+ "keywords" : keywords ,
634+ # TODO: Is there a good codemeta/ schema field?
623635 "notes" : None ,
624636 "related_identifiers" : self .related_identifiers (),
625637 # TODO: Use `contributors`. In the case of the hermes workflow itself, the
@@ -641,6 +653,10 @@ def _get_license_identifier(self) -> Union[str, None]:
641653
642654 If no license is configured, ``None`` will be returned.
643655 """
656+ if "license" not in self .metadata :
657+ raise HermesValidationError ("No license is given." )
658+ if len (self .metadata ["license" ]) > 1 :
659+ raise HermesValidationError ("Too many licenses for invenio deposit." )
644660 license_url = self .metadata ["license" ][0 ]
645661 return self .resolver .resolve_license_id (license_url )
646662
0 commit comments