Commit a38d2f5e authored by Simon Chabot's avatar Simon Chabot
Browse files

[rdf] Add a basic foaf adapter for CWUser

By the way, use RDFLib namespaces
parent eb0cd6060062
......@@ -20,8 +20,11 @@ framework itself.
"""
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 import (Unauthorized, ValidationError, ViolatedConstraint,
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 @@ class EntityRDFAdapter(EntityAdapter):
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')
......
......@@ -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 @@ RDF_MIMETYPE_TO_FORMAT = {
'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 @@ def conjunctive_graph():
"""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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment