From 6de55932f94992af5500000e8812e656e229cfe5 Mon Sep 17 00:00:00 2001 From: alexis mcclimans Date: Tue, 12 Nov 2019 11:21:39 -0800 Subject: [PATCH] add cl arg parsing --- setup.py | 8 ++-- shaclgen/__main__.py | 33 ++++++++++--- shaclgen/shaclgen.py | 107 ++++++++++++++++++++++++++++++++----------- 3 files changed, 110 insertions(+), 38 deletions(-) diff --git a/setup.py b/setup.py index d73290c..b73eda1 100644 --- a/setup.py +++ b/setup.py @@ -9,8 +9,8 @@ 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, @@ -18,11 +18,11 @@ url='https://github.com/alexiskeely/shaclgen', author_email='alexiskm@uw.com', 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', ], diff --git a/shaclgen/__main__.py b/shaclgen/__main__.py index 1d17a4e..6ce0ad2 100644 --- a/shaclgen/__main__.py +++ b/shaclgen/__main__.py @@ -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() diff --git a/shaclgen/shaclgen.py b/shaclgen/shaclgen.py index 5764d9d..288b011 100644 --- a/shaclgen/shaclgen.py +++ b/shaclgen/shaclgen.py @@ -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): @@ -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""" - . - <{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""" - _:{counter} . - _:{counter} <{statement[1]}> . """) - else: + name = statement[0].split("/")[-1] + #gen node shape + node_triples = (f""" + . + <{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""" + _:{counter} . + _:{counter} <{statement[1]}> . + _:{counter} "{prop_name}" . """) + else: + if '#' in statement[1]: + prop_name = statement[1].split("#")[-1] + else: + prop_name = statement[1].split("/")[-1] + prop_triples = (f""" + . + . + <{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""" + . + <{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""" + _:{counter} . + _:{counter} <{statement[1]}> . + _:{counter} "{prop_name}" . + . + . + <{statement[1]}> .""") + else: + if '#' in statement[1]: + prop_name = statement[1].split("#")[-1] + else: + prop_name = statement[1].split("/")[-1] + prop_triples = (f""" + . + . + <{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""" + . + <{statement[0]}> .""") if '#' in statement[1]: prop_name = statement[1].split("#")[-1] else: prop_name = statement[1].split("/")[-1] prop_triples = (f""" - . - . - <{statement[1]}> .""") - triples = triples + node_triples + prop_triples + . + . + <{statement[1]}> .""") + triples = triples + node_triples + prop_triples return triples