88
99from pydantic import BaseModel
1010
11- from hermes .commands .base import HermesCommand
11+ from hermes .commands .base import HermesCommand , HermesPlugin
12+ from hermes .error import HermesPluginRunError , MisconfigurationError
1213from hermes .model import SoftwareMetadata
1314from hermes .model .context_manager import HermesContext
1415from hermes .model .error import HermesValidationError
1516
1617
18+ class HermesCuratePlugin (HermesPlugin ):
19+ """ Base plugin for curate plugins. """
20+
21+ def __call__ (self , command : HermesCommand , metadata : SoftwareMetadata ) -> SoftwareMetadata :
22+ pass
23+
24+
1725class CurateSettings (BaseModel ):
1826 """Generic deposition settings."""
1927
20- pass
28+ plugin : str = ""
2129
2230
2331class HermesCurateCommand (HermesCommand ):
@@ -26,28 +34,41 @@ class HermesCurateCommand(HermesCommand):
2634 command_name = "curate"
2735 settings_class = CurateSettings
2836
29- def init_command_parser (self , command_parser : argparse .ArgumentParser ) -> None :
30- pass
31-
3237 def __call__ (self , args : argparse .Namespace ) -> None :
3338 self .log .info ("# Metadata curation" )
39+ plugin_name = self .settings .plugin
3440
3541 ctx = HermesContext ()
3642 ctx .prepare_step ("curate" )
3743
44+ self .log .info ("## Load processed metadata" )
45+ # load processed data
3846 ctx .prepare_step ("process" )
39- with ctx ["result" ] as process_ctx :
40- expanded_data = process_ctx ["expanded" ]
41- context_data = process_ctx ["context" ]
47+ try :
48+ metadata = SoftwareMetadata .load_from_cache (ctx , "result" )
49+ except Exception as e :
50+ self .log .error ("The data from the process step could not be loaded or is invalid for some reason." )
51+ raise HermesValidationError ("The results of the process step are invalid." ) from e
4252 ctx .finalize_step ("process" )
4353
54+ self .log .info ("## Load curation plugin" )
55+ # load plugin
4456 try :
45- data = SoftwareMetadata (expanded_data [0 ], context_data ["@context" ][1 ])
57+ plugin_func = self .plugins [plugin_name ]()
58+ except KeyError as e :
59+ self .log .error (f"Plugin { plugin_name } not found." )
60+ raise MisconfigurationError (f"Curate plugin { plugin_name } not found." )
61+
62+ self .log .info ("## Run curation plugin" )
63+ # run plugin
64+ try :
65+ curated_metadata = plugin_func (self , metadata )
4666 except Exception as e :
47- raise HermesValidationError ("The results of the process step are invalid." ) from e
67+ self .log .error (f"Unknown error while executing the { plugin_name } plugin." )
68+ raise HermesPluginRunError (f"Something went wrong while running the curate plugin { plugin_name } " ) from e
4869
49- with ctx [ "result" ] as curate_ctx :
50- curate_ctx [ "expanded" ] = data . ld_value
51- curate_ctx [ "context" ] = { "@context" : data . full_context }
70+ self . log . info ( "## Store curated data" )
71+ # store metadata
72+ curated_metadata . write_to_cache ( ctx , "result" )
5273
5374 ctx .finalize_step ("curate" )
0 commit comments