From 42ab24050ca17a4d5f2dff7611e0cc86faafe673 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 13 Jul 2024 21:56:48 +0100 Subject: [PATCH] use tempfile module --- cmem_plugin_pyshacl/plugin_pyshacl.py | 47 +++++++++++++-------------- poetry.lock | 12 +------ pyproject.toml | 1 - 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/cmem_plugin_pyshacl/plugin_pyshacl.py b/cmem_plugin_pyshacl/plugin_pyshacl.py index 5892aae..b271a0e 100644 --- a/cmem_plugin_pyshacl/plugin_pyshacl.py +++ b/cmem_plugin_pyshacl/plugin_pyshacl.py @@ -4,8 +4,9 @@ from datetime import UTC, datetime from os import environ from pathlib import Path +from secrets import token_hex +from tempfile import TemporaryDirectory from time import time -from uuid import uuid4 import validators.url from cmem.cmempy.dp.proxy.graph import get, post_streamed @@ -90,12 +91,10 @@ def preferred_label( def langfilter(lbl: Literal) -> bool: return lbl.language is None - else: def langfilter(lbl: Literal) -> bool: return lbl.language == lang - else: # we don't care about language tags def langfilter(lbl: Literal) -> bool: # noqa: ARG001 @@ -267,7 +266,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001 name="remove_dataset_graph_type", label="Remove graph type from data graph", description="Before validating, remove the triple a " - " from the loaded data graph.", + " from the in-memory data graph.", default_value=False, advanced=True, ), @@ -277,7 +276,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001 label="Remove graph type " "from data graph", description="Before validating, remove the triple a " - " from the loaded data " + " from the in-memory data " "graph.", default_value=False, advanced=True, @@ -288,7 +287,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001 label="Remove graph type " "from data graph", description="Before validating, remove the triple a " - " from the loaded data " + " from the in-memory data " "graph.", default_value=False, advanced=True, @@ -444,23 +443,19 @@ def add_shui_conforms_val( def post_graph(self, validation_graph: Graph) -> None: """Post validation graph to cmem""" self.log.info("Posting SHACL validation graph...") - temp_file = Path(f"{uuid4()}.nt") - validation_graph.serialize(temp_file, format="nt", encoding="utf-8") - self.log.info( - f"Created temporary file {temp_file} with size " f"{temp_file.stat().st_size} bytes" - ) - res = post_streamed( - self.validation_graph_uri, - temp_file, - replace=self.clear_validation_graph, - content_type="application/n-triples", - ) - Path.unlink(temp_file) - self.log.info("Deleted temporary file") - if res.status_code == 204: # noqa: PLR2004 - self.log.info("Successfully posted SHACL validation graph") - else: - self.log.info("Error posting SHACL validation graph: " f"status code {res.status_code}") + with TemporaryDirectory() as temp: + temp_file = Path(temp) / f"{token_hex(8)}.nt" + validation_graph.serialize(temp_file, format="nt", encoding="utf-8") + res = post_streamed( + self.validation_graph_uri, + temp_file, + replace=self.clear_validation_graph, + content_type="application/n-triples", + ) + if res.status_code != 204: # noqa: PLR2004 + self.log.info( + "Error posting SHACL validation graph: " f"status code {res.status_code}" + ) def check_object( # noqa: C901 PLR0912 PLR0913 self, graph: Graph, subj: Node, pred: URIRef, data_graph: Graph, shacl_graph: Graph @@ -556,7 +551,7 @@ def check_parameters( # noqa: C901 PLR0912 self.log.info("Validating parameters...") if not self.output_entities and not self.generate_graph: raise ValueError( - "Generate validation graph or Output values parameter " "needs to be set to true" + "Generate validation graph or Output values parameter needs to be set to true" ) if not validators.url(self.data_graph_uri): raise ValueError("Data graph URI parameter is invalid") @@ -635,7 +630,7 @@ def execute( # noqa: C901 self.log.info("Starting SHACL validation...") start = time() _conforms, validation_graph, _results_text = validate( - data_graph, + data_graph=data_graph, shacl_graph=shacl_graph, ont_graph=ontology_graph, meta_shacl=self.meta_shacl, @@ -664,7 +659,9 @@ def execute( # noqa: C901 validation_graph, validation_graph_uris, focus_nodes ) validation_graph = self.add_prov(validation_graph, utctime) + self.post_graph(validation_graph) + if self.output_entities: self.log.info("Outputting entities") return entities diff --git a/poetry.lock b/poetry.lock index a46bb6c..d3e8e12 100644 --- a/poetry.lock +++ b/poetry.lock @@ -243,16 +243,6 @@ files = [ {file = "defusedxml-0.7.1.tar.gz", hash = "sha256:1bb3032db185915b62d7c6209c5a8792be6a32ab2fedacc84e01b52c51aa3e69"}, ] -[[package]] -name = "distutils-strtobool" -version = "0.1.0" -description = "distutils.utils.strtobool" -optional = false -python-versions = "*" -files = [ - {file = "distutils-strtobool-0.1.0.tar.gz", hash = "sha256:92f189acca35ec50ca462acd70904ee8c1a81422ef3658fa2fec2b5c7068d662"}, -] - [[package]] name = "dparse" version = "0.6.3" @@ -1344,4 +1334,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "04a2b3d8039ff36f871dca5089b79d391db0c6a9db725522d98d790b3c3eeb82" +content-hash = "61520cb20a5128dde9d277bdd8c23089716e7a626d3783b7af58aba7f206733f" diff --git a/pyproject.toml b/pyproject.toml index 7cddd32..74ec2a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,6 @@ python = "^3.11" pyshacl = "^0.26.0" validators = "^0.32.0" rdflib = "^6.3.2" -distutils-strtobool = "^0.1.0" ruamel-yaml = "^0.18.6" [tool.poetry.dependencies.cmem-plugin-base]