<s> """<STR_LIT>""" <EOL> from . . import inspection <EOL> from . . import util <EOL> from . . import exc <EOL> from itertools import chain <EOL> from . base import class_mapper <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> def _unreduce_path ( path ) : <EOL> return PathRegistry . deserialize ( path ) <EOL> _WILDCARD_TOKEN = "<STR_LIT:*>" <EOL> _DEFAULT_TOKEN = "<STR_LIT>" <EOL> class PathRegistry ( object ) : <EOL> """<STR_LIT>""" <EOL> is_token = False <EOL> is_root = False <EOL> def __eq__ ( self , other ) : <EOL> return other is not None and self . path == other . path <EOL> def set ( self , attributes , key , value ) : <EOL> log . debug ( "<STR_LIT>" , key , self , value ) <EOL> attributes [ ( key , self . path ) ] = value <EOL> def setdefault ( self , attributes , key , value ) : <EOL> log . debug ( "<STR_LIT>" , key , self , value ) <EOL> attributes . setdefault ( ( key , self . path ) , value ) <EOL> def get ( self , attributes , key , value = None ) : <EOL> key = ( key , self . path ) <EOL> if key in attributes : <EOL> return attributes [ key ] <EOL> else : <EOL> return value <EOL> def __len__ ( self ) : <EOL> return len ( self . path ) <EOL> @ property <EOL> def length ( self ) : <EOL> return len ( self . path ) <EOL> def pairs ( self ) : <EOL> path = self . path <EOL> for i in range ( <NUM_LIT:0> , len ( path ) , <NUM_LIT:2> ) : <EOL> yield path [ i ] , path [ i + <NUM_LIT:1> ] <EOL> def contains_mapper ( self , mapper ) : <EOL> for path_mapper in [ <EOL> self . path [ i ] for i in range ( <NUM_LIT:0> , len ( self . path ) , <NUM_LIT:2> ) <EOL> ] : <EOL> if path_mapper . is_mapper and path_mapper . isa ( mapper ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def contains ( self , attributes , key ) : <EOL> return ( key , self . path ) in attributes <EOL> def __reduce__ ( self ) : <EOL> return _unreduce_path , ( self . serialize ( ) , ) <EOL> def serialize ( self ) : <EOL> path = self . path <EOL> return list ( zip ( <EOL> [ m . class_ for m in [ path [ i ] for i in range ( <NUM_LIT:0> , len ( path ) , <NUM_LIT:2> ) ] ] , <EOL> [ path [ i ] . key for i in range ( <NUM_LIT:1> , len ( path ) , <NUM_LIT:2> ) ] + [ None ] <EOL> ) ) <EOL> @ classmethod <EOL> def deserialize ( cls , path ) : <EOL> if path is None : <EOL> return None <EOL> p = tuple ( chain ( * [ ( class_mapper ( mcls ) , <EOL> class_mapper ( mcls ) . attrs [ key ] <EOL> if key is not None else None ) <EOL> for mcls , key in path ] ) ) <EOL> if p and p [ - <NUM_LIT:1> ] is None : <EOL> p = p [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> return cls . coerce ( p ) <EOL> @ classmethod <EOL> def per_mapper ( cls , mapper ) : <EOL> return EntityRegistry ( <EOL> cls . root , mapper <EOL> ) <EOL> @ classmethod <EOL> def coerce ( cls , raw ) : <EOL> return util . reduce ( lambda prev , next : prev [ next ] , raw , cls . root ) <EOL> def token ( self , token ) : <EOL> if token . endswith ( '<STR_LIT::>' + _WILDCARD_TOKEN ) : <EOL> return TokenRegistry ( self , token ) <EOL> elif token . endswith ( "<STR_LIT::>" + _DEFAULT_TOKEN ) : <EOL> return TokenRegistry ( self . root , token ) <EOL> else : <EOL> raise exc . ArgumentError ( "<STR_LIT>" % token ) <EOL> def __add__ ( self , other ) : <EOL> return util . reduce ( <EOL> lambda prev , next : prev [ next ] , <EOL> other . path , self ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . path , ) <EOL> class RootRegistry ( PathRegistry ) : <EOL> """<STR_LIT>""" <EOL> path = ( ) <EOL> has_entity = False <EOL> is_aliased_class = False <EOL> is_root = True <EOL> def __getitem__ ( self , entity ) : <EOL> return entity . _path_registry <EOL> PathRegistry . root = RootRegistry ( ) <EOL> class TokenRegistry ( PathRegistry ) : <EOL> def __init__ ( self , parent , token ) : <EOL> self . token = token <EOL> self . parent = parent <EOL> self . path = parent . path + ( token , ) <EOL> has_entity = False <EOL> is_token = True <EOL> def generate_for_superclasses ( self ) : <EOL> if not self . parent . is_aliased_class and not self . parent . is_root : <EOL> for ent in self . parent . mapper . iterate_to_root ( ) : <EOL> yield TokenRegistry ( self . parent . parent [ ent ] , self . token ) <EOL> else : <EOL> yield self <EOL> def __getitem__ ( self , entity ) : <EOL> raise NotImplementedError ( ) <EOL> class PropRegistry ( PathRegistry ) : <EOL> def __init__ ( self , parent , prop ) : <EOL> insp = inspection . inspect ( parent [ - <NUM_LIT:1> ] ) <EOL> if not insp . is_aliased_class or insp . _use_mapper_path : <EOL> parent = parent . parent [ prop . parent ] <EOL> elif insp . is_aliased_class and insp . with_polymorphic_mappers : <EOL> if prop . parent is not insp . mapper and prop . parent in insp . with_polymorphic_mappers : <EOL> subclass_entity = parent [ - <NUM_LIT:1> ] . _entity_for_mapper ( prop . parent ) <EOL> parent = parent . parent [ subclass_entity ] <EOL> self . prop = prop <EOL> self . parent = parent <EOL> self . path = parent . path + ( prop , ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . join ( <EOL> str ( elem ) for elem in self . path <EOL> ) <EOL> @ util . memoized_property <EOL> def has_entity ( self ) : <EOL> return hasattr ( self . prop , "<STR_LIT>" ) <EOL> @ util . memoized_property <EOL> def entity ( self ) : <EOL> return self . prop . mapper <EOL> @ util . memoized_property <EOL> def _wildcard_path_loader_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT>" , <EOL> self . parent . token ( <EOL> "<STR_LIT>" % ( <EOL> self . prop . strategy_wildcard_key , _WILDCARD_TOKEN ) <EOL> ) . path <EOL> ) <EOL> @ util . memoized_property <EOL> def _default_path_loader_key ( self ) : <EOL> return ( "<STR_LIT>" , <EOL> self . parent . token ( <EOL> "<STR_LIT>" % ( self . prop . strategy_wildcard_key , <EOL> _DEFAULT_TOKEN ) <EOL> ) . path <EOL> ) <EOL> @ util . memoized_property <EOL> def _loader_key ( self ) : <EOL> return ( "<STR_LIT>" , self . path ) <EOL> @ property <EOL> def mapper ( self ) : <EOL> return self . entity <EOL> @ property <EOL> def entity_path ( self ) : <EOL> return self [ self . entity ] <EOL> def __getitem__ ( self , entity ) : <EOL> if isinstance ( entity , ( int , slice ) ) : <EOL> return self . path [ entity ] <EOL> else : <EOL> return EntityRegistry ( <EOL> self , entity <EOL> ) <EOL> class EntityRegistry ( PathRegistry , dict ) : <EOL> is_aliased_class = False <EOL> has_entity = True <EOL> def __init__ ( self , parent , entity ) : <EOL> self . key = entity <EOL> self . parent = parent <EOL> self . is_aliased_class = entity . is_aliased_class <EOL> self . entity = entity <EOL> self . path = parent . path + ( entity , ) <EOL> self . entity_path = self <EOL> @ property <EOL> def mapper ( self ) : <EOL> return inspection . inspect ( self . entity ) . mapper <EOL> def __bool__ ( self ) : <EOL> return True <EOL> __nonzero__ = __bool__ <EOL> def __getitem__ ( self , entity ) : <EOL> if isinstance ( entity , ( int , slice ) ) : <EOL> return self . path [ entity ] <EOL> else : <EOL> return dict . __getitem__ ( self , entity ) <EOL> def __missing__ ( self , key ) : <EOL> self [ key ] = item = PropRegistry ( self , key ) <EOL> return item </s>
