Skip to content

Commit

Permalink
remove temp files on error
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jul 8, 2024
1 parent fac6994 commit 719eac7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 10 additions & 1 deletion cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
if not Path(self.temp).exists():
Path(self.temp).mkdir(parents=True)
for graph in graphs:
self.log.info(f"Fetching graph {graph}.")
with (Path(self.temp) / graphs[graph]).open("w", encoding="utf-8") as file:
setup_cmempy_user_access(context.user)
file.write(get(graph).text)
Expand Down Expand Up @@ -298,7 +299,7 @@ def reason(self, graphs: dict) -> None:
raise OSError(response.stderr.decode())
raise OSError("ROBOT error")

def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002
def _execute(self, context: ExecutionContext) -> None:
"""`Execute plugin"""
setup_cmempy_user_access(context.user)
graphs = get_graphs_tree((self.data_graph_iri, self.ontology_graph_iri))
Expand All @@ -311,3 +312,11 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: AR
post_profiles(self, validate_profiles(self, graphs))
post_provenance(self, get_provenance(self, context))
remove_temp(self)

def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002
"""Remove temp files on error"""
try:
self._execute(context)
except Exception as exc:
remove_temp(self)
raise type(exc)(exc) from exc
16 changes: 13 additions & 3 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
if not Path(self.temp).exists():
Path(self.temp).mkdir(parents=True)
for graph in graphs:
self.log.info(f"Fetching graph {graph}.")
with (Path(self.temp) / graphs[graph]).open("w", encoding="utf-8") as file:
setup_cmempy_user_access(context.user)
file.write(get(graph).text)
Expand Down Expand Up @@ -211,7 +212,7 @@ def make_entities(self, text: str, valid_profiles: list) -> Entities:
)
return Entities(entities=entities, schema=schema)

def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None: # noqa: ARG002
def _execute(self, context: ExecutionContext) -> Entities:
"""Run the workflow operator."""
setup_cmempy_user_access(context.user)
graphs = get_graphs_tree((self.ontology_graph_iri,))
Expand All @@ -235,9 +236,18 @@ def execute(self, inputs: tuple, context: ExecutionContext) -> Entities | None:

text = (Path(self.temp) / self.md_filename).read_text()

remove_temp(self)

if self.stop_at_inconsistencies and text != "No explanations found.":
raise RuntimeError("Inconsistencies found in Ontology.")

return self.make_entities(text, valid_profiles)

def execute(self, inputs: tuple, context: ExecutionContext) -> Entities: # noqa: ARG002
"""Remove temp files on error"""
try:
output = self._execute(context)
remove_temp(self)
except Exception as exc:
remove_temp(self)
raise type(exc)(exc) from exc
else:
return output
1 change: 1 addition & 0 deletions cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def validate_profiles(plugin: WorkflowPlugin, graphs: dict) -> list:
ontology_location = f"{plugin.temp}/{graphs[plugin.ontology_graph_iri]}"
valid_profiles = []
for profile in ("Full", "DL", "EL", "QL", "RL"):
plugin.log.info(f"Validating {profile} profile.")
cmd = f"validate-profile --profile {profile} --input {ontology_location}"
response = robot(cmd, plugin.max_ram_percentage)
if response.stdout.endswith(b"[Ontology and imports closure in profile]\n\n"):
Expand Down

0 comments on commit 719eac7

Please sign in to comment.