# HG changeset patch # User Drew Perttula # Date 1401175855 0 # Node ID 531e097d3386875a60efba764ea019e22a7b5a86 # Parent a13eee92e60c2dc9151a7cc6f5f06b1b7f9b8db1 give up on forcing everyone to pass URIRef context; accept Graph objects too Ignore-this: dbba5eacc31d9b64cb3627f037530608 diff -r a13eee92e60c -r 531e097d3386 light9/rdfdb/rdflibpatch.py --- a/light9/rdfdb/rdflibpatch.py Tue May 27 07:29:54 2014 +0000 +++ b/light9/rdfdb/rdflibpatch.py Tue May 27 07:30:55 2014 +0000 @@ -17,13 +17,13 @@ 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 @@ 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 @@ 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):