cubicweb/rdf.py
changeset 12891 eb0cd6060062
child 12892 0df0db725f07
equal deleted inserted replaced
12890:0cd5b9057202 12891:eb0cd6060062
       
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This program is free software: you can redistribute it and/or modify it under
       
     6 # the terms of the GNU Lesser General Public License as published by the Free
       
     7 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     8 # any later version.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful, but WITHOUT
       
    11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
       
    13 # details.
       
    14 #
       
    15 # You should have received a copy of the GNU Lesser General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 from rdflib import Namespace, ConjunctiveGraph, plugin
       
    19 import rdflib_jsonld  # noqa
       
    20 
       
    21 plugin.register("jsonld", plugin.Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer")
       
    22 
       
    23 RDF_MIMETYPE_TO_FORMAT = {
       
    24     'application/rdf+xml': 'xml',
       
    25     'text/turtle': 'turtle',
       
    26     'text/n3': 'n3',
       
    27     'application/n-quads': 'nquads',
       
    28     'application/n-triples': 'nt',
       
    29     'application/trig': 'trig',
       
    30     'application/ld+json': 'json-ld',
       
    31 }
       
    32 
       
    33 namespaces = {
       
    34     "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
       
    35     "schema": "http://schema.org/",
       
    36 }
       
    37 
       
    38 NS_VARS = {ns: Namespace(uri) for ns, uri in namespaces.items()}
       
    39 
       
    40 
       
    41 # dict: name of CWEType -> list of regid of adapters derived from EntityRDFAdapter
       
    42 ETYPES_ADAPTERS = {
       
    43 }
       
    44 
       
    45 
       
    46 def conjunctive_graph():
       
    47     """factory to build a ``ConjunctiveGraph`` and bind all namespaces
       
    48     """
       
    49     graph = ConjunctiveGraph()
       
    50     for vocab, rdfns in NS_VARS.items():
       
    51         graph.bind(vocab, rdfns)
       
    52     return graph
       
    53 
       
    54 
       
    55 def iter_rdf_adapters(entity):
       
    56     for adapter_id in ETYPES_ADAPTERS.get(entity.__regid__, ()):
       
    57         adapter = entity.cw_adapt_to(adapter_id)
       
    58         if adapter:
       
    59             yield adapter
       
    60 
       
    61 
       
    62 def add_entity_to_graph(graph, entity):
       
    63     for adapter in iter_rdf_adapters(entity):
       
    64         for triple in adapter.triples():
       
    65             graph.add(triple)