<s> """<STR_LIT:U+0020>""" <EOL> from . interfaces import MapperOption , PropComparator <EOL> from . . import util <EOL> from . . sql . base import _generative , Generative <EOL> from . . import exc as sa_exc , inspect <EOL> from . base import _is_aliased_class , _class_to_mapper <EOL> from . import util as orm_util <EOL> from . path_registry import PathRegistry , TokenRegistry , _WILDCARD_TOKEN , _DEFAULT_TOKEN <EOL> class Load ( Generative , MapperOption ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , entity ) : <EOL> insp = inspect ( entity ) <EOL> self . path = insp . _path_registry <EOL> self . context = { } <EOL> self . local_opts = { } <EOL> def _generate ( self ) : <EOL> cloned = super ( Load , self ) . _generate ( ) <EOL> cloned . local_opts = { } <EOL> return cloned <EOL> is_opts_only = False <EOL> strategy = None <EOL> propagate_to_loaders = False <EOL> def process_query ( self , query ) : <EOL> self . _process ( query , True ) <EOL> def process_query_conditionally ( self , query ) : <EOL> self . _process ( query , False ) <EOL> def _process ( self , query , raiseerr ) : <EOL> current_path = query . _current_path <EOL> if current_path : <EOL> for ( token , start_path ) , loader in self . context . items ( ) : <EOL> chopped_start_path = self . _chop_path ( start_path , current_path ) <EOL> if chopped_start_path is not None : <EOL> query . _attributes [ ( token , chopped_start_path ) ] = loader <EOL> else : <EOL> query . _attributes . update ( self . context ) <EOL> def _generate_path ( self , path , attr , wildcard_key , raiseerr = True ) : <EOL> if raiseerr and not path . has_entity : <EOL> if isinstance ( path , TokenRegistry ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( path . prop . key , path . parent . entity ) <EOL> ) <EOL> if isinstance ( attr , util . string_types ) : <EOL> default_token = attr . endswith ( _DEFAULT_TOKEN ) <EOL> if attr . endswith ( _WILDCARD_TOKEN ) or default_token : <EOL> if default_token : <EOL> self . propagate_to_loaders = False <EOL> if wildcard_key : <EOL> attr = "<STR_LIT>" % ( wildcard_key , attr ) <EOL> return path . token ( attr ) <EOL> try : <EOL> attr = getattr ( path . entity . class_ , attr ) <EOL> except AttributeError : <EOL> if raiseerr : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> attr , path . entity ) <EOL> ) <EOL> else : <EOL> return None <EOL> else : <EOL> attr = attr . property <EOL> path = path [ attr ] <EOL> else : <EOL> prop = attr . property <EOL> if not prop . parent . common_parent ( path . mapper ) : <EOL> if raiseerr : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( attr , path . entity ) ) <EOL> else : <EOL> return None <EOL> if getattr ( attr , '<STR_LIT>' , None ) : <EOL> ac = attr . _of_type <EOL> ext_info = inspect ( ac ) <EOL> path_element = ext_info . mapper <EOL> existing = path . entity_path [ prop ] . get ( <EOL> self . context , "<STR_LIT>" ) <EOL> if not ext_info . is_aliased_class : <EOL> ac = orm_util . with_polymorphic ( <EOL> ext_info . mapper . base_mapper , <EOL> ext_info . mapper , aliased = True , <EOL> _use_mapper_path = True , <EOL> _existing_alias = existing ) <EOL> path . entity_path [ prop ] . set ( <EOL> self . context , "<STR_LIT>" , inspect ( ac ) ) <EOL> path = path [ prop ] [ path_element ] <EOL> else : <EOL> path = path [ prop ] <EOL> if path . has_entity : <EOL> path = path . entity_path <EOL> return path <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . strategy , ) <EOL> def _coerce_strat ( self , strategy ) : <EOL> if strategy is not None : <EOL> strategy = tuple ( sorted ( strategy . items ( ) ) ) <EOL> return strategy <EOL> @ _generative <EOL> def set_relationship_strategy ( <EOL> self , attr , strategy , propagate_to_loaders = True ) : <EOL> strategy = self . _coerce_strat ( strategy ) <EOL> self . propagate_to_loaders = propagate_to_loaders <EOL> self . path = self . _generate_path ( self . path , attr , "<STR_LIT>" ) <EOL> self . strategy = strategy <EOL> if strategy is not None : <EOL> self . _set_path_strategy ( ) <EOL> @ _generative <EOL> def set_column_strategy ( self , attrs , strategy , opts = None , opts_only = False ) : <EOL> strategy = self . _coerce_strat ( strategy ) <EOL> for attr in attrs : <EOL> path = self . _generate_path ( self . path , attr , "<STR_LIT>" ) <EOL> cloned = self . _generate ( ) <EOL> cloned . strategy = strategy <EOL> cloned . path = path <EOL> cloned . propagate_to_loaders = True <EOL> if opts : <EOL> cloned . local_opts . update ( opts ) <EOL> if opts_only : <EOL> cloned . is_opts_only = True <EOL> cloned . _set_path_strategy ( ) <EOL> def _set_for_path ( self , context , path , replace = True , merge_opts = False ) : <EOL> if merge_opts or not replace : <EOL> existing = path . get ( self . context , "<STR_LIT>" ) <EOL> if existing : <EOL> if merge_opts : <EOL> existing . local_opts . update ( self . local_opts ) <EOL> else : <EOL> path . set ( context , "<STR_LIT>" , self ) <EOL> else : <EOL> existing = path . get ( self . context , "<STR_LIT>" ) <EOL> path . set ( context , "<STR_LIT>" , self ) <EOL> if existing and existing . is_opts_only : <EOL> self . local_opts . update ( existing . local_opts ) <EOL> def _set_path_strategy ( self ) : <EOL> if self . path . has_entity : <EOL> effective_path = self . path . parent <EOL> else : <EOL> effective_path = self . path <EOL> self . _set_for_path ( <EOL> self . context , effective_path , replace = True , <EOL> merge_opts = self . is_opts_only ) <EOL> def __getstate__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> d [ "<STR_LIT:path>" ] = self . path . serialize ( ) <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> self . path = PathRegistry . deserialize ( self . path ) <EOL> def _chop_path ( self , to_chop , path ) : <EOL> i = - <NUM_LIT:1> <EOL> for i , ( c_token , p_token ) in enumerate ( zip ( to_chop , path . path ) ) : <EOL> if isinstance ( c_token , util . string_types ) : <EOL> if i == <NUM_LIT:0> and c_token . endswith ( '<STR_LIT::>' + _DEFAULT_TOKEN ) : <EOL> return to_chop <EOL> elif c_token != '<STR_LIT>' % ( _WILDCARD_TOKEN , ) and c_token != p_token . key : <EOL> return None <EOL> if c_token is p_token : <EOL> continue <EOL> else : <EOL> return None <EOL> return to_chop [ i + <NUM_LIT:1> : ] <EOL> class _UnboundLoad ( Load ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . path = ( ) <EOL> self . _to_bind = set ( ) <EOL> self . local_opts = { } <EOL> _is_chain_link = False <EOL> def _set_path_strategy ( self ) : <EOL> self . _to_bind . add ( self ) <EOL> def _generate_path ( self , path , attr , wildcard_key ) : <EOL> if wildcard_key and isinstance ( attr , util . string_types ) and attr in ( _WILDCARD_TOKEN , _DEFAULT_TOKEN ) : <EOL> if attr == _DEFAULT_TOKEN : <EOL> self . propagate_to_loaders = False <EOL> attr = "<STR_LIT>" % ( wildcard_key , attr ) <EOL> return path + ( attr , ) <EOL> def __getstate__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> d [ '<STR_LIT:path>' ] = ret = [ ] <EOL> for token in util . to_list ( self . path ) : <EOL> if isinstance ( token , PropComparator ) : <EOL> ret . append ( ( token . _parentmapper . class_ , token . key ) ) <EOL> else : <EOL> ret . append ( token ) <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> ret = [ ] <EOL> for key in state [ '<STR_LIT:path>' ] : <EOL> if isinstance ( key , tuple ) : <EOL> cls , propkey = key <EOL> ret . append ( getattr ( cls , propkey ) ) <EOL> else : <EOL> ret . append ( key ) <EOL> state [ '<STR_LIT:path>' ] = tuple ( ret ) <EOL> self . __dict__ = state <EOL> def _process ( self , query , raiseerr ) : <EOL> for val in self . _to_bind : <EOL> val . _bind_loader ( query , query . _attributes , raiseerr ) <EOL> @ classmethod <EOL> def _from_keys ( cls , meth , keys , chained , kw ) : <EOL> opt = _UnboundLoad ( ) <EOL> def _split_key ( key ) : <EOL> if isinstance ( key , util . string_types ) : <EOL> if key == _WILDCARD_TOKEN : <EOL> return ( _DEFAULT_TOKEN , ) <EOL> elif key . startswith ( "<STR_LIT:.>" + _WILDCARD_TOKEN ) : <EOL> key = key [ <NUM_LIT:1> : ] <EOL> return key . split ( "<STR_LIT:.>" ) <EOL> else : <EOL> return ( key , ) <EOL> all_tokens = [ token for key in keys for token in _split_key ( key ) ] <EOL> for token in all_tokens [ <NUM_LIT:0> : - <NUM_LIT:1> ] : <EOL> if chained : <EOL> opt = meth ( opt , token , ** kw ) <EOL> else : <EOL> opt = opt . defaultload ( token ) <EOL> opt . _is_chain_link = True <EOL> opt = meth ( opt , all_tokens [ - <NUM_LIT:1> ] , ** kw ) <EOL> opt . _is_chain_link = False <EOL> return opt <EOL> def _chop_path ( self , to_chop , path ) : <EOL> i = - <NUM_LIT:1> <EOL> for i , ( c_token , ( p_mapper , p_prop ) ) in enumerate ( <EOL> zip ( to_chop , path . pairs ( ) ) ) : <EOL> if isinstance ( c_token , util . string_types ) : <EOL> if i == <NUM_LIT:0> and c_token . endswith ( '<STR_LIT::>' + _DEFAULT_TOKEN ) : <EOL> return to_chop <EOL> elif c_token != '<STR_LIT>' % ( <EOL> _WILDCARD_TOKEN , ) and c_token != p_prop . key : <EOL> return None <EOL> elif isinstance ( c_token , PropComparator ) : <EOL> if c_token . property is not p_prop : <EOL> return None <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> return to_chop [ i : ] <EOL> def _bind_loader ( self , query , context , raiseerr ) : <EOL> start_path = self . path <EOL> current_path = query . _current_path <EOL> if current_path : <EOL> start_path = self . _chop_path ( start_path , current_path ) <EOL> if not start_path : <EOL> return None <EOL> token = start_path [ <NUM_LIT:0> ] <EOL> if isinstance ( token , util . string_types ) : <EOL> entity = self . _find_entity_basestring ( query , token , raiseerr ) <EOL> elif isinstance ( token , PropComparator ) : <EOL> prop = token . property <EOL> entity = self . _find_entity_prop_comparator ( <EOL> query , <EOL> prop . key , <EOL> token . _parententity , <EOL> raiseerr ) <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not entity : <EOL> return <EOL> path_element = entity . entity_zero <EOL> loader = Load ( path_element ) <EOL> loader . context = context <EOL> loader . strategy = self . strategy <EOL> loader . is_opts_only = self . is_opts_only <EOL> path = loader . path <EOL> for token in start_path : <EOL> loader . path = path = loader . _generate_path ( <EOL> loader . path , token , None , raiseerr ) <EOL> if path is None : <EOL> return <EOL> loader . local_opts . update ( self . local_opts ) <EOL> if loader . path . has_entity : <EOL> effective_path = loader . path . parent <EOL> else : <EOL> effective_path = loader . path <EOL> if effective_path . is_token : <EOL> for path in effective_path . generate_for_superclasses ( ) : <EOL> loader . _set_for_path ( <EOL> context , path , <EOL> replace = not self . _is_chain_link , <EOL> merge_opts = self . is_opts_only ) <EOL> else : <EOL> loader . _set_for_path ( <EOL> context , effective_path , <EOL> replace = not self . _is_chain_link , <EOL> merge_opts = self . is_opts_only ) <EOL> def _find_entity_prop_comparator ( self , query , token , mapper , raiseerr ) : <EOL> if _is_aliased_class ( mapper ) : <EOL> searchfor = mapper <EOL> else : <EOL> searchfor = _class_to_mapper ( mapper ) <EOL> for ent in query . _mapper_entities : <EOL> if ent . corresponds_to ( searchfor ) : <EOL> return ent <EOL> else : <EOL> if raiseerr : <EOL> if not list ( query . _mapper_entities ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( token , ) <EOL> ) <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( token , "<STR_LIT:U+002C>" . join ( str ( x ) for <EOL> x in query . _mapper_entities ) ) <EOL> ) <EOL> else : <EOL> return None <EOL> def _find_entity_basestring ( self , query , token , raiseerr ) : <EOL> if token . endswith ( '<STR_LIT::>' + _WILDCARD_TOKEN ) : <EOL> if len ( list ( query . _mapper_entities ) ) != <NUM_LIT:1> : <EOL> if raiseerr : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif token . endswith ( _DEFAULT_TOKEN ) : <EOL> raiseerr = False <EOL> for ent in query . _mapper_entities : <EOL> return ent <EOL> else : <EOL> if raiseerr : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( token , ) <EOL> ) <EOL> else : <EOL> return None <EOL> class loader_option ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __call__ ( self , fn ) : <EOL> self . name = name = fn . __name__ <EOL> self . fn = fn <EOL> if hasattr ( Load , name ) : <EOL> raise TypeError ( "<STR_LIT>" % ( name ) ) <EOL> setattr ( Load , name , fn ) <EOL> return self <EOL> def _add_unbound_fn ( self , fn ) : <EOL> self . _unbound_fn = fn <EOL> fn_doc = self . fn . __doc__ <EOL> self . fn . __doc__ = """<STR_LIT>""" % { "<STR_LIT:name>" : self . name } <EOL> fn . __doc__ = fn_doc <EOL> return self <EOL> def _add_unbound_all_fn ( self , fn ) : <EOL> self . _unbound_all_fn = fn <EOL> fn . __doc__ = """<STR_LIT>""" % { "<STR_LIT:name>" : self . name } <EOL> return self <EOL> @ loader_option ( ) <EOL> def contains_eager ( loadopt , attr , alias = None ) : <EOL> """<STR_LIT>""" <EOL> if alias is not None : <EOL> if not isinstance ( alias , str ) : <EOL> info = inspect ( alias ) <EOL> alias = info . selectable <EOL> cloned = loadopt . set_relationship_strategy ( <EOL> attr , <EOL> { "<STR_LIT>" : "<STR_LIT>" } , <EOL> propagate_to_loaders = False <EOL> ) <EOL> cloned . local_opts [ '<STR_LIT>' ] = alias <EOL> return cloned <EOL> @ contains_eager . _add_unbound_fn <EOL> def contains_eager ( * keys , ** kw ) : <EOL> return _UnboundLoad ( ) . _from_keys ( <EOL> _UnboundLoad . contains_eager , keys , True , kw ) <EOL> @ loader_option ( ) <EOL> def load_only ( loadopt , * attrs ) : <EOL> """<STR_LIT>""" <EOL> cloned = loadopt . set_column_strategy ( <EOL> attrs , <EOL> { "<STR_LIT>" : False , "<STR_LIT>" : True } <EOL> ) <EOL> cloned . set_column_strategy ( "<STR_LIT:*>" , <EOL> { "<STR_LIT>" : True , "<STR_LIT>" : True } , <EOL> { "<STR_LIT>" : True } ) <EOL> return cloned <EOL> @ load_only . _add_unbound_fn <EOL> def load_only ( * attrs ) : <EOL> return _UnboundLoad ( ) . load_only ( * attrs ) <EOL> @ loader_option ( ) <EOL> def joinedload ( loadopt , attr , innerjoin = None ) : <EOL> """<STR_LIT>""" <EOL> loader = loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> if innerjoin is not None : <EOL> loader . local_opts [ '<STR_LIT>' ] = innerjoin <EOL> return loader <EOL> @ joinedload . _add_unbound_fn <EOL> def joinedload ( * keys , ** kw ) : <EOL> return _UnboundLoad . _from_keys ( <EOL> _UnboundLoad . joinedload , keys , False , kw ) <EOL> @ joinedload . _add_unbound_all_fn <EOL> def joinedload_all ( * keys , ** kw ) : <EOL> return _UnboundLoad . _from_keys ( <EOL> _UnboundLoad . joinedload , keys , True , kw ) <EOL> @ loader_option ( ) <EOL> def subqueryload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ subqueryload . _add_unbound_fn <EOL> def subqueryload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . subqueryload , keys , False , { } ) <EOL> @ subqueryload . _add_unbound_all_fn <EOL> def subqueryload_all ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . subqueryload , keys , True , { } ) <EOL> @ loader_option ( ) <EOL> def lazyload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ lazyload . _add_unbound_fn <EOL> def lazyload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . lazyload , keys , False , { } ) <EOL> @ lazyload . _add_unbound_all_fn <EOL> def lazyload_all ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . lazyload , keys , True , { } ) <EOL> @ loader_option ( ) <EOL> def immediateload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> loader = loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> return loader <EOL> @ immediateload . _add_unbound_fn <EOL> def immediateload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( <EOL> _UnboundLoad . immediateload , keys , False , { } ) <EOL> @ loader_option ( ) <EOL> def noload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ noload . _add_unbound_fn <EOL> def noload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . noload , keys , False , { } ) <EOL> @ loader_option ( ) <EOL> def raiseload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_relationship_strategy ( attr , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ raiseload . _add_unbound_fn <EOL> def raiseload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . raiseload , keys , False , { } ) <EOL> @ loader_option ( ) <EOL> def defaultload ( loadopt , attr ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_relationship_strategy ( <EOL> attr , <EOL> None <EOL> ) <EOL> @ defaultload . _add_unbound_fn <EOL> def defaultload ( * keys ) : <EOL> return _UnboundLoad . _from_keys ( _UnboundLoad . defaultload , keys , False , { } ) <EOL> @ loader_option ( ) <EOL> def defer ( loadopt , key ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_column_strategy ( <EOL> ( key , ) , <EOL> { "<STR_LIT>" : True , "<STR_LIT>" : True } <EOL> ) <EOL> @ defer . _add_unbound_fn <EOL> def defer ( key , * addl_attrs ) : <EOL> return _UnboundLoad . _from_keys ( <EOL> _UnboundLoad . defer , ( key , ) + addl_attrs , False , { } ) <EOL> @ loader_option ( ) <EOL> def undefer ( loadopt , key ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_column_strategy ( <EOL> ( key , ) , <EOL> { "<STR_LIT>" : False , "<STR_LIT>" : True } <EOL> ) <EOL> @ undefer . _add_unbound_fn <EOL> def undefer ( key , * addl_attrs ) : <EOL> return _UnboundLoad . _from_keys ( <EOL> _UnboundLoad . undefer , ( key , ) + addl_attrs , False , { } ) <EOL> @ loader_option ( ) <EOL> def undefer_group ( loadopt , name ) : <EOL> """<STR_LIT>""" <EOL> return loadopt . set_column_strategy ( <EOL> "<STR_LIT:*>" , <EOL> None , <EOL> { "<STR_LIT>" % name : True } , <EOL> opts_only = True <EOL> ) <EOL> @ undefer_group . _add_unbound_fn <EOL> def undefer_group ( name ) : <EOL> return _UnboundLoad ( ) . undefer_group ( name ) </s>
