66"""Module containing the Software CaRD curation plugin for HERMES."""
77
88import json
9- from pathlib import Path
109
11- from hermes .commands .curate .base import BaseCuratePlugin
10+ from hermes .commands .base import HermesCommand
11+ from hermes .commands .curate .base import HermesCuratePlugin
12+ from hermes .model import SoftwareMetadata
13+ from hermes .model .hermes_cache import HermesCacheManager
1214from software_card_policies .config import Config
1315from software_card_policies .data_model import (
1416 make_shacl_graph ,
2022from hermes_plugin_software_card import environment
2123
2224
23- class SoftwareCaRDCuratePlugin (BaseCuratePlugin ):
25+ class SoftwareCaRDCuratePlugin (HermesCuratePlugin ):
2426 """Software CaRD curation plugin."""
2527
26- def __init__ (self , command , ctx ):
28+ def __init__ (self ):
2729 """Initialize the plugin."""
28- super ().__init__ (command , ctx )
30+ super ().__init__ ()
2931 self ._data_graph = None
3032 self ._shacl_graph = None
3133 self ._conforms = False
@@ -65,14 +67,34 @@ def __init__(self, command, ctx):
6567 }
6668 }
6769
68- def prepare (self ):
70+ def __call__ (
71+ self , command : HermesCommand , metadata : SoftwareMetadata
72+ ) -> SoftwareMetadata :
73+ """Entry point of the callable.
74+
75+ This method runs the main logic of the plugin. It calls the other methods of the
76+ object in the correct order. Depending on the result of
77+ ``is_publication_approved`` the corresponding ``process_decision_*()`` method is
78+ called, based on the curation decision.
79+ """
80+ self .prepare (metadata )
81+ self .validate ()
82+ self .create_report (metadata )
83+
84+ if not self .is_publication_approved ():
85+ return SoftwareMetadata ()
86+
87+ return metadata
88+
89+ def prepare (self , metadata : SoftwareMetadata ):
6990 """Prepare the validation.
7091
7192 The metadata given in the context is parsed as an RDF graph and then validated
7293 using the Software CaRD validation.
7394 """
74- text = json .dumps (self .ctx .get_data ()["curate" ])
75- self ._data_graph = read_rdf_resource (format = "json-ld" , data = text )
95+ self ._data_graph = read_rdf_resource (
96+ format = "json-ld" , data = json .dumps (metadata .ld_value )
97+ )
7698 self ._shacl_graph = make_shacl_graph (Config .from_dict (self ._validation_config ))
7799
78100 def validate (self ):
@@ -81,8 +103,17 @@ def validate(self):
81103 self ._conforms = conforms
82104 self ._validation_graph = validation_graph
83105
84- def create_report (self ):
85- """Create basic text report."""
106+ def create_report (self , metadata : SoftwareMetadata ):
107+ """Create validation report.
108+
109+ This creates the report both as a machine-readble JSON-LD file, and prints the
110+ URL to the Software CaRD web app to the screen.
111+ """
112+ ctx = HermesCacheManager ()
113+ validation_file = ctx .cache_dir / "curate" / "validation.json"
114+ validation_file .parent .mkdir (exist_ok = True , parents = True )
115+ self ._validation_graph .serialize (validation_file , format = "json-ld" )
116+
86117 self ._report = create_report (self ._validation_graph )
87118 if self ._environment is None :
88119 print ("Software CaRD plugin not running in CI environment." )
@@ -95,10 +126,3 @@ def create_report(self):
95126 def is_publication_approved (self ) -> bool :
96127 """Decide whether the publication of the software is approved."""
97128 return self ._conforms
98-
99- def process_decision_positive (self ):
100- """Write the given metadata into the curate directory."""
101- curate_output = Path (self .ctx .get_cache ("curate" , self .ctx .hermes_name ))
102- Path .mkdir (curate_output .parent )
103- with open (curate_output , "w" ) as curate_output_fh :
104- json .dump (self .ctx .get_data (), curate_output_fh )
0 commit comments