[rdf] Add a basic foaf adapter for CWUser
authorSimon Chabot <simon.chabot@logilab.fr>
Fri, 28 Feb 2020 17:11:01 +0100
changeset 12892 0df0db725f07
parent 12891 eb0cd6060062
child 12900 2cc3f481ecd0
child 12910 c87c3943d6ab
[rdf] Add a basic foaf adapter for CWUser By the way, use RDFLib namespaces
cubicweb/entities/adapters.py
cubicweb/rdf.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')
--- 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 <http://www.gnu.org/licenses/>.
 
-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