cubicweb/rdf.py
author Laurent Wouters <lwouters@cenotelie.fr>
Fri, 20 Mar 2020 14:34:07 +0100
changeset 12931 6eae252361e5
parent 12910 c87c3943d6ab
permissions -rw-r--r--
[rql] Store selected variables for RQL select queries in ResultSet (#17218476) By storing the name of the selected variables for RQL select queries in the ResultSet (within the "variables" attribute), the information can be passed down to specific protocols, e.g. rqlio that may wish to pass is down further to clients. In turn, clients can then choose to present the results of RQL select queries as symbolic bindings using the names used in the query's projection, instead of ordinal arrays.

# -*- 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 <http://www.gnu.org/licenses/>.

from rdflib import plugin, namespace
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": namespace.RDF,
    "rdfs": namespace.RDFS,
    "owl": namespace.OWL,
    "xsd": namespace.XSD,
    "skos": namespace.SKOS,
    "void": namespace.VOID,
    "dc": namespace.DC,
    "dcterms": namespace.DCTERMS,
    "foaf": namespace.FOAF,
    "doap": namespace.DOAP,
    "schema": namespace.Namespace("http://schema.org/"),
    "cubicweb": namespace.Namespace("http://ns.cubicweb.org/cubicweb/0.0/")
}


def add_entity_to_graph(graph, entity):
    adapter = entity.cw_adapt_to("rdf")
    if adapter:
        for triple in adapter.triples():
            graph.add(triple)
        for prefix, rdfns in adapter.used_namespaces.items():
            graph.bind(prefix, rdfns)