cubicweb/rdf.py
changeset 12910 c87c3943d6ab
parent 12892 0df0db725f07
equal deleted inserted replaced
12892:0df0db725f07 12910:c87c3943d6ab
    13 # details.
    13 # details.
    14 #
    14 #
    15 # You should have received a copy of the GNU Lesser General Public License
    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/>.
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
    17 
    17 
    18 from rdflib import ConjunctiveGraph, plugin
    18 from rdflib import plugin, namespace
    19 from rdflib.namespace import Namespace, RDF, FOAF
       
    20 import rdflib_jsonld  # noqa
    19 import rdflib_jsonld  # noqa
    21 
    20 
    22 plugin.register("jsonld", plugin.Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer")
    21 plugin.register("jsonld", plugin.Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer")
    23 
    22 
    24 RDF_MIMETYPE_TO_FORMAT = {
    23 RDF_MIMETYPE_TO_FORMAT = {
    30     'application/trig': 'trig',
    29     'application/trig': 'trig',
    31     'application/ld+json': 'json-ld',
    30     'application/ld+json': 'json-ld',
    32 }
    31 }
    33 
    32 
    34 NAMESPACES = {
    33 NAMESPACES = {
    35     "rdf": RDF,
    34     "rdf": namespace.RDF,
    36     "schema": Namespace("http://schema.org/"),
    35     "rdfs": namespace.RDFS,
    37     "foaf": FOAF,
    36     "owl": namespace.OWL,
       
    37     "xsd": namespace.XSD,
       
    38     "skos": namespace.SKOS,
       
    39     "void": namespace.VOID,
       
    40     "dc": namespace.DC,
       
    41     "dcterms": namespace.DCTERMS,
       
    42     "foaf": namespace.FOAF,
       
    43     "doap": namespace.DOAP,
       
    44     "schema": namespace.Namespace("http://schema.org/"),
       
    45     "cubicweb": namespace.Namespace("http://ns.cubicweb.org/cubicweb/0.0/")
    38 }
    46 }
    39 
    47 
    40 
    48 
    41 # dict: name of CWEType -> list of regid of adapters derived from EntityRDFAdapter
       
    42 ETYPES_ADAPTERS = {
       
    43     "CWUser": ("rdf.foaf",),
       
    44 }
       
    45 
       
    46 
       
    47 def conjunctive_graph():
       
    48     """factory to build a ``ConjunctiveGraph`` and bind all namespaces
       
    49     """
       
    50     graph = ConjunctiveGraph()
       
    51     for prefix, rdfns in NAMESPACES.items():
       
    52         graph.bind(prefix, rdfns)
       
    53     return graph
       
    54 
       
    55 
       
    56 def iter_rdf_adapters(entity):
       
    57     for adapter_id in ETYPES_ADAPTERS.get(entity.__regid__, ()):
       
    58         adapter = entity.cw_adapt_to(adapter_id)
       
    59         if adapter:
       
    60             yield adapter
       
    61 
       
    62 
       
    63 def add_entity_to_graph(graph, entity):
    49 def add_entity_to_graph(graph, entity):
    64     for adapter in iter_rdf_adapters(entity):
    50     adapter = entity.cw_adapt_to("rdf")
       
    51     if adapter:
    65         for triple in adapter.triples():
    52         for triple in adapter.triples():
    66             graph.add(triple)
    53             graph.add(triple)
       
    54         for prefix, rdfns in adapter.used_namespaces.items():
       
    55             graph.bind(prefix, rdfns)