Skip to content

Commit

Permalink
Upgraded to rdflib>=6.0.0 and pyshacl==v0.17.2: (BlueBrain#201)
Browse files Browse the repository at this point in the history
* Upgraded to rdflib>=6.0.0 and pyshacl==v0.17.2:
* removed rdflib-jsonld dependancy which is now part of rdflib from rdflib>=6.0.0
* Added a ShapeWrapper class extending pyshacl.shape to workaround added __slots__ preventing from dynamically adding methods

* Removed python==3.6 support
  • Loading branch information
MFSY committed Nov 8, 2021
1 parent e53c3f9 commit 6ba1636
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: python

python:
- "3.6"
- "3.7"
- "3.8"

Expand Down
6 changes: 2 additions & 4 deletions kgforge/core/commons/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.

from typing import Optional, Union, Dict, List
from rdflib.plugins.shared.jsonld.context import source_to_json, Context as JSONLD_Context

from rdflib_jsonld.context import Context as RdflibContext
from rdflib_jsonld.util import source_to_json


class Context(RdflibContext):
class Context(JSONLD_Context):
"""Context class will hold a JSON-LD context in two forms: iri and document.
See also: https://w3c.github.io/json-ld-syntax/#the-context
Expand Down
6 changes: 3 additions & 3 deletions kgforge/core/conversions/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.

from copy import deepcopy

from rdflib.plugins.shared.jsonld.keys import CONTEXT, TYPE, GRAPH
from typing import Union, Dict, List, Tuple, Optional, Callable, Any
from urllib.error import URLError, HTTPError

Expand All @@ -21,9 +23,7 @@
from rdflib import Graph, Literal
import json
from collections import OrderedDict

from rdflib.namespace import RDF
from rdflib_jsonld.keys import CONTEXT, GRAPH, TYPE, ID

from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.context import Context
Expand Down Expand Up @@ -214,7 +214,7 @@ def _as_jsonld_one(resource: Resource, form: Form, store_metadata: bool,
context.expand(resource_type) for resource_type in resource_types]
resource.type = resource_types
if store_metadata is True and len(metadata_graph) > 0:
metadata_expanded = json.loads(metadata_graph.serialize(format="json-ld").decode("utf-8"))
metadata_expanded = json.loads(metadata_graph.serialize(format="json-ld"))
metadata_framed = jsonld.frame(metadata_expanded, {"@id": resource.id})
if form is Form.COMPACTED:
data_compacted = jsonld.compact(data_framed, resolved_context)
Expand Down
20 changes: 17 additions & 3 deletions kgforge/specializations/models/rdf/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.
import types
from abc import abstractmethod
from pyshacl.constraints import ALL_CONSTRAINT_PARAMETERS
from typing import List, Dict, Tuple, Set, Optional
from pyshacl.shape import Shape
from pyshacl.shapes_graph import ShapesGraph
Expand Down Expand Up @@ -89,6 +90,17 @@ def traverse(self, predecessors: Set[URIRef]) -> Tuple[List, Dict]:
return properties, attributes


class ShapeWrapper(Shape):
__slots__ = ('__dict__',)

def __init__(self, shape: Shape) -> None:
super().__init__(shape.sg, shape.node, shape._p, shape._path, shape.logger)

def parameters(self):
return (p for p, v in self.sg.predicate_objects(self.node)
if p in ALL_CONSTRAINT_PARAMETERS)


class ShapesGraphWrapper(ShapesGraph):

def __init__(self, graph: Graph) -> None:
Expand All @@ -105,10 +117,12 @@ def lookup_shape_from_node(self, node: URIRef) -> Shape:
Returns:
Shape: The Shacl shape of the requested node.
"""

shape = self._node_shape_cache[node]
if not hasattr(shape, "traverse"):
shape.traverse = types.MethodType(traverse, shape)
if shape:
shape_wrapper = ShapeWrapper(self._node_shape_cache[node])
if not hasattr(shape_wrapper, "traverse"):
shape_wrapper.traverse = types.MethodType(traverse, shape_wrapper)
return shape_wrapper
return shape


Expand Down
11 changes: 5 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
keywords="framework knowledge graph data science",
url="https://github.com/BlueBrain/nexus-forge",
packages=find_packages(),
python_requires=">=3.6",
python_requires=">=3.7",
setup_requires=[
"setuptools_scm",
],
Expand All @@ -46,13 +46,12 @@
"nexus-sdk",
"aiohttp",
"nest_asyncio",
"rdflib<6.0.0",
"rdflib>=6.0.0",
"pyLD",
"pyshacl==0.11.6.post1",
"rdflib-jsonld<0.6.1",
"pyshacl==v0.17.2",
"nest-asyncio>=1.5.1",
"pyparsing==2.*",
"owlrl>=5.2.1, < 6.0.2"
"pyparsing>=2.0.2",
"owlrl>=5.2.3"
],
extras_require={
"dev": ["tox", "pytest", "pytest-bdd==3.4.0", "pytest-cov", "pytest-mock", "codecov"],
Expand Down

0 comments on commit 6ba1636

Please sign in to comment.