changeset 828:6bd158820ae8

in a Patch, always keep the context of a quad as a URIRef, never a Graph Ignore-this: 490213fede41f75af27d4bf9e4669d0e
author Drew Perttula <drewp@bigasterisk.com>
date Tue, 04 Jun 2013 20:40:56 +0000
parents 9816e249ce2e
children e53e78db7b17
files light9/rdfdb/patch.py
diffstat 1 files changed, 21 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/light9/rdfdb/patch.py	Tue Jun 04 20:29:13 2013 +0000
+++ b/light9/rdfdb/patch.py	Tue Jun 04 20:40:56 2013 +0000
@@ -1,9 +1,21 @@
 import json, unittest
-from rdflib import ConjunctiveGraph, URIRef, URIRef as U
+from rdflib import ConjunctiveGraph, Graph, URIRef, URIRef as U
 from light9.rdfdb.rdflibpatch import graphFromNQuad, graphFromQuads, serializeQuad
 
 ALLSTMTS = (None, None, None)
 
+def quadsWithContextUris(quads):
+    """
+    yield the given quads, correcting any context values that are
+    Graphs into URIRefs
+    """
+    for s,p,o,c in quads:
+        if isinstance(c, Graph):
+            c = c.identifier
+        if not isinstance(c, URIRef):
+            raise TypeError("bad quad context type in %r" % ((s,p,o,c),))
+        yield s,p,o,c
+
 class Patch(object):
     """
     immutable
@@ -16,6 +28,7 @@
         addQuads/delQuads can be lists or sets, but if we make them internally,
         they'll be lists
 
+        4th element of a quad must be a URIRef
         """
         self._jsonRepr = jsonRepr
         self._addQuads, self._delQuads = addQuads, delQuads
@@ -27,14 +40,14 @@
             self._addGraph = graphFromNQuad(body['patch']['adds'])
             if 'senderUpdateUri' in body:
                 self.senderUpdateUri = body['senderUpdateUri']
-
+                
     @classmethod
     def fromDiff(cls, oldGraph, newGraph):
         """
         make a patch that changes oldGraph to newGraph
         """
-        old = set(oldGraph.quads(ALLSTMTS))
-        new = set(newGraph.quads(ALLSTMTS))
+        old = set(quadsWithContextUris(oldGraph.quads(ALLSTMTS)))
+        new = set(quadsWithContextUris(newGraph.quads(ALLSTMTS)))
         return cls(addQuads=list(new - old), delQuads=list(old - new))
 
     def __nonzero__(self):
@@ -51,7 +64,8 @@
         if self._addQuads is None:
             if self._addGraph is None:
                 return []
-            self._addQuads = list(self._addGraph.quads(ALLSTMTS))
+            self._addQuads = list(quadsWithContextUris(
+                self._addGraph.quads(ALLSTMTS)))
         return self._addQuads
 
     @property
@@ -59,7 +73,8 @@
         if self._delQuads is None:
             if self._delGraph is None:
                 return []
-            self._delQuads = list(self._delGraph.quads(ALLSTMTS))
+            self._delQuads = list(quadsWithContextUris(
+                self._delGraph.quads(ALLSTMTS)))
         return self._delQuads
 
     @property