# HG changeset patch # User Simon Chabot # Date 1581700555 -3600 # Node ID eb0cd60600623a70aa74b0d243fe243ecb2c55a0 # Parent 0cd5b90572026996d0045fd5a9a962e106323ddd [rdf] add functions and tools to generate rdf graph diff -r 0cd5b9057202 -r eb0cd6060062 cubicweb/entities/adapters.py --- a/cubicweb/entities/adapters.py Wed Feb 12 13:58:17 2020 +0100 +++ b/cubicweb/entities/adapters.py Fri Feb 14 18:15:55 2020 +0100 @@ -23,7 +23,7 @@ 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.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) + self.entity.complete() + + @cachedproperty + 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') diff -r 0cd5b9057202 -r eb0cd6060062 cubicweb/rdf.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cubicweb/rdf.py Fri Feb 14 18:15:55 2020 +0100 @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# contact http://www.logilab.fr -- mailto:contact@logilab.fr +# +# 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") + +RDF_MIMETYPE_TO_FORMAT = { + '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": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", + "schema": "http://schema.org/", +} + +NS_VARS = {ns: Namespace(uri) for ns, uri in namespaces.items()} + + +# dict: name of CWEType -> list of regid of adapters derived from EntityRDFAdapter +ETYPES_ADAPTERS = { +} + + +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(): + graph.add(triple) diff -r 0cd5b9057202 -r eb0cd6060062 setup.py --- a/setup.py Wed Feb 12 13:58:17 2020 +0100 +++ b/setup.py Fri Feb 14 18:15:55 2020 +0100 @@ -73,6 +73,8 @@ 'pytz', 'Markdown', 'filelock', + 'rdflib', + 'rdflib-jsonld', ], entry_points={ 'console_scripts': [ @@ -102,10 +104,6 @@ 'pyramid_multiauth', 'repoze.lru', ], - 'rdf': [ - 'rdflib', - 'rdflib-jsonld', - ], 'sparql': [ 'fyzz >= 0.1.0', ],