1717import requests
1818from pydantic import BaseModel
1919
20- from hermes .commands .deposit .base import BaseDepositPlugin , HermesDepositCommand
20+ from hermes .commands .deposit .base import BaseDepositPlugin
2121from hermes .commands .deposit .error import DepositionUnauthorizedError
2222from hermes .error import MisconfigurationError
23- from hermes .model .context import CodeMetaContext
24- from hermes .model .path import ContextPath
23+ from hermes .model .context_manager import HermesContext
2524from hermes .utils import hermes_doi , hermes_user_agent
2625
2726
@@ -258,11 +257,13 @@ class InvenioDepositPlugin(BaseDepositPlugin):
258257 invenio_resolver_class = InvenioResolver
259258 settings_class = InvenioDepositSettings
260259
261- def __init__ (self , command : HermesDepositCommand , ctx : CodeMetaContext , client = None , resolver = None ) -> None :
262- super ().__init__ (command , ctx )
260+ def __init__ (self ) -> None :
261+ super ().__init__ ()
263262
264- self .invenio_context_path = ContextPath .parse (f"deposit.{ self .platform_name } " )
265263 self .invenio_ctx = None
264+
265+ def __call__ (self , command , * , client = None , resolver = None ):
266+ self .command = command
266267 self .config = getattr (self .command .settings , self .platform_name )
267268
268269 if client is None :
@@ -292,7 +293,9 @@ def __init__(self, command: HermesDepositCommand, ctx: CodeMetaContext, client=N
292293 self .resolver = resolver or self .invenio_resolver_class (self .client )
293294 self .links = {}
294295
295- # TODO: Populate some data structure here? Or move more of this into __init__?
296+ super ().__call__ (command )
297+
298+ # TODO: Populate some data structure here? Or move more of this into __init__.py?
296299 def prepare (self ) -> None :
297300 """Prepare the deposition on an Invenio-based platform.
298301
@@ -305,49 +308,42 @@ def prepare(self) -> None:
305308 - check access modalities (access right, access conditions, embargo data, existence
306309 of license)
307310 - check whether required configuration options are present
308- - update ``self.ctx `` with metadata collected during the checks
311+ - update ``self.metadata `` with metadata collected during the checks
309312 """
310313
311314 rec_id = self .config .record_id
312315 doi = self .config .doi
313316
314- try :
315- codemeta_identifier = self .ctx ["codemeta.identifier" ]
316- except KeyError :
317- codemeta_identifier = None
318-
317+ codemeta_identifier = self .metadata .get ("identifier" , None )
319318 rec_id , rec_meta = self .resolver .resolve_latest_id (
320319 record_id = rec_id , doi = doi , codemeta_identifier = codemeta_identifier
321320 )
322321
323- version = self .ctx [ "codemeta" ]. get ( " version")
322+ version = self .metadata [ " version"]
324323 if rec_meta and (version == rec_meta .get ("version" )):
325324 raise ValueError (f"Version { version } already deposited." )
326325
327- self .ctx .update (self .invenio_context_path ['latestRecord' ], {'id' : rec_id , 'metadata' : rec_meta })
328-
329- license = self ._get_license_identifier ()
330- self .ctx .update (self .invenio_context_path ["license" ], license )
331-
332- communities = self ._get_community_identifiers ()
333- self .ctx .update (self .invenio_context_path ["communities" ], communities )
326+ deposition_data = {}
327+ deposition_data ["latestRecord" ] = {'id' : rec_id , 'metadata' : rec_meta }
328+ deposition_data ["license" ] = self ._get_license_identifier ()
329+ deposition_data ["communities" ] = self ._get_community_identifiers ()
334330
335331 access_right , embargo_date , access_conditions = self ._get_access_modalities (license )
336- self . ctx . update ( self . invenio_context_path ["access_right" ], access_right )
337- self . ctx . update ( self . invenio_context_path ["embargo_date" ], embargo_date )
338- self . ctx . update ( self . invenio_context_path ["access_conditions" ], access_conditions )
332+ deposition_data ["access_right" ] = access_right
333+ deposition_data ["embargo_date" ] = embargo_date
334+ deposition_data ["access_conditions" ] = access_conditions
339335
340- self .invenio_ctx = self . ctx [ self . invenio_context_path ]
336+ self .invenio_ctx = deposition_data
341337
342338 def map_metadata (self ) -> None :
343339 """Map the harvested metadata onto the Invenio schema."""
344340
345341 deposition_metadata = self ._codemeta_to_invenio_deposition ()
346- self . ctx . update ( self . invenio_context_path [ "depositionMetadata" ], deposition_metadata )
347-
348- # Store a snapshot of the mapped data within the cache, useful for analysis, debugging, etc
349- with open ( self . ctx . get_cache ( "deposit" , self . platform_name , create = True ), 'w' ) as invenio_json :
350- json . dump ( deposition_metadata , invenio_json , indent = ' ' )
342+ ctx = HermesContext ( )
343+ ctx . prepare_step ( "deposit" )
344+ with ctx [ self . platform_name ] as deposit_ctx :
345+ deposit_ctx [ "deposit" ] = deposition_metadata
346+ ctx . finalize_step ( "deposit" )
351347
352348 def is_initial_publication (self ) -> bool :
353349 latest_record_id = self .invenio_ctx .get ("latestRecord" , {}).get ("id" )
@@ -426,7 +422,7 @@ def update_metadata(self) -> None:
426422 self .links .update (deposit ["links" ])
427423
428424 _log .debug ("Created new version deposit: %s" , self .links ["html" ])
429- with open (self .ctx .get_cache ('deposit' , 'deposit' , create = True ), 'w' ) as deposit_file :
425+ with open (self .metadata .get_cache ('deposit' , 'deposit' , create = True ), 'w' ) as deposit_file :
430426 json .dump (deposit , deposit_file , indent = 4 )
431427
432428 def delete_artifacts (self ) -> None :
@@ -505,7 +501,7 @@ def _codemeta_to_invenio_deposition(self) -> dict:
505501 differences between Invenio-based platforms.
506502 """
507503
508- metadata = self .ctx [ "codemeta" ]
504+ metadata = self .metadata
509505 license = self .invenio_ctx ["license" ]
510506 communities = self .invenio_ctx ["communities" ]
511507 access_right = self .invenio_ctx ["access_right" ]
@@ -520,7 +516,7 @@ def _codemeta_to_invenio_deposition(self) -> dict:
520516 "affiliation" : author .get ("affiliation" , {"legalName" : None }).get ("legalName" ),
521517 # Invenio wants "family, given". author.get("name") might not have this format.
522518 "name" : f"{ author .get ('familyName' )} , { author .get ('givenName' )} "
523- if author . get ( "familyName" ) and author . get ( "givenName" )
519+ if "familyName" in author and "givenName" in author
524520 else author .get ("name" ),
525521 # Invenio expects the ORCID without the URL part
526522 "orcid" : author .get ("@id" , "" ).replace ("https://orcid.org/" , "" ) or None ,
@@ -538,7 +534,7 @@ def _codemeta_to_invenio_deposition(self) -> dict:
538534 "affiliation" : contributor .get ("affiliation" , {"legalName" : None }).get ("legalName" ),
539535 # Invenio wants "family, given". contributor.get("name") might not have this format.
540536 "name" : f"{ contributor .get ('familyName' )} , { contributor .get ('givenName' )} "
541- if contributor . get ( "familyName" ) and contributor . get ( "givenName" )
537+ if "familyName" in contributor and "givenName" in contributor
542538 else contributor .get ("name" ),
543539 # Invenio expects the ORCID without the URL part
544540 "orcid" : contributor .get ("@id" , "" ).replace ("https://orcid.org/" , "" ) or None ,
@@ -604,15 +600,15 @@ def _get_license_identifier(self) -> t.Optional[str]:
604600
605601 If no license is configured, ``None`` will be returned.
606602 """
607- license_url = self .ctx [ "codemeta" ]. get ( " license")
603+ license_url = self .metadata [ " license"]
608604 return self .resolver .resolve_license_id (license_url )
609605
610606 def _get_community_identifiers (self ):
611607 """Get Invenio community identifiers from config.
612608
613609 This function gets the communities to be used for the deposition on an Invenio-based
614610 site from the config and checks their validity against the site's API. If one of the
615- identifiers can not be found on the site, a :class:`HermesMisconfigurationError ` is
611+ identifiers can not be found on the site, a :class:`MisconfigurationError ` is
616612 raised.
617613 """
618614
0 commit comments