Changeset - 531e097d3386
[Not reviewed]
default
0 1 0
Drew Perttula - 11 years ago 2014-05-27 07:30:55
drewp@bigasterisk.com
give up on forcing everyone to pass URIRef context; accept Graph objects too
Ignore-this: dbba5eacc31d9b64cb3627f037530608
1 file changed with 12 insertions and 5 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/rdflibpatch.py
Show inline comments
 
@@ -17,13 +17,13 @@ def patchQuads(graph, deleteQuads, addQu
 
    deletes isn't in the graph or if any of the adds was already in
 
    the graph.
 

	
 
    These input quads use URIRef for the context, unlike some rdflib
 
    APIs that use a Graph(identifier=...) for the context.
 
    These input quads use URIRef for the context, but
 
    Graph(identifier=) is also allowed (which is what you'll get
 
    sometimes from rdflib APIs).
 
    """
 
    toDelete = []
 
    for spoc in deleteQuads:
 
        if not isinstance(spoc[3], U):
 
            raise TypeError("please pass URIRef contexts to patchQuads")
 
        spoc = fixContextToUri(spoc)
 

	
 
        if perfect:
 
            if inGraph(spoc, graph):
 
@@ -38,10 +38,16 @@ def patchQuads(graph, deleteQuads, addQu
 
    if perfect:
 
        addQuads = list(addQuads)
 
        for spoc in addQuads:
 
            spoc = fixContextToUri(spoc)
 
            if inGraph(spoc, graph):
 
                raise ValueError("%r already in %r" % (spoc[:3], spoc[3]))
 
    graph.addN(addQuads)
 

	
 
def fixContextToUri(spoc):
 
    if not isinstance(spoc[3], U):
 
        return spoc[:3] + (spoc[3].identifier,)
 
    return spoc
 
    
 
def inGraph(spoc, graph):
 
    """
 
    c is just a URIRef.
 
@@ -102,7 +108,8 @@ class TestContextsForStatement(unittest.
 
    def testTwoContexts(self):
 
        g = graphFromQuads([(A,A,A,A), (A,A,A,B)])
 
        self.assertEqual(sorted(contextsForStatement(g, (A,A,A))), sorted([A,B]))
 

	
 
    # There's a case where contextsForStatement was returning a Graph
 
    # with identifier, which I've fixed without a test
 

	
 

	
 
class TestGraphFromQuads(unittest.TestCase):
0 comments (0 inline, 0 general)