2121
2222import click
2323
24+ from renku .core .errors import SHACLValidationError
25+ from renku .core .utils .shacl import validate_graph
2426
25- def ascii (graph ):
27+
28+ def ascii (graph , strict = False ):
2629 """Format graph as an ASCII art."""
2730 from ..ascii import DAG
2831 from ..echo import echo_via_pager
2932
33+ if strict :
34+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
35+
3036 echo_via_pager (str (DAG (graph )))
3137
3238
3339def _jsonld (graph , format , * args , ** kwargs ):
3440 """Return formatted graph in JSON-LD ``format`` function."""
3541 import json
3642
37- from pyld import jsonld
43+ from renku . core . compat import pyld
3844 from renku .core .models .jsonld import asjsonld
3945
40- output = getattr (jsonld , format )([
46+ output = getattr (pyld . jsonld , format )([
4147 asjsonld (action ) for action in graph .activities .values ()
4248 ])
4349 return json .dumps (output , indent = 2 )
4450
4551
46- def dot (graph , simple = True , debug = False , landscape = False ):
47- """Format graph as a dot file."""
48- import sys
49-
52+ def _conjunctive_graph (graph ):
53+ """Convert a renku ``Graph`` to an rdflib ``ConjunctiveGraph``."""
5054 from rdflib import ConjunctiveGraph
5155 from rdflib .plugin import register , Parser
52- from rdflib .tools .rdf2dot import rdf2dot
5356
5457 register ('json-ld' , Parser , 'rdflib_jsonld.parser' , 'JsonLDParser' )
5558
56- g = ConjunctiveGraph ().parse (
59+ return ConjunctiveGraph ().parse (
5760 data = _jsonld (graph , 'expand' ),
5861 format = 'json-ld' ,
5962 )
6063
64+
65+ def dot (graph , simple = True , debug = False , landscape = False , strict = False ):
66+ """Format graph as a dot file."""
67+ import sys
68+
69+ from rdflib .tools .rdf2dot import rdf2dot
70+
71+ if strict :
72+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
73+
74+ g = _conjunctive_graph (graph )
75+
6176 g .bind ('prov' , 'http://www.w3.org/ns/prov#' )
6277 g .bind ('foaf' , 'http://xmlns.com/foaf/0.1/' )
6378 g .bind ('wfdesc' , 'http://purl.org/wf4ever/wfdesc#' )
@@ -92,7 +107,7 @@ def _rdf2dot_simple(g, stream):
92107 import re
93108
94109 path_re = re .compile (
95- r'file:/// (?P<type>[a-zA-Z]+)/'
110+ r'(?P<prefix> file://|https://\w+/\w+/){0,1} (?P<type>[a-zA-Z]+)/'
96111 r'(?P<commit>\w+)'
97112 r'(?P<path>.+)?'
98113 )
@@ -293,10 +308,13 @@ def color(p):
293308 stream .write ('}\n ' )
294309
295310
296- def makefile (graph ):
311+ def makefile (graph , strict = False ):
297312 """Format graph as Makefile."""
298313 from renku .core .models .provenance .activities import ProcessRun , WorkflowRun
299314
315+ if strict :
316+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
317+
300318 for activity in graph .activities .values ():
301319 if not isinstance (activity , ProcessRun ):
302320 continue
@@ -316,44 +334,53 @@ def makefile(graph):
316334 )
317335
318336
319- def jsonld (graph ):
337+ def jsonld (graph , strict = False ):
320338 """Format graph as JSON-LD file."""
321- click .echo (_jsonld (graph , 'expand' ))
339+ ld = _jsonld (graph , 'expand' )
340+
341+ if strict :
342+ r , _ , t = validate_graph (ld , format = 'json-ld' )
343+
344+ if not r :
345+ raise SHACLValidationError (
346+ "{}\n Couldn't get log: Invalid Knowledge Graph data" .format (t )
347+ )
348+ click .echo (ld )
322349
323350
324- def jsonld_graph (graph ):
351+ def jsonld_graph (graph , strict = False ):
325352 """Format graph as JSON-LD graph file."""
353+ if strict :
354+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
326355 click .echo (_jsonld (graph , 'flatten' ))
327356
328357
329- def nt (graph ):
358+ def nt (graph , strict = False ):
330359 """Format graph as n-tuples."""
331- from rdflib import ConjunctiveGraph
332- from rdflib .plugin import register , Parser
360+ nt = _conjunctive_graph (graph ).serialize (format = 'nt' )
361+ if strict :
362+ r , _ , t = validate_graph (nt , format = 'nt' )
333363
334- register ('json-ld' , Parser , 'rdflib_jsonld.parser' , 'JsonLDParser' )
364+ if not r :
365+ raise SHACLValidationError (
366+ "{}\n Couldn't get log: Invalid Knowledge Graph data" .format (t )
367+ )
335368
336- click .echo (
337- ConjunctiveGraph ().parse (
338- data = _jsonld (graph , 'expand' ),
339- format = 'json-ld' ,
340- ).serialize (format = 'nt' )
341- )
369+ click .echo (nt )
342370
343371
344- def rdf (graph ):
372+ def rdf (graph , strict = False ):
345373 """Output the graph as RDF."""
346- from rdflib import ConjunctiveGraph
347- from rdflib .plugin import register , Parser
374+ xml = _conjunctive_graph (graph ).serialize (format = 'application/rdf+xml' )
375+ if strict :
376+ r , _ , t = validate_graph (xml , format = 'xml' )
348377
349- register ('json-ld' , Parser , 'rdflib_jsonld.parser' , 'JsonLDParser' )
378+ if not r :
379+ raise SHACLValidationError (
380+ "{}\n Couldn't get log: Invalid Knowledge Graph data" .format (t )
381+ )
350382
351- click .echo (
352- ConjunctiveGraph ().parse (
353- data = _jsonld (graph , 'expand' ),
354- format = 'json-ld' ,
355- ).serialize (format = 'application/rdf+xml' )
356- )
383+ click .echo (xml )
357384
358385
359386FORMATS = {
0 commit comments