Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Created March 19, 2014 15:58
Show Gist options
  • Save ericmoritz/9644841 to your computer and use it in GitHub Desktop.
Save ericmoritz/9644841 to your computer and use it in GitHub Desktop.
from collections import Mapping, Iterator
from rdflib import URIRef, Graph, Literal, Namespace
import pystache
from pystache.context import ContextStack
import re
RDFS = Namespace(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
class GraphDict(Mapping, dict):
"""
Provides a dict interface to a RDF graph
>>> d = GraphDict(__graph_fixture(), "http://example.com/")
>>> d.bind("", "http://example.com/")
>>> d.bind("schema", "http://schema.org/")
>>> d
<GraphDict rdflib.term.URIRef(u'http://example.com/')>
>>> d['unknown:field']
[]
>>> d['schema:title']
u'Root Object'
>>> d['schema:dateCreated']
datetime.date(2014, 3, 18)
>>> sorted(d[':story'])
[<GraphDict rdflib.term.URIRef(u'http://example.com/story-1')>, <GraphDict rdflib.term.URIRef(u'http://example.com/story-2')>]
>>> sorted(d[':story'])[0]['schema:title']
u'Story 1'
>>> sorted(d[':story'])[1]['schema:title']
u'Story 2'
"""
def __init__(self, graph, subject_uri):
self.__graph = graph
self.subject_uri = URIRef(subject_uri)
self.__namespaces = set()
def clone(self):
d = GraphDict(
self.__graph,
self.subject_uri
)
d.__namespaces = self.__namespaces
return d
def bind(self, prefix, uri):
self.__namespaces.add((prefix, uri))
self.__graph.bind(prefix, Namespace(uri))
def __cmp__(self, y):
return cmp(repr(self), repr(y))
def __eq__(self, y):
return x.subject_uri == y.subject_uri
def __repr__(self):
return "<GraphDict {!r}>".format(self.subject_uri)
def __contains__(self, key):
return self.__getitem__(key) != []
def __getitem__(self, key):
def dict_or_literal(o):
if type(o) is Literal:
return o.toPython()
else:
d = self.clone()
d.subject_uri = o
return d
predicate_uri = self._expand_key(
key,
self.__namespaces,
)
if predicate_uri is None:
return []
values = [
dict_or_literal(o)
for (s, p, o) in
self.__graph.triples(
(self.subject_uri, URIRef(predicate_uri), None)
)
]
if len(values) == 1:
return values[0]
else:
return values
def __iter__(self):
return (
p
for (s, p, o) in
self.__graph.triples((self.subject_uri, None, None))
)
def __len__(self):
return len(
self.__graph.triples((self.subject_uri, None, None))
)
@staticmethod
def __split_key(key):
bits = key.split(":", 1)
if len(bits) == 1:
return "", bits[0]
else:
return bits
@classmethod
def _expand_key(cls, key, namespaces):
"""
>>> GraphDict._expand_key(
... u"schema:title",
... [(u"schema", u"http://schema.org/")]
... )
u'http://schema.org/title'
>>> GraphDict._expand_key(
... u"xsd:title",
... [(u"schema", u"http://schema.org/")]
... ) is None
True
"""
prefix, field = cls.__split_key(key)
for (ns_prefix, ns) in namespaces:
if prefix == ns_prefix:
return ns + field
@staticmethod
def _compact_key(uri, namespaces):
"""
>>> GraphDict._compact_key(
... u"http://schema.org/title",
... [(u"schema", u"http://schema.org/")]
... )
u'schema:title'
>>> GraphDict._compact_key(
... u"http://example.com/title",
... [(u"schema", u"http://schema.org/")]
... )
u'http://example.com/title'
"""
for (prefix, ns_uri) in namespaces:
if uri.startswith(ns_uri):
field = uri[len(ns_uri):]
return prefix + u":" + field
return uri
def render(graph, subject_uri, template):
"""
>>> template = '''
... {{#prefix}}schema: <http://schema.org/>{{/prefix}}
... {{#prefix}}: <http://example.com/>{{/prefix}}
... {{schema:title}}
...
... {{#:story}}
... {{schema:title}}
... {{/:story}}
... '''
>>> render(__graph_fixture(), "http://example.com/", template).strip()
u'Root Object\\n\\nStory 1\\nStory 2'
"""
d = GraphDict(graph, subject_uri)
def prefix_fun(text):
bits = __parse_prefix(text)
if bits:
d.bind(*bits)
return ""
return pystache.render(
template,
ContextStack(
{"prefix": prefix_fun},
d,
)
)
PAT = re.compile(r"([^:]*):\s*<([^>]+)>")
def __parse_prefix(src):
"""
>>> __parse_prefix(u": <http://example.com/>")
(u'', u'http://example.com/')
>>> __parse_prefix(u"schema: <http://schema.org/>")
(u'schema', u'http://schema.org/')
"""
match = PAT.match(src)
if match:
return match.group(1), match.group(2)
def __graph_fixture():
return Graph().parse(data='''
@prefix : <http://example.com/> .
@prefix schema: <http://schema.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<./>
schema:title "Root Object" ;
schema:dateCreated "2014-03-18"^^xsd:date ;
:story <./story-1>, <./story-2> .
<./story-1>
schema:title "Story 1" .
<./story-2>
schema:title "Story 2" .
''', format="turtle", publicID="http://example.com/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment