Skip to content

Commit

Permalink
use tempfile module
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jul 13, 2024
1 parent a4b85e3 commit 42ab240
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
47 changes: 22 additions & 25 deletions cmem_plugin_pyshacl/plugin_pyshacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -267,7 +266,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001
name="remove_dataset_graph_type",
label="Remove graph type <http://rdfs.org/ns/void#Dataset> from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<http://rdfs.org/ns/void#Dataset> from the loaded data graph.",
"<http://rdfs.org/ns/void#Dataset> from the in-memory data graph.",
default_value=False,
advanced=True,
),
Expand All @@ -277,7 +276,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001
label="Remove graph type <https://vocab.eccenca.com/dsm/ThesaurusProject> "
"from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<https://vocab.eccenca.com/dsm/ThesaurusProject> from the loaded data "
"<https://vocab.eccenca.com/dsm/ThesaurusProject> from the in-memory data "
"graph.",
default_value=False,
advanced=True,
Expand All @@ -288,7 +287,7 @@ def langfilter(lbl: Literal) -> bool: # noqa: ARG001
label="Remove graph type <https://vocab.eccenca.com/shui/ShapeCatalog> "
"from data graph",
description="Before validating, remove the triple <data_graph_uri> a "
"<https://vocab.eccenca.com/shui/ShapeCatalog> from the loaded data "
"<https://vocab.eccenca.com/shui/ShapeCatalog> from the in-memory data "
"graph.",
default_value=False,
advanced=True,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 42ab240

Please sign in to comment.