21
21
22
22
import click
23
23
24
+ from renku .core .errors import SHACLValidationError
25
+ from renku .core .utils .shacl import validate_graph
24
26
25
- def ascii (graph ):
27
+
28
+ def ascii (graph , strict = False ):
26
29
"""Format graph as an ASCII art."""
27
30
from ..ascii import DAG
28
31
from ..echo import echo_via_pager
29
32
33
+ if strict :
34
+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
35
+
30
36
echo_via_pager (str (DAG (graph )))
31
37
32
38
33
39
def _jsonld (graph , format , * args , ** kwargs ):
34
40
"""Return formatted graph in JSON-LD ``format`` function."""
35
41
import json
36
42
37
- from pyld import jsonld
43
+ from renku . core . compat import pyld
38
44
from renku .core .models .jsonld import asjsonld
39
45
40
- output = getattr (jsonld , format )([
46
+ output = getattr (pyld . jsonld , format )([
41
47
asjsonld (action ) for action in graph .activities .values ()
42
48
])
43
49
return json .dumps (output , indent = 2 )
44
50
45
51
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``."""
50
54
from rdflib import ConjunctiveGraph
51
55
from rdflib .plugin import register , Parser
52
- from rdflib .tools .rdf2dot import rdf2dot
53
56
54
57
register ('json-ld' , Parser , 'rdflib_jsonld.parser' , 'JsonLDParser' )
55
58
56
- g = ConjunctiveGraph ().parse (
59
+ return ConjunctiveGraph ().parse (
57
60
data = _jsonld (graph , 'expand' ),
58
61
format = 'json-ld' ,
59
62
)
60
63
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
+
61
76
g .bind ('prov' , 'http://www.w3.org/ns/prov#' )
62
77
g .bind ('foaf' , 'http://xmlns.com/foaf/0.1/' )
63
78
g .bind ('wfdesc' , 'http://purl.org/wf4ever/wfdesc#' )
@@ -92,7 +107,7 @@ def _rdf2dot_simple(g, stream):
92
107
import re
93
108
94
109
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]+)/'
96
111
r'(?P<commit>\w+)'
97
112
r'(?P<path>.+)?'
98
113
)
@@ -293,10 +308,13 @@ def color(p):
293
308
stream .write ('}\n ' )
294
309
295
310
296
- def makefile (graph ):
311
+ def makefile (graph , strict = False ):
297
312
"""Format graph as Makefile."""
298
313
from renku .core .models .provenance .activities import ProcessRun , WorkflowRun
299
314
315
+ if strict :
316
+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
317
+
300
318
for activity in graph .activities .values ():
301
319
if not isinstance (activity , ProcessRun ):
302
320
continue
@@ -316,44 +334,53 @@ def makefile(graph):
316
334
)
317
335
318
336
319
- def jsonld (graph ):
337
+ def jsonld (graph , strict = False ):
320
338
"""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 )
322
349
323
350
324
- def jsonld_graph (graph ):
351
+ def jsonld_graph (graph , strict = False ):
325
352
"""Format graph as JSON-LD graph file."""
353
+ if strict :
354
+ raise SHACLValidationError ('--strict not supported for json-ld-graph' )
326
355
click .echo (_jsonld (graph , 'flatten' ))
327
356
328
357
329
- def nt (graph ):
358
+ def nt (graph , strict = False ):
330
359
"""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' )
333
363
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
+ )
335
368
336
- click .echo (
337
- ConjunctiveGraph ().parse (
338
- data = _jsonld (graph , 'expand' ),
339
- format = 'json-ld' ,
340
- ).serialize (format = 'nt' )
341
- )
369
+ click .echo (nt )
342
370
343
371
344
- def rdf (graph ):
372
+ def rdf (graph , strict = False ):
345
373
"""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' )
348
377
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
+ )
350
382
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 )
357
384
358
385
359
386
FORMATS = {
0 commit comments