[Adapters, rdf] Use RDF adapters and triples function draft
authorElodie Thieblin <ethieblin@logilab.fr>
Mon, 09 Mar 2020 17:18:44 +0100
changeset 400 8eb729fccb8b
parent 399 dceb8fb22e57
child 401 94e3c2fdfce3
[Adapters, rdf] Use RDF adapters and triples function
cubicweb_blog/entities.py
cubicweb_blog/rdf.py
--- a/cubicweb_blog/entities.py	Fri Feb 14 18:03:54 2020 +0100
+++ b/cubicweb_blog/entities.py	Mon Mar 09 17:18:44 2020 +0100
@@ -9,73 +9,93 @@
 
 from logilab.common.date import todate
 
+from rdflib import URIRef, Literal
+
 from cubicweb.entities import AnyEntity, fetch_config
 from cubicweb.entities.adapters import (
-    EntityRDFAdapter, CWUserFoafAdapter as _CWUserFoafAdapter,
+    EntityRDFAdapter, CWUserRDFAdapter as _CWUserRDFAdapter,
 )
 from cubicweb.view import EntityAdapter
 from cubicweb.predicates import is_instance
 
-from cubicweb_blog.rdf import NS_VARS
+from cubicweb_blog.rdf import NAMESPACES
 
 __docformat__ = "restructuredtext en"
 
 
-class CWUserFoafAdapter(_CWUserFoafAdapter):
-    def relations(self):
-        yield from super().relations()
+class CWUserRDFAdapter(_CWUserRDFAdapter):
+    def triples(self):
+        yield from super().triples()
+        uri = URIRef(self.uri)
         blog_entries = self._cw.find("BlogEntry", created_by=self.entity)
         for entry in blog_entries.entities():
-            yield ("sioc", "creator_of", entry.cwuri)
+            yield (uri, NAMESPACES["sioc"].creator_of, URIRef(entry.cwuri))
 
 
-class BlogSiocAdapter(EntityRDFAdapter):
-    __regid__ = "rdf.sioc"
+class BlogRDFAdapter(EntityRDFAdapter):
+    __regid__ = "rdf"
     __select__ = is_instance("Blog")
-    rdf_type = NS_VARS["sioc"] + "Weblog"
 
-    def properties(self):
-        yield ("dcterms", "title", self.entity.dc_title(), {})
-        yield ("dcterms", "description", self.entity.description, {})
-        yield ("sioc", "num_items", len(self.entity.reverse_entry_of), {})
-        yield ("sioc", "feed", self.entity.cw_adapt_to('IFeed').rss_feed_url(), {})
+    def triples(self):
+        SIOC = NAMESPACES["sioc"]
+        RDF = NAMESPACES["rdf"]
+        DCTERMS = NAMESPACES["dcterms"]
+        uri = URIRef(self.uri)
+        yield(uri, RDF.type, SIOC.Weblog)
+        yield (uri, DCTERMS.title, Literal(self.entity.dc_title()))
+        yield (uri, DCTERMS.description, Literal(self.entity.description))
+        yield (uri, SIOC.num_items, Literal(len(self.entity.reverse_entry_of)))
+        yield(uri, SIOC.feed, URIRef(self.entity.cw_adapt_to('IFeed').rss_feed_url()))
 
-    def relations(self):
         if self.entity.creator:
-            yield ("sioc", "has_creator", self.entity.creator.cwuri)
+            yield (uri, SIOC.has_creator, URIRef(self.entity.creator.cwuri))
 
         for blog_entry in self.entity.reverse_entry_of:
-            yield ("sioc", "container_of", blog_entry.cwuri)
+            yield (uri, SIOC.container_of, URIRef(blog_entry.cwuri))
 
 
-class BlogEntrySiocAdapter(EntityRDFAdapter):
-    __regid__ = "rdf.sioc"
+class BlogEntryRDFAdapter(EntityRDFAdapter):
+    __regid__ = "rdf"
     __select__ = is_instance("BlogEntry")
-    rdf_type = NS_VARS["sioc"] + "BlogPost"
 
-    def properties(self):
-        yield ("dcterms", "title", self.entity.dc_title(), {})
-        yield ("dcterms", "date", self.entity.creation_date, {})
-        yield ("dcterms", "modified", self.entity.modification_date, {})
-        yield ("sioc", "content", self.entity.content, {})
-
-    def relations(self):
+    def triples(self):
+        SIOC = NAMESPACES["sioc"]
+        RDF = NAMESPACES["rdf"]
+        DCTERMS = NAMESPACES["dcterms"]
+        uri = URIRef(self.uri)
+        yield(uri, RDF.type, SIOC.BlogPost)
+        yield (uri, DCTERMS.title, Literal(self.entity.dc_title()))
+        yield (uri, DCTERMS.date, Literal(self.entity.creation_date))
+        yield (uri, DCTERMS.modified, Literal(self.entity.modification_date))
+        yield (uri, SIOC.content, Literal(self.entity.content))
         if self.entity.entry_of:
-            yield ("sioc", "container", self.entity.entry_of[0].cwuri)
+            yield (uri, SIOC.container, URIRef(self.entity.entry_of[0].cwuri))
         if self.entity.creator:
-            yield ("sioc", "has_owner", self.entity.creator.cwuri)
+            yield (uri, SIOC.has_owner, URIRef(self.entity.creator.cwuri))
 
 
-class MicroBlogSiocAdapter(BlogSiocAdapter):
-    __regid__ = "rdf.sioc"
+class MicroBlogRDFAdapter(BlogRDFAdapter):
+    __regid__ = "rdf"
     __select__ = is_instance("MicroBlog")
-    rdf_type = NS_VARS["sioc"] + "Microblog"
+
+    def triples(self):
+        yield from super().triples()
+        SIOC = NAMESPACES["sioc"]
+        RDF = NAMESPACES["rdf"]
+        uri = URIRef(self.uri)
+        yield(uri, RDF.type, SIOC.Microblog)
 
 
-class MicroBlogEntrySiocAdapter(BlogEntrySiocAdapter):
-    __regid__ = "rdf.sioc"
+class MicroBlogEntryRDFAdapter(BlogEntryRDFAdapter):
+    __regid__ = "rdf"
     __select__ = is_instance("MicroBlogEntry")
-    rdf_type = NS_VARS["sioc"] + "MicroblogPost"
+
+    def triples(self):
+        yield from super().triples()
+        SIOC = NAMESPACES["sioc"]
+        RDF = NAMESPACES["rdf"]
+        uri = URIRef(self.uri)
+        yield(uri, RDF.type, SIOC.MicroblogPost)
 
 
 ###################################################
@@ -177,8 +197,8 @@
 
 
 def registration_callback(vreg):
-    vreg.register(BlogSiocAdapter)
-    vreg.register(BlogEntrySiocAdapter)
-    vreg.register(MicroBlogSiocAdapter)
-    vreg.register(MicroBlogEntrySiocAdapter)
-    vreg.register_and_replace(CWUserFoafAdapter, _CWUserFoafAdapter)
+    vreg.register(BlogRDFAdapter)
+    vreg.register(BlogEntryRDFAdapter)
+    vreg.register(MicroBlogRDFAdapter)
+    vreg.register(MicroBlogEntryRDFAdapter)
+    vreg.register_and_replace(CWUserRDFAdapter, _CWUserRDFAdapter)
--- a/cubicweb_blog/rdf.py	Fri Feb 14 18:03:54 2020 +0100
+++ b/cubicweb_blog/rdf.py	Mon Mar 09 17:18:44 2020 +0100
@@ -3,11 +3,7 @@
 
 from rdflib import Namespace
 
-from cubicweb.rdf import ETYPES_ADAPTERS, NS_VARS
+from cubicweb.rdf import NAMESPACES
 
-NS_VARS["sioc"] = Namespace("http://rdfs.org/sioc/types#")
-NS_VARS["dcterms"] = Namespace("http://purl.org/dc/terms/")
-
-ETYPES_ADAPTERS["Blog"] = ("rdf.sioc", )
-ETYPES_ADAPTERS["BlogEntry"] = ("rdf.sioc", )
-ETYPES_ADAPTERS["MicroBlog"] = ("rdf.sioc", )
+NAMESPACES["sioc"] = Namespace("http://rdfs.org/sioc/types#")
+NAMESPACES["dcterms"] = Namespace("http://purl.org/dc/terms/")