<s> from . . import exc as sa_exc <EOL> from . . util import ScopedRegistry , ThreadLocalRegistry , warn <EOL> from . import class_mapper , exc as orm_exc <EOL> from . session import Session <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class scoped_session ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session_factory , scopefunc = None ) : <EOL> """<STR_LIT>""" <EOL> self . session_factory = session_factory <EOL> if scopefunc : <EOL> self . registry = ScopedRegistry ( session_factory , scopefunc ) <EOL> else : <EOL> self . registry = ThreadLocalRegistry ( session_factory ) <EOL> def __call__ ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if kw : <EOL> scope = kw . pop ( '<STR_LIT>' , False ) <EOL> if scope is not None : <EOL> if self . registry . has ( ) : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> sess = self . session_factory ( ** kw ) <EOL> self . registry . set ( sess ) <EOL> return sess <EOL> else : <EOL> return self . session_factory ( ** kw ) <EOL> else : <EOL> return self . registry ( ) <EOL> def remove ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . registry . has ( ) : <EOL> self . registry ( ) . close ( ) <EOL> self . registry . clear ( ) <EOL> def configure ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . registry . has ( ) : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . session_factory . configure ( ** kwargs ) <EOL> def query_property ( self , query_cls = None ) : <EOL> """<STR_LIT>""" <EOL> class query ( object ) : <EOL> def __get__ ( s , instance , owner ) : <EOL> try : <EOL> mapper = class_mapper ( owner ) <EOL> if mapper : <EOL> if query_cls : <EOL> return query_cls ( mapper , session = self . registry ( ) ) <EOL> else : <EOL> return self . registry ( ) . query ( mapper ) <EOL> except orm_exc . UnmappedClassError : <EOL> return None <EOL> return query ( ) <EOL> ScopedSession = scoped_session <EOL> """<STR_LIT>""" <EOL> def instrument ( name ) : <EOL> def do ( self , * args , ** kwargs ) : <EOL> return getattr ( self . registry ( ) , name ) ( * args , ** kwargs ) <EOL> return do <EOL> for meth in Session . public_methods : <EOL> setattr ( scoped_session , meth , instrument ( meth ) ) <EOL> def makeprop ( name ) : <EOL> def set ( self , attr ) : <EOL> setattr ( self . registry ( ) , name , attr ) <EOL> def get ( self ) : <EOL> return getattr ( self . registry ( ) , name ) <EOL> return property ( get , set ) <EOL> for prop in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:info>' ) : <EOL> setattr ( scoped_session , prop , makeprop ( prop ) ) <EOL> def clslevel ( name ) : <EOL> def do ( cls , * args , ** kwargs ) : <EOL> return getattr ( Session , name ) ( * args , ** kwargs ) <EOL> return classmethod ( do ) <EOL> for prop in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> setattr ( scoped_session , prop , clslevel ( prop ) ) </s>
