Changeset - 717774b8a4a6
[Not reviewed]
default
0 1 0
Drew Perttula - 11 years ago 2014-05-28 05:55:58
drewp@bigasterisk.com
syncedgraph gets items() method for fetching an rdf list
Ignore-this: c97d4718ab1cede9c196464053c9ab47
1 file changed with 15 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/autodepgraphapi.py
Show inline comments
 
@@ -78,53 +78,67 @@ class AutoDepGraphApi(object):
 
    # work from a starting uri)
 

	
 
    def value(self, subject=None, predicate=RDF.value, object=None,
 
              default=None, any=True):
 
        if object is not None:
 
            raise NotImplementedError()
 
        func = self._getCurrentFunc()
 
        self._watchers.addSubjPredWatcher(func, subject, predicate)
 
        return self._graph.value(subject, predicate, object=object,
 
                                 default=default, any=any)
 

	
 
    def objects(self, subject=None, predicate=None):
 
        func = self._getCurrentFunc()
 
        self._watchers.addSubjPredWatcher(func, subject, predicate)
 
        return self._graph.objects(subject, predicate)
 

	
 
    def label(self, uri):
 
        return self.value(uri, RDFS.label)
 

	
 
    def subjects(self, predicate=None, object=None):
 
        func = self._getCurrentFunc()
 
        self._watchers.addPredObjWatcher(func, predicate, object)
 
        return self._graph.subjects(predicate, object)
 

	
 
    def items(self, listUri):
 
        """generator. Having a chain of watchers on the results is not
 
        well-tested yet"""
 
        chain = set([listUri])
 
        while listUri:
 
            item = self.value(listUri, RDF.first)
 
            if item:
 
                yield item
 
            listUri = self.value(listUri, RDF.rest)
 
            if listUri in chain:
 
                raise ValueError("List contains a recursive rdf:rest reference")
 
            chain.add(listUri)
 

	
 
        
 
    def contains(self, triple):
 
        func = self._getCurrentFunc()
 
        self._watchers.addTripleWatcher(func, triple)
 
        return triple in self._graph
 
        
 

	
 
    def contextsForStatement(self, triple):
 
        """currently this needs to be in an addHandler section, but it
 
        sets no watchers so it won't actually update if the statement
 
        was added or dropped from contexts"""
 
        func = self._getCurrentFunc()
 
        return contextsForStatementNoWildcards(self._graph, triple)
 

	
 
    # i find myself wanting 'patch' (aka enter/leave) versions of these calls that tell
 
    # you only what results have just appeared or disappeared. I think
 
    # I'm going to be repeating that logic a lot. Maybe just for the
 
    # subjects(RDF.type, t) call
 

	
 

	
 
class _GraphWatchers(object):
 
    """
 
    store the current handlers that care about graph changes
 
    """
 
    def __init__(self):
 
        self._handlersSp = {} # (s,p): set(handlers)
 
        self._handlersPo = {} # (p,o): set(handlers)
 
        self._handlersSpo = {} # (s,p,o): set(handlers)
 

	
 
    def addSubjPredWatcher(self, func, s, p):
 
        if func is None:
0 comments (0 inline, 0 general)