Skip to content

Commit

Permalink
add cl arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiskeely committed Nov 12, 2019
1 parent 4cc3d85 commit 6de5593
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 38 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
requirements = reqs.read()

setup(
name = 'shaclgen_test',
version = '0.1.1',
name = 'shaclgen',
version = '0.1.2',
packages = ['shaclgen'],
description='Shacl graph generator',
long_description=l_description,
author='Alexis Keely',
url='https://github.com/alexiskeely/shaclgen',
author_email='[email protected]',
install_requires=requirements,

keywords=['Linked Data', 'Semantic Web', 'Python',
'SHACL', 'Shapes', 'Schema', 'Validate'],
license='LICENSE.txt',
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
'Programming Language :: Python :: 3',

],
Expand Down
33 changes: 27 additions & 6 deletions shaclgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@
#!/usr/bin/env python

#%%
import sys
from .shaclgen import generate_groups, generate_triples, generate_shacl
import argparse

parser = argparse.ArgumentParser(description="""
Shacl file generator.
Shaclgen will create a simple shape file by default:
every class and property will get their own shape.
Nested and extended shape files are possible.""")

parser.add_argument("graph", type=str, help="the data graph")
parser.add_argument("serialization", type=str, help="the data graph rdf serialization")
group = parser.add_mutually_exclusive_group()
group.add_argument("-nf", "--nested", action="store_true", help='Property shapes will be nested in nodeshapes iif they occur with one class.')
group.add_argument("-ef", "--extended", action="store_true", help='Expands nested shapes to create individual property shapes for each property, in addition to nesting them when appropriate.')

args = parser.parse_args()



def main():
input_URI = sys.argv[1:][0]
serialization = sys.argv[1:][1]
output = generate_groups(input_URI, serialization)
triples = generate_triples(output)
output = generate_groups(args.graph, args.serialization)
if args.nested:
triples = generate_triples(output, 'nf')
elif args.extended:
triples = generate_triples(output, 'ef')
else:
triples = generate_triples(output, 'sf')

graph = generate_shacl(triples)
print( graph)
print('test environment')
print(graph)

if __name__ == '__main__':
main()
Expand Down
107 changes: 79 additions & 28 deletions shaclgen/shaclgen.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#!/usr/bin/env python

from rdflib import Graph, Namespace, XSD, RDF, URIRef
import requests
import rdflib
import sys
from collections import Counter, defaultdict


from collections import Counter


def generate_groups(input_URI, serialization):
Expand All @@ -32,37 +28,92 @@ def generate_groups(input_URI, serialization):
return output

#generate the triples
def generate_triples(output):
def generate_triples(output, graph_format):
triples = ''
counter = 0
for statement in output:
counter = counter + 1
if '#' in statement[0]:
name = statement[0].split("#")[-1]
else:
name = statement[0].split("/")[-1]
#gen node shape
node_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#NodeShape> .
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#targetClass> <{statement[0]}> .""")
if statement[2] == 'unique':
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
if graph_format == 'nf':
for statement in output:
counter = counter + 1
if '#' in statement[0]:
name = statement[0].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> _:{counter} .
_:{counter} <http://www.w3.org/ns/shacl#path> <{statement[1]}> . """)
else:
name = statement[0].split("/")[-1]
#gen node shape
node_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#NodeShape> .
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#targetClass> <{statement[0]}> .""")
if statement[2] == 'unique':
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> _:{counter} .
_:{counter} <http://www.w3.org/ns/shacl#path> <{statement[1]}> .
_:{counter} <http://www.w3.org/ns/shacl#name> "{prop_name}" . """)
else:
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> <http://example.org/{prop_name}Shape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#PropertyShape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/ns/shacl#path> <{statement[1]}> .""")
triples = triples + node_triples + prop_triples
elif graph_format =='ef':
for statement in output:
counter = counter + 1
if '#' in statement[0]:
name = statement[0].split("#")[-1]
else:
name = statement[0].split("/")[-1]
#gen node shape
node_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#NodeShape> .
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#targetClass> <{statement[0]}> .""")
if statement[2] == 'unique':
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> _:{counter} .
_:{counter} <http://www.w3.org/ns/shacl#path> <{statement[1]}> .
_:{counter} <http://www.w3.org/ns/shacl#name> "{prop_name}" .
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> <http://example.org/{prop_name}Shape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#PropertyShape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/ns/shacl#path> <{statement[1]}> .""")
else:
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> <http://example.org/{prop_name}Shape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#PropertyShape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/ns/shacl#path> <{statement[1]}> .""")
triples = triples + node_triples + prop_triples
else:
for statement in output:
counter = counter + 1
if '#' in statement[0]:
name = statement[0].split("#")[-1]
else:
name = statement[0].split("/")[-1]
#gen node shape
node_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#NodeShape> .
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#targetClass> <{statement[0]}> .""")
if '#' in statement[1]:
prop_name = statement[1].split("#")[-1]
else:
prop_name = statement[1].split("/")[-1]
prop_triples = (f"""
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> <http://example.org/{prop_name}Shape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#PropertyShape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/ns/shacl#path> <{statement[1]}> .""")
triples = triples + node_triples + prop_triples
<http://example.org/{name}Shape> <http://www.w3.org/ns/shacl#property> <http://example.org/{prop_name}Shape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#PropertyShape> .
<http://example.org/{prop_name}Shape> <http://www.w3.org/ns/shacl#path> <{statement[1]}> .""")
triples = triples + node_triples + prop_triples
return triples


Expand Down

0 comments on commit 6de5593

Please sign in to comment.