<s> import collections <EOL> import itertools <EOL> import datetime <EOL> from rdflib . namespace import NamespaceManager <EOL> from rdflib import Variable , BNode , Graph , ConjunctiveGraph , URIRef , Literal <EOL> from rdflib . term import Node <EOL> from parserutils import CompValue <EOL> import rdflib . plugins . sparql <EOL> from rdflib . plugins . sparql . compat import Mapping , MutableMapping <EOL> class SPARQLError ( Exception ) : <EOL> def __init__ ( self , msg = None ) : <EOL> Exception . __init__ ( self , msg ) <EOL> class NotBoundError ( SPARQLError ) : <EOL> def __init__ ( self , msg = None ) : <EOL> SPARQLError . __init__ ( self , msg ) <EOL> class AlreadyBound ( SPARQLError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> SPARQLError . __init__ ( self ) <EOL> class SPARQLTypeError ( SPARQLError ) : <EOL> def __init__ ( self , msg ) : <EOL> SPARQLError . __init__ ( self , msg ) <EOL> class Bindings ( MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outer = None , d = [ ] ) : <EOL> self . _d = dict ( d ) <EOL> self . outer = outer <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> return self . _d [ key ] <EOL> except KeyError : <EOL> if not self . outer : <EOL> raise <EOL> return self . outer [ key ] <EOL> def __contains__ ( self , key ) : <EOL> try : <EOL> self [ key ] <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _d [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def __len__ ( self ) : <EOL> i = <NUM_LIT:0> <EOL> for x in self : <EOL> i += <NUM_LIT:1> <EOL> return i <EOL> def __iter__ ( self ) : <EOL> d = self <EOL> while d is not None : <EOL> for i in dict . __iter__ ( d . _d ) : <EOL> yield i <EOL> d = d . outer <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( ( k , self [ k ] ) for k in self ) + "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return unicode ( self ) <EOL> class FrozenDict ( Mapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _d = dict ( * args , ** kwargs ) <EOL> self . _hash = None <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _d ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _d ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _d [ key ] <EOL> def __hash__ ( self ) : <EOL> if self . _hash is None : <EOL> self . _hash = <NUM_LIT:0> <EOL> for key , value in self . iteritems ( ) : <EOL> self . _hash ^= hash ( key ) <EOL> self . _hash ^= hash ( value ) <EOL> return self . _hash <EOL> def project ( self , vars ) : <EOL> return FrozenDict ( <EOL> ( x for x in self . iteritems ( ) if x [ <NUM_LIT:0> ] in vars ) ) <EOL> def disjointDomain ( self , other ) : <EOL> return not bool ( set ( self ) . intersection ( other ) ) <EOL> def compatible ( self , other ) : <EOL> for k in self : <EOL> try : <EOL> if self [ k ] != other [ k ] : <EOL> return False <EOL> except KeyError : <EOL> pass <EOL> return True <EOL> def merge ( self , other ) : <EOL> res = FrozenDict ( <EOL> itertools . chain ( self . iteritems ( ) , other . iteritems ( ) ) ) <EOL> return res <EOL> def __str__ ( self ) : <EOL> return str ( self . _d ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . _d ) <EOL> class FrozenBindings ( FrozenDict ) : <EOL> def __init__ ( self , ctx , * args , ** kwargs ) : <EOL> FrozenDict . __init__ ( self , * args , ** kwargs ) <EOL> self . ctx = ctx <EOL> def __getitem__ ( self , key ) : <EOL> if not isinstance ( key , Node ) : <EOL> key = Variable ( key ) <EOL> if not type ( key ) in ( BNode , Variable ) : <EOL> return key <EOL> return self . _d [ key ] <EOL> def project ( self , vars ) : <EOL> return FrozenBindings ( <EOL> self . ctx , ( x for x in self . iteritems ( ) if x [ <NUM_LIT:0> ] in vars ) ) <EOL> def merge ( self , other ) : <EOL> res = FrozenBindings ( <EOL> self . ctx , itertools . chain ( self . iteritems ( ) , other . iteritems ( ) ) ) <EOL> return res <EOL> def _now ( self ) : <EOL> return self . ctx . now <EOL> def _bnodes ( self ) : <EOL> return self . ctx . bnodes <EOL> def _prologue ( self ) : <EOL> return self . ctx . prologue <EOL> prologue = property ( _prologue ) <EOL> bnodes = property ( _bnodes ) <EOL> now = property ( _now ) <EOL> def forget ( self , before ) : <EOL> """<STR_LIT>""" <EOL> return FrozenBindings ( self . ctx , ( x for x in self . iteritems ( ) if before [ x [ <NUM_LIT:0> ] ] is None ) ) <EOL> def remember ( self , these ) : <EOL> """<STR_LIT>""" <EOL> return FrozenBindings ( self . ctx , ( x for x in self . iteritems ( ) if x [ <NUM_LIT:0> ] in these ) ) <EOL> class QueryContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph = None , bindings = None ) : <EOL> self . bindings = bindings or Bindings ( ) <EOL> if isinstance ( graph , ConjunctiveGraph ) : <EOL> self . _dataset = graph <EOL> if rdflib . plugins . sparql . SPARQL_DEFAULT_GRAPH_UNION : <EOL> self . graph = self . dataset <EOL> else : <EOL> self . graph = self . dataset . default_context <EOL> else : <EOL> self . _dataset = None <EOL> self . graph = graph <EOL> self . prologue = None <EOL> self . now = datetime . datetime . now ( ) <EOL> self . bnodes = collections . defaultdict ( BNode ) <EOL> def clone ( self , bindings = None ) : <EOL> r = QueryContext ( <EOL> self . _dataset if self . _dataset is not None else self . graph ) <EOL> r . prologue = self . prologue <EOL> r . bindings . update ( bindings or self . bindings ) <EOL> r . graph = self . graph <EOL> r . bnodes = self . bnodes <EOL> return r <EOL> def _get_dataset ( self ) : <EOL> if self . _dataset is None : <EOL> raise Exception ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> return self . _dataset <EOL> dataset = property ( _get_dataset , doc = "<STR_LIT>" ) <EOL> def load ( self , source , default = False , ** kwargs ) : <EOL> def _load ( graph , source ) : <EOL> try : <EOL> return graph . load ( source , ** kwargs ) <EOL> except : <EOL> pass <EOL> try : <EOL> return graph . load ( source , format = '<STR_LIT>' , ** kwargs ) <EOL> except : <EOL> pass <EOL> try : <EOL> return graph . load ( source , format = '<STR_LIT>' , ** kwargs ) <EOL> except : <EOL> raise Exception ( <EOL> "<STR_LIT>" % ( <EOL> source ) ) <EOL> if not rdflib . plugins . sparql . SPARQL_LOAD_GRAPHS : <EOL> if default : <EOL> self . graph += self . dataset . get_context ( source ) <EOL> else : <EOL> if default : <EOL> _load ( self . graph , source ) <EOL> else : <EOL> _load ( self . dataset , source ) <EOL> def __getitem__ ( self , key ) : <EOL> if not type ( key ) in ( BNode , Variable ) : <EOL> return key <EOL> try : <EOL> return self . bindings [ key ] <EOL> except KeyError : <EOL> return None <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return default <EOL> def solution ( self , vars = None ) : <EOL> """<STR_LIT>""" <EOL> if vars : <EOL> return FrozenBindings ( <EOL> self , ( ( k , v ) <EOL> for k , v in self . bindings . iteritems ( ) <EOL> if k in vars ) ) <EOL> else : <EOL> return FrozenBindings ( self , self . bindings . iteritems ( ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in self . bindings and self . bindings [ key ] != value : <EOL> raise AlreadyBound ( ) <EOL> self . bindings [ key ] = value <EOL> def pushGraph ( self , graph ) : <EOL> r = self . clone ( ) <EOL> r . graph = graph <EOL> return r <EOL> def push ( self ) : <EOL> r = self . clone ( Bindings ( self . bindings ) ) <EOL> return r <EOL> def clean ( self ) : <EOL> return self . clone ( [ ] ) <EOL> def thaw ( self , frozenbindings ) : <EOL> """<STR_LIT>""" <EOL> c = self . clone ( frozenbindings ) <EOL> return c <EOL> class Prologue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . base = None <EOL> self . namespace_manager = NamespaceManager ( <EOL> Graph ( ) ) <EOL> def resolvePName ( self , prefix , localname ) : <EOL> ns = self . namespace_manager . store . namespace ( prefix or "<STR_LIT>" ) <EOL> if ns is None : <EOL> raise Exception ( '<STR_LIT>' % prefix ) <EOL> return URIRef ( ns + ( localname or "<STR_LIT>" ) ) <EOL> def bind ( self , prefix , uri ) : <EOL> self . namespace_manager . bind ( prefix , uri , replace = True ) <EOL> def absolutize ( self , iri ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( iri , CompValue ) : <EOL> if iri . name == '<STR_LIT>' : <EOL> return self . resolvePName ( iri . prefix , iri . localname ) <EOL> if iri . name == '<STR_LIT>' : <EOL> return Literal ( <EOL> iri . string , lang = iri . lang , <EOL> datatype = self . absolutize ( iri . datatype ) ) <EOL> elif isinstance ( iri , URIRef ) and not '<STR_LIT::>' in iri : <EOL> return URIRef ( iri , base = self . base ) <EOL> return iri <EOL> class Query ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , prologue , algebra ) : <EOL> self . prologue = prologue <EOL> self . algebra = algebra </s>
