Skip to content

Commit

Permalink
use temp file when oarsing graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Dec 6, 2024
1 parent 9e822df commit 29d5949
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### Changed

- Load graphs with get_streamed instead of get
- Defined output port
- Update execution report
- Reduce memory usage when parsing graphs from CMEM by creating temporary file

## [5.1.0] 2024-12-03

Expand Down
15 changes: 10 additions & 5 deletions cmem_plugin_pyshacl/plugin_pyshacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def post_graph(self, validation_graph: Graph) -> None:
self.log.info("Posting SHACL validation graph...")
with NamedTemporaryFile(suffix=".nt") as temp:
validation_graph.serialize(temp.name, format="nt", encoding="utf-8")
setup_cmempy_user_access(self.context.user)
res = post_streamed(
self.validation_graph_uri,
temp.name,
Expand Down Expand Up @@ -570,9 +571,12 @@ def make_entities(
def get_graph(self, uri: str) -> Graph:
"""Get graph from cmem"""
graph = Graph()
graph.parse(
data=get_streamed(uri, owl_imports_resolution=self.owl_imports).text, format="turtle"
)
setup_cmempy_user_access(self.context.user)
with NamedTemporaryFile(suffix=".nt") as temp:
temp.write(
get_streamed(uri, owl_imports_resolution=self.owl_imports).text.encode("utf-8")
)
graph.parse(temp.name, format="nt")
return graph

def check_parameters( # noqa: C901 PLR0912
Expand All @@ -588,6 +592,7 @@ def check_parameters( # noqa: C901 PLR0912
raise ValueError("Data graph URI parameter is invalid")
if not validators.url(self.shacl_graph_uri):
raise ValueError("SHACL graph URI parameter is invalid")
setup_cmempy_user_access(self.context.user)
graphs_dict = {graph["iri"]: graph["assignedClasses"] for graph in get_graphs_list()}

if self.ontology_graph_uri:
Expand Down Expand Up @@ -634,7 +639,7 @@ def execute( # noqa: C901 PLR0915
) -> Entities | None:
"""Execute plugin"""
self.context = context
setup_cmempy_user_access(context.user)

self.check_parameters()
self.update_report("validate", "graphs validated.", 0)

Expand All @@ -659,6 +664,7 @@ def execute( # noqa: C901 PLR0915

if self.ontology_graph_uri:
self.log.info(f"Loading ontology graph <{self.ontology_graph_uri}> into memory...")
start = time()
ontology_graph = self.get_graph(self.ontology_graph_uri)
self.log.info(f"Finished loading ontology graph in {e_t(start)} seconds")
self.update_report("load", "graphs loaded", 3)
Expand Down Expand Up @@ -698,7 +704,6 @@ def execute( # noqa: C901 PLR0915
validation_graph, validation_graph_uris, focus_nodes
)
validation_graph = self.add_prov(validation_graph, utctime)

self.post_graph(validation_graph)

self.update_report("validate", "graph validated.", 1)
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

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

0 comments on commit 29d5949

Please sign in to comment.