-
Notifications
You must be signed in to change notification settings - Fork 0
Make the plugin ready for the new data model #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
7d0cd30
22df30e
df8f6ce
09a0236
f06467f
ecd924c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,4 @@ | |
| sources = ["cff"] | ||
|
|
||
| [curate] | ||
| method = "software_card" | ||
| plugin = "software_card" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,11 @@ | |
| """Module containing the Software CaRD curation plugin for HERMES.""" | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from hermes.commands.curate.base import BaseCuratePlugin | ||
| from hermes.commands.base import HermesCommand | ||
| from hermes.commands.curate.base import HermesCuratePlugin | ||
| from hermes.model import SoftwareMetadata | ||
| from hermes.model.hermes_cache import HermesCacheManager | ||
| from software_card_policies.config import Config | ||
| from software_card_policies.data_model import ( | ||
| make_shacl_graph, | ||
|
|
@@ -20,12 +22,12 @@ | |
| from hermes_plugin_software_card import environment | ||
|
|
||
|
|
||
| class SoftwareCaRDCuratePlugin(BaseCuratePlugin): | ||
| class SoftwareCaRDCuratePlugin(HermesCuratePlugin): | ||
| """Software CaRD curation plugin.""" | ||
|
|
||
| def __init__(self, command, ctx): | ||
| def __init__(self): | ||
| """Initialize the plugin.""" | ||
| super().__init__(command, ctx) | ||
| super().__init__() | ||
| self._data_graph = None | ||
| self._shacl_graph = None | ||
| self._conforms = False | ||
|
|
@@ -65,14 +67,34 @@ | |
| } | ||
| } | ||
|
|
||
| def prepare(self): | ||
| def __call__( | ||
| self, command: HermesCommand, metadata: SoftwareMetadata | ||
|
Check failure on line 71 in src/hermes_plugin_software_card/curate.py
|
||
| ) -> SoftwareMetadata: | ||
| """Entry point of the callable. | ||
|
|
||
| This method runs the main logic of the plugin. It calls the other methods of the | ||
| object in the correct order. Depending on the result of | ||
| ``is_publication_approved`` either the valid metadata or a new, empty | ||
| ``SoftwareMetadata`` object is returned. | ||
| """ | ||
| self.prepare(metadata) | ||
| self.validate() | ||
| self.create_report(metadata) | ||
|
|
||
| if not self.is_publication_approved(): | ||
| return SoftwareMetadata() | ||
|
|
||
| return metadata | ||
|
|
||
| def prepare(self, metadata: SoftwareMetadata): | ||
| """Prepare the validation. | ||
|
|
||
| The metadata given in the context is parsed as an RDF graph and then validated | ||
| using the Software CaRD validation. | ||
| """ | ||
| text = json.dumps(self.ctx.get_data()["curate"]) | ||
| self._data_graph = read_rdf_resource(format="json-ld", data=text) | ||
| self._data_graph = read_rdf_resource( | ||
| format="json-ld", data=json.dumps(metadata.ld_value) | ||
| ) | ||
| self._shacl_graph = make_shacl_graph(Config.from_dict(self._validation_config)) | ||
|
|
||
| def validate(self): | ||
|
|
@@ -81,8 +103,17 @@ | |
| self._conforms = conforms | ||
| self._validation_graph = validation_graph | ||
|
|
||
| def create_report(self): | ||
| """Create basic text report.""" | ||
| def create_report(self, metadata: SoftwareMetadata): | ||
|
Check failure on line 106 in src/hermes_plugin_software_card/curate.py
|
||
|
zyzzyxdonta marked this conversation as resolved.
Outdated
|
||
| """Create validation report. | ||
|
|
||
| This creates the report both as a machine-readble JSON-LD file, and prints the | ||
| URL to the Software CaRD web app to the screen. | ||
| """ | ||
| ctx = HermesCacheManager() | ||
| validation_file = ctx.cache_dir / "curate" / "validation.json" | ||
| validation_file.parent.mkdir(exist_ok=True, parents=True) | ||
| self._validation_graph.serialize(validation_file, format="json-ld") | ||
|
Comment on lines
+111
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the intended way? Do you mean with a We tried that but failed because the validation graph is a list and The interesting bit in this case is the validation report in ll. 443 - 454. The rest is just SHACL copied into the graph.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried working around it, taking a concise bounded description: report_refs = list(
self._validation_graph.subjects(
predicate=RDF.type, object=SH.ValidationReport
)
)
assert len(report_refs) == 1
report_ref = report_refs[0]
self._validation_graph.cbd(report_ref).serialize(
validation_file, format="json-ld"
)But this also creates a list: [
{
"@id": "_:Nd5d1172d2d2d46829819e6fdce9722a9",
"@type": [
"http://www.w3.org/ns/shacl#ValidationReport"
],
"http://www.w3.org/ns/shacl#conforms": [
{
"@type": "http://www.w3.org/2001/XMLSchema#boolean",
"@value": true
}
]
}
]Maybe the JSON-LD serializer in RDFlib always does this. I could return the single element from that list. But that won't work for failed validations because in those cases, the report is split into multiple objects rather than nested: |
||
|
|
||
| self._report = create_report(self._validation_graph) | ||
|
zyzzyxdonta marked this conversation as resolved.
Outdated
|
||
| if self._environment is None: | ||
| print("Software CaRD plugin not running in CI environment.") | ||
|
|
@@ -95,10 +126,3 @@ | |
| def is_publication_approved(self) -> bool: | ||
| """Decide whether the publication of the software is approved.""" | ||
| return self._conforms | ||
|
|
||
| def process_decision_positive(self): | ||
| """Write the given metadata into the curate directory.""" | ||
| curate_output = Path(self.ctx.get_cache("curate", self.ctx.hermes_name)) | ||
| Path.mkdir(curate_output.parent) | ||
| with open(curate_output, "w") as curate_output_fh: | ||
| json.dump(self.ctx.get_data(), curate_output_fh) | ||
Uh oh!
There was an error while loading. Please reload this page.