Commit 8e9a7e78 authored by Simon Chabot's avatar Simon Chabot
Browse files

[rdf] add functions and tools to generate rdf graph

parent 0cd5b9057202
......@@ -23,7 +23,7 @@ from cubicweb import _
from itertools import chain
from logilab.mtconverter import TransformError
from logilab.common.decorators import cached
from logilab.common.decorators import cached, cachedproperty
from cubicweb.entity import EntityAdapter
from cubicweb import (Unauthorized, ValidationError, ViolatedConstraint,
......@@ -32,6 +32,25 @@ from cubicweb.schema import constraint_name_for
from cubicweb.predicates import is_instance, relation_possible, match_exception
class EntityRDFAdapter(EntityAdapter):
"""EntityRDFAdapter is to be specialized for each entity that wants to
be converted to RDF using the mechanism from cubicweb.rdf
__abstract__ = True
def __init__(self, _cw, **kwargs):
super().__init__(_cw, **kwargs)
def uri(self):
return self.entity.cwuri
def triples(self):
"""return sequence of 3-tuple of rdflib identifiers"""
raise NotImplementedError()
class IDublinCoreAdapter(EntityAdapter):
__regid__ = 'IDublinCore'
__select__ = is_instance('Any')
# -*- coding: utf-8 -*-
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <>.
from rdflib import Namespace, ConjunctiveGraph, plugin
import rdflib_jsonld # noqa
plugin.register("jsonld", plugin.Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer")
'application/rdf+xml': 'xml',
'text/turtle': 'turtle',
'text/n3': 'n3',
'application/n-quads': 'nquads',
'application/n-triples': 'nt',
'application/trig': 'trig',
'application/ld+json': 'json-ld',
namespaces = {
"rdf": "",
"schema": "",
NS_VARS = {ns: Namespace(uri) for ns, uri in namespaces.items()}
# dict: name of CWEType -> list of regid of adapters derived from EntityRDFAdapter
def conjunctive_graph():
"""factory to build a ``ConjunctiveGraph`` and bind all namespaces
graph = ConjunctiveGraph()
for vocab, rdfns in NS_VARS.items():
graph.bind(vocab, rdfns)
return graph
def iter_rdf_adapters(entity):
for adapter_id in ETYPES_ADAPTERS.get(entity.__regid__, ()):
adapter = entity.cw_adapt_to(adapter_id)
if adapter:
yield adapter
def add_entity_to_graph(graph, entity):
for adapter in iter_rdf_adapters(entity):
for triple in adapter.triples():
......@@ -73,6 +73,8 @@ setup(
'console_scripts': [
......@@ -102,10 +104,6 @@ setup(
'rdf': [
'sparql': [
'fyzz >= 0.1.0',
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment