Changeset - 6bd158820ae8
[Not reviewed]
default
0 1 0
Drew Perttula - 12 years ago 2013-06-04 20:40:56
drewp@bigasterisk.com
in a Patch, always keep the context of a quad as a URIRef, never a Graph
Ignore-this: 490213fede41f75af27d4bf9e4669d0e
1 file changed with 20 insertions and 5 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/patch.py
Show inline comments
 
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
 
    
 
    the json representation includes the {"patch":...} wrapper
 
    """
 
    def __init__(self, jsonRepr=None, addQuads=None, delQuads=None,
 
                 addGraph=None, delGraph=None):
 
        """
 
        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
 
        self._addGraph, self._delGraph = addGraph, delGraph
 

	
 
        if self._jsonRepr is not None:
 
@@ -30,14 +43,14 @@ class Patch(object):
 

	
 
    @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):
 
        """
 
        does this patch do anything to a graph?
 
        """
 
@@ -48,21 +61,23 @@ class Patch(object):
 

	
 
    @property
 
    def addQuads(self):
 
        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
 
    def delQuads(self):
 
        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
 
    def addGraph(self):
 
        if self._addGraph is None:
 
            self._addGraph = graphFromQuads(self._addQuads)
0 comments (0 inline, 0 general)