|
5 | 5 |
|
6 | 6 | """Module containing the Software CaRD curation plugin for HERMES.""" |
7 | 7 |
|
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | + |
8 | 11 | from hermes.commands.curate.base import BaseCuratePlugin |
| 12 | +from software_card_policies.config import Config |
| 13 | +from software_card_policies.data_model import ( |
| 14 | + make_shacl_graph, |
| 15 | + read_rdf_resource, |
| 16 | + validate_graph, |
| 17 | +) |
| 18 | +from software_card_policies.report import create_report |
9 | 19 |
|
10 | 20 |
|
11 | 21 | class SoftwareCaRDCuratePlugin(BaseCuratePlugin): |
12 | 22 | """Software CaRD curation plugin.""" |
13 | 23 |
|
14 | | - def is_publication_approved(self): |
| 24 | + def __init__(self, command, ctx): |
| 25 | + """Initialize the plugin.""" |
| 26 | + super().__init__(command, ctx) |
| 27 | + self._data_graph = None |
| 28 | + self._shacl_graph = None |
| 29 | + self._conforms = False |
| 30 | + self._validation_graph = None |
| 31 | + self._report = None |
| 32 | + |
| 33 | + def prepare(self): |
| 34 | + """Prepare the validation. |
| 35 | +
|
| 36 | + The metadata given in the context is parsed as an RDF graph. The configuration |
| 37 | + for the Software CaRD validation process is left empty. |
| 38 | + """ |
| 39 | + text = json.dumps(self.ctx.get_data()["curate"]) |
| 40 | + self._data_graph = read_rdf_resource(format="json-ld", data=text) |
| 41 | + self._shacl_graph = make_shacl_graph(Config.from_dict({"policies": {}})) |
| 42 | + |
| 43 | + def validate(self): |
| 44 | + """Run Software CaRD validation.""" |
| 45 | + conforms, validation_graph = validate_graph(self._data_graph, self._shacl_graph) |
| 46 | + self._conforms = conforms |
| 47 | + self._validation_graph = validation_graph |
| 48 | + |
| 49 | + def create_report(self): |
| 50 | + """Create basic text report.""" |
| 51 | + self._report = create_report(self._validation_graph) |
| 52 | + |
| 53 | + def is_publication_approved(self) -> bool: |
15 | 54 | """Decide whether the publication of the software is approved.""" |
16 | | - return False |
| 55 | + return self._conforms |
| 56 | + |
| 57 | + def process_decision_positive(self): |
| 58 | + """Write the given metadata into the curate directory.""" |
| 59 | + curate_output = Path(self.ctx.get_cache("curate", self.ctx.hermes_name)) |
| 60 | + Path.mkdir(curate_output.parent) |
| 61 | + with open(curate_output, "w") as curate_output_fh: |
| 62 | + json.dump(self.ctx.get_data(), curate_output_fh) |
0 commit comments