<s> """<STR_LIT>""" <EOL> from ... orm . properties import ColumnProperty , RelationshipProperty , SynonymProperty <EOL> from ... schema import _get_table_key <EOL> from ... orm import class_mapper , interfaces <EOL> from ... import util <EOL> from ... import inspection <EOL> from ... import exc <EOL> import weakref <EOL> _registries = set ( ) <EOL> def add_class ( classname , cls ) : <EOL> """<STR_LIT>""" <EOL> if classname in cls . _decl_class_registry : <EOL> existing = cls . _decl_class_registry [ classname ] <EOL> if not isinstance ( existing , _MultipleClassMarker ) : <EOL> existing = cls . _decl_class_registry [ classname ] = _MultipleClassMarker ( [ cls , existing ] ) <EOL> else : <EOL> cls . _decl_class_registry [ classname ] = cls <EOL> try : <EOL> root_module = cls . _decl_class_registry [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> cls . _decl_class_registry [ '<STR_LIT>' ] = root_module = _ModuleMarker ( '<STR_LIT>' , None ) <EOL> tokens = cls . __module__ . split ( "<STR_LIT:.>" ) <EOL> while tokens : <EOL> token = tokens . pop ( <NUM_LIT:0> ) <EOL> module = root_module . get_module ( token ) <EOL> for token in tokens : <EOL> module = module . get_module ( token ) <EOL> module . add_class ( classname , cls ) <EOL> class _MultipleClassMarker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , classes , on_remove = None ) : <EOL> self . on_remove = on_remove <EOL> self . contents = set ( [ <EOL> weakref . ref ( item , self . _remove_item ) for item in classes ] ) <EOL> _registries . add ( self ) <EOL> def __iter__ ( self ) : <EOL> return ( ref ( ) for ref in self . contents ) <EOL> def attempt_get ( self , path , key ) : <EOL> if len ( self . contents ) > <NUM_LIT:1> : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( "<STR_LIT:.>" . join ( path + [ key ] ) ) <EOL> ) <EOL> else : <EOL> ref = list ( self . contents ) [ <NUM_LIT:0> ] <EOL> cls = ref ( ) <EOL> if cls is None : <EOL> raise NameError ( key ) <EOL> return cls <EOL> def _remove_item ( self , ref ) : <EOL> self . contents . remove ( ref ) <EOL> if not self . contents : <EOL> _registries . discard ( self ) <EOL> if self . on_remove : <EOL> self . on_remove ( ) <EOL> def add_item ( self , item ) : <EOL> modules = set ( [ <EOL> cls . __module__ for cls in <EOL> [ ref ( ) for ref in self . contents ] if cls is not None ] ) <EOL> if item . __module__ in modules : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> item . __module__ , <EOL> item . __name__ <EOL> ) <EOL> ) <EOL> self . contents . add ( weakref . ref ( item , self . _remove_item ) ) <EOL> class _ModuleMarker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , parent ) : <EOL> self . parent = parent <EOL> self . name = name <EOL> self . contents = { } <EOL> self . mod_ns = _ModNS ( self ) <EOL> if self . parent : <EOL> self . path = self . parent . path + [ self . name ] <EOL> else : <EOL> self . path = [ ] <EOL> _registries . add ( self ) <EOL> def __contains__ ( self , name ) : <EOL> return name in self . contents <EOL> def __getitem__ ( self , name ) : <EOL> return self . contents [ name ] <EOL> def _remove_item ( self , name ) : <EOL> self . contents . pop ( name , None ) <EOL> if not self . contents and self . parent is not None : <EOL> self . parent . _remove_item ( self . name ) <EOL> _registries . discard ( self ) <EOL> def resolve_attr ( self , key ) : <EOL> return getattr ( self . mod_ns , key ) <EOL> def get_module ( self , name ) : <EOL> if name not in self . contents : <EOL> marker = _ModuleMarker ( name , self ) <EOL> self . contents [ name ] = marker <EOL> else : <EOL> marker = self . contents [ name ] <EOL> return marker <EOL> def add_class ( self , name , cls ) : <EOL> if name in self . contents : <EOL> existing = self . contents [ name ] <EOL> existing . add_item ( cls ) <EOL> else : <EOL> existing = self . contents [ name ] = _MultipleClassMarker ( [ cls ] , <EOL> on_remove = lambda : self . _remove_item ( name ) ) <EOL> class _ModNS ( object ) : <EOL> def __init__ ( self , parent ) : <EOL> self . __parent = parent <EOL> def __getattr__ ( self , key ) : <EOL> try : <EOL> value = self . __parent . contents [ key ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> if value is not None : <EOL> if isinstance ( value , _ModuleMarker ) : <EOL> return value . mod_ns <EOL> else : <EOL> assert isinstance ( value , _MultipleClassMarker ) <EOL> return value . attempt_get ( self . __parent . path , key ) <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . __parent . name , key ) ) <EOL> class _GetColumns ( object ) : <EOL> def __init__ ( self , cls ) : <EOL> self . cls = cls <EOL> def __getattr__ ( self , key ) : <EOL> mp = class_mapper ( self . cls , configure = False ) <EOL> if mp : <EOL> if key not in mp . all_orm_descriptors : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> % ( self . cls , key ) ) <EOL> desc = mp . all_orm_descriptors [ key ] <EOL> if desc . extension_type is interfaces . NOT_EXTENSION : <EOL> prop = desc . property <EOL> if isinstance ( prop , SynonymProperty ) : <EOL> key = prop . name <EOL> elif not isinstance ( prop , ColumnProperty ) : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % key ) <EOL> return getattr ( self . cls , key ) <EOL> inspection . _inspects ( _GetColumns ) ( <EOL> lambda target : inspection . inspect ( target . cls ) ) <EOL> class _GetTable ( object ) : <EOL> def __init__ ( self , key , metadata ) : <EOL> self . key = key <EOL> self . metadata = metadata <EOL> def __getattr__ ( self , key ) : <EOL> return self . metadata . tables [ <EOL> _get_table_key ( key , self . key ) <EOL> ] <EOL> def _determine_container ( key , value ) : <EOL> if isinstance ( value , _MultipleClassMarker ) : <EOL> value = value . attempt_get ( [ ] , key ) <EOL> return _GetColumns ( value ) <EOL> class _class_resolver ( object ) : <EOL> def __init__ ( self , cls , prop , fallback , arg ) : <EOL> self . cls = cls <EOL> self . prop = prop <EOL> self . arg = self . _declarative_arg = arg <EOL> self . fallback = fallback <EOL> self . _dict = util . PopulateDict ( self . _access_cls ) <EOL> self . _resolvers = ( ) <EOL> def _access_cls ( self , key ) : <EOL> cls = self . cls <EOL> if key in cls . _decl_class_registry : <EOL> return _determine_container ( key , cls . _decl_class_registry [ key ] ) <EOL> elif key in cls . metadata . tables : <EOL> return cls . metadata . tables [ key ] <EOL> elif key in cls . metadata . _schemas : <EOL> return _GetTable ( key , cls . metadata ) <EOL> elif '<STR_LIT>' in cls . _decl_class_registry and key in cls . _decl_class_registry [ '<STR_LIT>' ] : <EOL> registry = cls . _decl_class_registry [ '<STR_LIT>' ] <EOL> return registry . resolve_attr ( key ) <EOL> elif self . _resolvers : <EOL> for resolv in self . _resolvers : <EOL> value = resolv ( key ) <EOL> if value is not None : <EOL> return value <EOL> return self . fallback [ key ] <EOL> def __call__ ( self ) : <EOL> try : <EOL> x = eval ( self . arg , globals ( ) , self . _dict ) <EOL> if isinstance ( x , _GetColumns ) : <EOL> return x . cls <EOL> else : <EOL> return x <EOL> except NameError as n : <EOL> raise exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . prop . parent , self . arg , n . args [ <NUM_LIT:0> ] , self . cls ) <EOL> ) <EOL> def _resolver ( cls , prop ) : <EOL> import sqlalchemy <EOL> from sqlalchemy . orm import foreign , remote <EOL> fallback = sqlalchemy . __dict__ . copy ( ) <EOL> fallback . update ( { '<STR_LIT>' : foreign , '<STR_LIT>' : remote } ) <EOL> def resolve_arg ( arg ) : <EOL> return _class_resolver ( cls , prop , fallback , arg ) <EOL> return resolve_arg <EOL> def _deferred_relationship ( cls , prop ) : <EOL> if isinstance ( prop , RelationshipProperty ) : <EOL> resolve_arg = _resolver ( cls , prop ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> v = getattr ( prop , attr ) <EOL> if isinstance ( v , util . string_types ) : <EOL> setattr ( prop , attr , resolve_arg ( v ) ) <EOL> if prop . backref and isinstance ( prop . backref , tuple ) : <EOL> key , kwargs = prop . backref <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if attr in kwargs and isinstance ( kwargs [ attr ] , str ) : <EOL> kwargs [ attr ] = resolve_arg ( kwargs [ attr ] ) <EOL> return prop </s>
