# HG changeset patch # User Simon Chabot # Date 1582906261 -3600 # Node ID 0df0db725f07cea86caad147c16f91863429af93 # Parent eb0cd60600623a70aa74b0d243fe243ecb2c55a0 [rdf] Add a basic foaf adapter for CWUser By the way, use RDFLib namespaces diff -r eb0cd6060062 -r 0df0db725f07 cubicweb/entities/adapters.py --- a/cubicweb/entities/adapters.py Fri Feb 14 18:15:55 2020 +0100 +++ b/cubicweb/entities/adapters.py Fri Feb 28 17:11:01 2020 +0100 @@ -20,8 +20,11 @@ """ from cubicweb import _ +from hashlib import sha1 from itertools import chain +from rdflib import URIRef, Literal + from logilab.mtconverter import TransformError from logilab.common.decorators import cached, cachedproperty @@ -31,6 +34,8 @@ from cubicweb.schema import constraint_name_for from cubicweb.predicates import is_instance, relation_possible, match_exception +from cubicweb.rdf import NAMESPACES + class EntityRDFAdapter(EntityAdapter): """EntityRDFAdapter is to be specialized for each entity that wants to @@ -51,6 +56,25 @@ raise NotImplementedError() +class CWUserFoafAdapter(EntityRDFAdapter): + __regid__ = "rdf.foaf" + __select__ = is_instance("CWUser") + + def triples(self): + RDF = NAMESPACES["rdf"] + FOAF = NAMESPACES["foaf"] + uri = URIRef(self.uri) + yield (uri, RDF.type, FOAF.Person) + if self.entity.surname: + yield (uri, FOAF.familyName, Literal(self.entity.surname)) + if self.entity.firstname: + yield (uri, FOAF.givenName, Literal(self.entity.firstname)) + emailaddr = self.entity.cw_adapt_to("IEmailable").get_email() + if emailaddr: + email_digest = sha1(emailaddr.encode("utf-8")).hexdigest() + yield (uri, FOAF.mbox_sha1sum, Literal(email_digest)) + + class IDublinCoreAdapter(EntityAdapter): __regid__ = 'IDublinCore' __select__ = is_instance('Any') diff -r eb0cd6060062 -r 0df0db725f07 cubicweb/rdf.py --- a/cubicweb/rdf.py Fri Feb 14 18:15:55 2020 +0100 +++ b/cubicweb/rdf.py Fri Feb 28 17:11:01 2020 +0100 @@ -15,7 +15,8 @@ # 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 +from rdflib import ConjunctiveGraph, plugin +from rdflib.namespace import Namespace, RDF, FOAF import rdflib_jsonld # noqa plugin.register("jsonld", plugin.Serializer, "rdflib_jsonld.serializer", "JsonLDSerializer") @@ -30,16 +31,16 @@ 'application/ld+json': 'json-ld', } -namespaces = { - "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", - "schema": "http://schema.org/", +NAMESPACES = { + "rdf": RDF, + "schema": Namespace("http://schema.org/"), + "foaf": FOAF, } -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 = { + "CWUser": ("rdf.foaf",), } @@ -47,8 +48,8 @@ """factory to build a ``ConjunctiveGraph`` and bind all namespaces """ graph = ConjunctiveGraph() - for vocab, rdfns in NS_VARS.items(): - graph.bind(vocab, rdfns) + for prefix, rdfns in NAMESPACES.items(): + graph.bind(prefix, rdfns) return graph