From b66f3c57b440e7c92e83a31854a0a0e4da5147e6 Mon Sep 17 00:00:00 2001 From: muddymudskipper Date: Sun, 30 Jun 2024 13:00:50 +0100 Subject: [PATCH] move cleanup to utils.py, annotate inferred axioms not advanced parameter --- CHANGELOG.md | 3 ++- cmem_plugin_reason/plugin_reason.py | 25 +++++-------------------- cmem_plugin_reason/plugin_validate.py | 23 ++++++----------------- cmem_plugin_reason/utils.py | 16 +++++++++++++++- 4 files changed, 28 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9858111..fd55505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - valid range of "Maximum RAM Percentage" parameter in Validate plugin (1-100) -### Change +### Changed - completed validation for IRI parameters +- "Annnotate inferred subclass axioms" is now not an advanced parameter ## [1.0.0alpha3] 2024-06-28 diff --git a/cmem_plugin_reason/plugin_reason.py b/cmem_plugin_reason/plugin_reason.py index 0267af2..31c8399 100644 --- a/cmem_plugin_reason/plugin_reason.py +++ b/cmem_plugin_reason/plugin_reason.py @@ -25,6 +25,7 @@ ROBOT, create_xml_catalog_file, get_graphs_tree, + remove_temp, send_result, ) @@ -57,7 +58,7 @@ param_type=StringParameterType(), name="result_graph_iri", label="Result graph IRI", - description="The IRI of the output graph for the reasoning result. ⚠️ Existing graph " + description="The IRI of the output graph for the reasoning result. ⚠️ Existing graphs " "will be overwritten.", ), PluginParameter( @@ -162,10 +163,9 @@ param_type=BoolParameterType(), name="annotate_inferred_axioms", label="Annnotate inferred subclass axioms", - description="Annotate inferred subclass axioms. ⚠️ This parameter can only be enabled " - "if the only enabled axiom generator is SubClass.", + description="⚠️ This parameter can only be enabled if the only enabled axiom generator " + "is SubClass.", default_value=False, - advanced=True, ), ], ) @@ -211,7 +211,6 @@ def __init__( # noqa: PLR0913, C901 "ObjectPropertyRange": object_property_range, "ObjectPropertyDomain": object_property_domain, } - errors = "" if not validators.url(data_graph_iri): errors += 'Invalid IRI for parameter "Data graph IRI". ' @@ -299,20 +298,6 @@ def reason(self, graphs: dict) -> None: raise OSError(response.stderr.decode()) raise OSError("ROBOT error") - def clean_up(self, graphs: dict) -> None: - """Remove temporary files""" - files = ["catalog-v001.xml", "result.ttl"] - files += list(graphs.values()) - for file in files: - try: - (Path(self.temp) / file).unlink() - except (OSError, FileNotFoundError) as err: - self.log.warning(f"Cannot remove file {file} ({err})") - try: - Path(self.temp).rmdir() - except (OSError, FileNotFoundError) as err: - self.log.warning(f"Cannot remove directory {self.temp} ({err})") - def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002 """Execute plugin""" setup_cmempy_user_access(context.user) @@ -322,4 +307,4 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: AR self.reason(graphs) setup_cmempy_user_access(context.user) send_result(self.result_graph_iri, Path(self.temp) / "result.ttl") - self.clean_up(graphs) + remove_temp(self, ["catalog-v001.xml", "result.ttl", *graphs.values()]) diff --git a/cmem_plugin_reason/plugin_validate.py b/cmem_plugin_reason/plugin_validate.py index 3fca096..fa55ad3 100644 --- a/cmem_plugin_reason/plugin_validate.py +++ b/cmem_plugin_reason/plugin_validate.py @@ -32,6 +32,7 @@ ROBOT, create_xml_catalog_file, get_graphs_tree, + remove_temp, send_result, ) @@ -112,7 +113,6 @@ def __init__( # noqa: PLR0913 errors += 'Invalid value for parameter "Maximum RAM Percentage". ' if errors: raise ValueError(errors[:-1]) - self.ontology_graph_iri = ontology_graph_iri self.reasoner = reasoner self.produce_graph = produce_graph @@ -174,20 +174,6 @@ def make_resource(self, context: ExecutionContext) -> None: replace=True, ) - def clean_up(self, graphs: dict) -> None: - """Remove temporary files""" - files = ["catalog-v001.xml", "output.ttl", self.md_filename] - files += list(graphs.values()) - for file in files: - try: - (Path(self.temp) / file).unlink() - except (OSError, FileNotFoundError) as err: - self.log.warning(f"Cannot remove file {file} ({err})") - try: - Path(self.temp).rmdir() - except (OSError, FileNotFoundError) as err: - self.log.warning(f"Cannot remove directory {self.temp} ({err})") - def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None: # noqa: ARG002 """Run the workflow operator.""" setup_cmempy_user_access(context.user) @@ -195,10 +181,13 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None: self.get_graphs(graphs, context) create_xml_catalog_file(self.temp, graphs) self.validate(graphs) + files = ["catalog-v001.xml", self.md_filename, *graphs.values()] + if self.produce_graph: + files.append("output.ttl") text = (Path(self.temp) / self.md_filename).read_text() if text == "No explanations found.": - self.clean_up(graphs) + remove_temp(self, files) return None if self.produce_graph: @@ -209,7 +198,7 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None: setup_cmempy_user_access(context.user) self.make_resource(context) - self.clean_up(graphs) + remove_temp(self, files) if self.stop_at_inconsistencies: raise RuntimeError("Inconsistencies found in Ontology.") diff --git a/cmem_plugin_reason/utils.py b/cmem_plugin_reason/utils.py index 0b06e25..c0e37fa 100644 --- a/cmem_plugin_reason/utils.py +++ b/cmem_plugin_reason/utils.py @@ -1,4 +1,4 @@ -"""Common functions""" +"""Common constants and functions""" import re import unicodedata @@ -10,6 +10,7 @@ from cmem_plugin_base.dataintegration.description import PluginParameter from cmem_plugin_base.dataintegration.parameter.choice import ChoiceParameterType from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType +from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin from cmem_plugin_base.dataintegration.types import IntParameterType from defusedxml import minidom @@ -105,3 +106,16 @@ def send_result(iri: str, filepath: Path) -> None: replace=True, content_type="text/turtle", ) + + +def remove_temp(plugin: WorkflowPlugin, files: list) -> None: + """Remove temproray files""" + for file in files: + try: + (Path(plugin.temp) / file).unlink() + except (OSError, FileNotFoundError) as err: + plugin.log.warning(f"Cannot remove file {file} ({err})") + try: + Path(plugin.temp).rmdir() + except (OSError, FileNotFoundError) as err: + plugin.log.warning(f"Cannot remove directory {plugin.temp} ({err})")