Changeset - f1c062f3b227
[Not reviewed]
default
0 2 0
Drew Perttula - 8 years ago 2017-03-30 05:22:48
drewp@bigasterisk.com
rdflib upgrade
Ignore-this: 299dba7405bf435dcd432fd49081a0f8
2 files changed with 12 insertions and 5 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/rdflibpatch.py
Show inline comments
 
"""
 
this is a proposal for a ConjunctiveGraph method in rdflib
 
"""
 
import sys
 
if sys.path[0] == '/usr/lib/python2.7/dist-packages':
 
    # nosetests puts this in
 
    sys.path = sys.path[1:]
 

	
 
import unittest
 
from rdflib import ConjunctiveGraph, Graph, URIRef as U
 
from rdflib import ConjunctiveGraph, Graph, URIRef as U, Literal
 

	
 
def patchQuads(graph, deleteQuads, addQuads, perfect=False):
 
    """
 
    Delete the sequence of given quads. Then add the given quads just
 
    like addN would. If perfect is True, we'll error before the
 
    deletes or before the adds (not a real transaction) if any of the
 
    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, but
 
    Graph(identifier=) is also allowed (which is what you'll get
 
    sometimes from rdflib APIs).
 
@@ -64,38 +64,45 @@ def graphFromQuads(q):
 
    g = ConjunctiveGraph()
 
    #g.addN(q) # no effect on nquad output
 
    for s,p,o,c in q:
 
        #g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code; you need this for json_ld serialize to work :(
 
        g.store.add((s,p,o), c) # no effect on nquad output
 
    return g
 

	
 
def graphFromNQuad(text):
 
    g1 = ConjunctiveGraph()
 
    g1.parse(data=text, format='nquads')
 
    return g1
 

	
 
from rdflib.plugins.serializers.nt import _xmlcharref_encode
 
from rdflib.plugins.serializers.nt import _quoteLiteral
 
def serializeQuad(g):
 
    """replacement for graph.serialize(format='nquads')"""
 
    """
 
    replacement for graph.serialize(format='nquads')
 

	
 
    Still broken in rdflib 4.2.2: graph.serialize(format='nquads')
 
    returns empty string for my graph in
 
    TestGraphFromQuads.testSerializes.
 
    """
 
    out = []
 
    for s,p,o,c in g.quads((None,None,None)):
 
        if isinstance(c, Graph):
 
            # still not sure why this is Graph sometimes,
 
            # already URIRef other times
 
            c = c.identifier
 
        if '[' in c.n3():
 
            import ipdb;ipdb.set_trace()
 
        ntObject = _quoteLiteral(o) if isinstance(o, Literal) else o.n3()
 
        out.append(u"%s %s %s %s .\n" % (s.n3(),
 
                                     p.n3(),
 
                                     _xmlcharref_encode(o.n3()),
 
                                     ntObject,
 
                                     c.n3()))
 
    return ''.join(out)
 

	
 
def inContext(graph, newContext):
 
    """
 
    make a ConjunctiveGraph where all the triples in the given graph
 
    (or collection) are now in newContext (a uri)
 
    """
 
    return graphFromQuads((s,p,o,newContext) for s,p,o in graph)
 

	
 
def contextsForStatement(graph, triple):
 
    return [q[3] for q in graph.quads(triple)]
requirements.txt
Show inline comments
 
rdflib==4.1.2
 
rdflib==4.2.2
 
Twisted==17.1.0
 
webcolors==1.7
 
Louie==1.1
 
cyclone==1.1
 

	
 
web.py==0.38
 
restkit==4.2.2
 
ipython==5.3.0
 
nose==1.3.7
 
nose-watcher==0.1.3
 
watchdog==0.8.3
 
freezegun==0.3.8
0 comments (0 inline, 0 general)