Skip to content

Commit 328f5b5

Browse files
author
Ralf Grubenmann
committed
Cleanup and addresses PR comments
1 parent 86e0456 commit 328f5b5

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

renku/core/commands/checks/validate_shacl.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ def _shacl_graph_to_string(graph):
3737
res = graph.value(result, sh.resultMessage)
3838

3939
if res:
40-
message = '{}: {}'.format(path, res)
40+
message = '{0}: {1}'.format(path, res)
4141
else:
4242
kind = graph.value(result, sh.sourceConstraintComponent)
4343
focusNode = graph.value(result, sh.focusNode)
4444

4545
if isinstance(focusNode, BNode):
4646
focusNode = '<Anonymous>'
4747

48-
message = '{}: Type: {}, Node ID: {}'.format(path, kind, focusNode)
48+
message = '{0}: Type: {1}, Node ID: {2}'.format(
49+
path, kind, focusNode
50+
)
4951

5052
problems.append(message)
5153

@@ -61,9 +63,8 @@ def check_project_structure(client):
6163
if conform:
6264
return True, None
6365

64-
problems = (
65-
WARNING + 'Invalid structure of project metadata\n\t' +
66-
_shacl_graph_to_string(graph)
66+
problems = '{0}Invalid structure of project metadata\n\t{1}'.format(
67+
WARNING, _shacl_graph_to_string(graph)
6768
)
6869

6970
return False, problems
@@ -73,26 +74,28 @@ def check_datasets_structure(client):
7374
"""Validate dataset metadata against SHACL."""
7475
ok = True
7576

76-
problems = WARNING + 'Invalid structure of dataset metadata\n'
77+
problems = ['{0}Invalid structure of dataset metadata'.format(WARNING)]
7778

7879
for path in client.renku_datasets_path.rglob(client.METADATA):
7980
try:
8081
conform, graph, t = check_shacl_structure(path)
8182
except (Exception, BaseException) as e:
82-
problems += 'Couldn\'t validate {}: {}\n\n'.format(path, e)
83+
problems.append('Couldn\'t validate {0}: {1}\n\n'.format(path, e))
8384
continue
8485

8586
if conform:
8687
continue
8788

8889
ok = False
8990

90-
problems += str(path) + '\n\t' + _shacl_graph_to_string(graph) + '\n\n'
91+
problems.append(
92+
'{0}\n\t{1}\n'.format(path, _shacl_graph_to_string(graph))
93+
)
9194

9295
if ok:
9396
return True, None
9497

95-
return False, problems
98+
return False, '\n'.join(problems)
9699

97100

98101
def check_shacl_structure(path):

renku/core/commands/format/graph.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import click
2323

24+
from renku.core.errors import SHACLValidationError
2425
from renku.core.utils.shacl import validate_graph
2526

2627

@@ -30,7 +31,7 @@ def ascii(graph, strict=False):
3031
from ..echo import echo_via_pager
3132

3233
if strict:
33-
raise click.BadParameter('--strict not supported for json-ld-graph')
34+
raise SHACLValidationError('--strict not supported for json-ld-graph')
3435

3536
echo_via_pager(str(DAG(graph)))
3637

@@ -68,7 +69,7 @@ def dot(graph, simple=True, debug=False, landscape=False, strict=False):
6869
from rdflib.tools.rdf2dot import rdf2dot
6970

7071
if strict:
71-
raise click.BadParameter('--strict not supported for json-ld-graph')
72+
raise SHACLValidationError('--strict not supported for json-ld-graph')
7273

7374
g = _conjunctive_graph(graph)
7475

@@ -312,7 +313,7 @@ def makefile(graph, strict=False):
312313
from renku.core.models.provenance.activities import ProcessRun, WorkflowRun
313314

314315
if strict:
315-
raise click.BadParameter('--strict not supported for json-ld-graph')
316+
raise SHACLValidationError('--strict not supported for json-ld-graph')
316317

317318
for activity in graph.activities.values():
318319
if not isinstance(activity, ProcessRun):
@@ -341,7 +342,7 @@ def jsonld(graph, strict=False):
341342
r, _, t = validate_graph(ld, format='json-ld')
342343

343344
if not r:
344-
raise click.BadParameter(
345+
raise SHACLValidationError(
345346
"{}\nCouldn't get log: Invalid Knowledge Graph data".format(t)
346347
)
347348
click.echo(ld)
@@ -350,7 +351,7 @@ def jsonld(graph, strict=False):
350351
def jsonld_graph(graph, strict=False):
351352
"""Format graph as JSON-LD graph file."""
352353
if strict:
353-
raise click.BadParameter('--strict not supported for json-ld-graph')
354+
raise SHACLValidationError('--strict not supported for json-ld-graph')
354355
click.echo(_jsonld(graph, 'flatten'))
355356

356357

@@ -361,7 +362,7 @@ def nt(graph, strict=False):
361362
r, _, t = validate_graph(nt, format='nt')
362363

363364
if not r:
364-
raise click.BadParameter(
365+
raise SHACLValidationError(
365366
"{}\nCouldn't get log: Invalid Knowledge Graph data".format(t)
366367
)
367368

@@ -375,7 +376,7 @@ def rdf(graph, strict=False):
375376
r, _, t = validate_graph(xml, format='xml')
376377

377378
if not r:
378-
raise click.BadParameter(
379+
raise SHACLValidationError(
379380
"{}\nCouldn't get log: Invalid Knowledge Graph data".format(t)
380381
)
381382

renku/core/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,7 @@ class UrlSchemeNotSupported(RenkuException):
373373

374374
class OperationError(RenkuException):
375375
"""Raised when an operation at runtime raises an error."""
376+
377+
378+
class SHACLValidationError(RenkuException):
379+
"""Raises when SHACL validation of the graph fails."""

renku/core/utils/shacl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18-
"""JSON-LD SHACL validation."""
18+
"""JSON-LD SHACL validations."""
1919

2020
from pkg_resources import resource_string
2121
from pyshacl import validate
@@ -24,7 +24,7 @@
2424
def validate_graph(graph, shacl_path=None, format='nquads'):
2525
"""Validate the current graph with a SHACL schema.
2626
27-
uses default schema if not supplied.
27+
Uses default schema if not supplied.
2828
"""
2929
if shacl_path:
3030
with open(shacl_path, 'r', encoding='utf-8') as f:

0 commit comments

Comments
 (0)