Skip to content

Commit

Permalink
Merge pull request #11 from white-gecko/feature/improvements
Browse files Browse the repository at this point in the history
More improvements
  • Loading branch information
briesenberg07 authored Aug 8, 2023
2 parents a5d1d2b + 38befe4 commit 4bff384
Show file tree
Hide file tree
Showing 10 changed files with 792 additions and 357 deletions.
23 changes: 13 additions & 10 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ version: '3'
tasks:

install_dependencies:
cmds:
- pip install poetry
- pip install poetry

install:
cmds:
- poetry install
- poetry install

code_formating:
cmds:
- poetry run black shaclgen tests
- poetry run black shaclgen tests

code_formating_check:
cmds:
- poetry run black --check shaclgen tests
- poetry run black --check shaclgen tests

test:
cmds:
- poetry run pytest -rP --cov=shaclgen --cov-report=term-missing
- poetry run pytest -rP --cov=shaclgen --cov-report=term-missing

ci:
cmds:
- task: install_dependencies
- task: install
- task: test
- task: code_formating_check

# Aliases
#
# Should be new syntax onece v4 is out:
# https://github.com/go-task/task/issues/675

black:
- task: code_formating
565 changes: 298 additions & 267 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ classifiers = [
python = "^3.7"
rdflib = "^6.1.1"
click = "^8.1.2"
pytest = "^7.1.1"
pytest = "^7.4.0"
loguru = "^0.6.0"

[tool.poetry.dev-dependencies]
black = "^22.3.0"
pytest-cov = "^3.0.0"
pytest-cov = "^4.1.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
57 changes: 53 additions & 4 deletions shaclgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
from .schema import schema

import click
from sys import stdout
from sys import stdout, stderr
from rdflib import Graph
from rdflib.util import guess_format
from rdflib.namespace import NamespaceManager
from loguru import logger
import json
import pkg_resources


@click.command()
Expand Down Expand Up @@ -51,7 +55,33 @@
help="use implicit class targets with RDFS",
default=False,
)
def main(graphs, ontology, output, serial, prefixes, namespace, implicit_class_target):
@click.option(
"--logfile",
help="Specify a logfile",
default=None,
)
@click.option(
"--loglevel",
help="Specify the standard output loglevel",
default="INFO",
)
@click.option(
"--logfilelevel",
help="Specify the loglevel for the logfile",
default="DEBUG",
)
def main(
graphs,
ontology,
output,
serial,
prefixes,
namespace,
implicit_class_target,
logfile,
loglevel,
logfilelevel,
):
"""
---------------------------Shaclgen---------------------------
Expand Down Expand Up @@ -89,16 +119,35 @@ def main(graphs, ontology, output, serial, prefixes, namespace, implicit_class_t
"""

logger.remove()
logger.add(stderr, level=loglevel)
if logfile:
logger.add(logfile, level=logfilelevel)

source_graph = Graph()

for graph in graphs:
source_graph.parse(graph, format=guess_format(graph))

path = "prefixes/namespaces.json"
filepath = pkg_resources.resource_filename(__name__, path)

namespaces = NamespaceManager(graph=Graph())

with open(filepath, "r", encoding="utf-8") as fin:
for prefix, namespace_iri in json.load(fin).items():
namespaces.bind(prefix, namespace_iri)

if prefixes:
with open(prefixes, "r", encoding="utf-8") as fin:
for prefix, namespace_iri in json.load(fin).items():
namespaces.bind(prefix, namespace_iri)

g = None
if ontology:
g = schema(source_graph, prefixes)
g = schema(source_graph, namespaces)
else:
g = data_graph(source_graph, prefixes)
g = data_graph(source_graph, namespaces)
shape_graph = g.gen_graph(
namespace=namespace, implicit_class_target=implicit_class_target
)
Expand Down
6 changes: 0 additions & 6 deletions shaclgen/constraint.py

This file was deleted.

2 changes: 1 addition & 1 deletion shaclgen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class Generator:
def __init__(self, graph: Graph, prefixes=None):
def __init__(self, graph: Graph, namespaces=None):
pass

def sh_label_gen(self, uri):
Expand Down
26 changes: 10 additions & 16 deletions shaclgen/schema.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from loguru import logger
from rdflib import Graph, Namespace
from rdflib.namespace import XSD, RDF, RDFS, OWL, SH
from rdflib.namespace import NamespaceManager
from rdflib.term import URIRef, Literal, BNode
import collections
import json
from rdflib.collection import Collection
import pkg_resources
from .generator import Generator

"""
Expand All @@ -17,7 +16,7 @@


class schema(Generator):
def __init__(self, graph: Graph, prefixes=None):
def __init__(self, graph: Graph, namespaces=None):
self.G = graph

self.CLASSES = collections.OrderedDict()
Expand All @@ -39,21 +38,12 @@ def __init__(self, graph: Graph, prefixes=None):
XSD.nonPositiveInteger,
]

path = "prefixes/namespaces.json"
filepath = pkg_resources.resource_filename(__name__, path)

self.namespaces = NamespaceManager(graph=Graph())
if namespaces:
self.namespaces = namespaces
else:
self.namespaces = NamespaceManager(graph=Graph())
self.namespaces.bind("sh", SH)

with open(filepath, "r", encoding="utf-8") as fin:
for prefix, namespace in json.load(fin).items():
self.namespaces.bind(prefix, namespace)

if prefixes:
with open(prefixes, "r", encoding="utf-8") as fin:
for prefix, namespace in json.load(fin).items():
self.namespaces.bind(prefix, namespace)

def extract_props(self):
properties = []
self.PROPS = {}
Expand Down Expand Up @@ -207,9 +197,13 @@ def extract_restrictions(self):
self.REST[rest]["value"] = rest_val[0]

def gen_graph(self, namespace=None, implicit_class_target=False):
logger.info("Start Extraction of the Ontology")
logger.info("Properties …")
self.extract_props()
logger.info("Classes …")
self.extract_classes()
self.extract_restrictions()
logger.info("Write resulting SHACL Graph …")
ng = Graph(namespace_manager=self.namespaces)

if namespace is not None:
Expand Down
Loading

0 comments on commit 4bff384

Please sign in to comment.