<s> """<STR_LIT>""" <EOL> from . . import event , exc , util <EOL> from . base import _mapper_or_none <EOL> import inspect <EOL> import weakref <EOL> from . import interfaces <EOL> from . import mapperlib , instrumentation <EOL> from . session import Session , sessionmaker <EOL> from . scoping import scoped_session <EOL> from . attributes import QueryableAttribute <EOL> from . query import Query <EOL> from sqlalchemy . util . compat import inspect_getargspec <EOL> class InstrumentationEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = instrumentation . InstrumentationFactory <EOL> @ classmethod <EOL> def _accept_with ( cls , target ) : <EOL> if isinstance ( target , type ) : <EOL> return _InstrumentationEventsHold ( target ) <EOL> else : <EOL> return None <EOL> @ classmethod <EOL> def _listen ( cls , event_key , propagate = True , ** kw ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . _listen_fn <EOL> def listen ( target_cls , * arg ) : <EOL> listen_cls = target ( ) <EOL> if propagate and issubclass ( target_cls , listen_cls ) : <EOL> return fn ( target_cls , * arg ) <EOL> elif not propagate and target_cls is listen_cls : <EOL> return fn ( target_cls , * arg ) <EOL> def remove ( ref ) : <EOL> key = event . registry . _EventKey ( <EOL> None , identifier , listen , <EOL> instrumentation . _instrumentation_factory ) <EOL> getattr ( instrumentation . _instrumentation_factory . dispatch , <EOL> identifier ) . remove ( key ) <EOL> target = weakref . ref ( target . class_ , remove ) <EOL> event_key . with_dispatch_target ( instrumentation . _instrumentation_factory ) . with_wrapper ( listen ) . base_listen ( ** kw ) <EOL> @ classmethod <EOL> def _clear ( cls ) : <EOL> super ( InstrumentationEvents , cls ) . _clear ( ) <EOL> instrumentation . _instrumentation_factory . dispatch . _clear ( ) <EOL> def class_instrument ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> def class_uninstrument ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> def attribute_instrument ( self , cls , key , inst ) : <EOL> """<STR_LIT>""" <EOL> class _InstrumentationEventsHold ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , class_ ) : <EOL> self . class_ = class_ <EOL> dispatch = event . dispatcher ( InstrumentationEvents ) <EOL> class InstanceEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = instrumentation . ClassManager <EOL> @ classmethod <EOL> def _new_classmanager_instance ( cls , class_ , classmanager ) : <EOL> _InstanceEventsHold . populate ( class_ , classmanager ) <EOL> @ classmethod <EOL> @ util . dependencies ( "<STR_LIT>" ) <EOL> def _accept_with ( cls , orm , target ) : <EOL> if isinstance ( target , instrumentation . ClassManager ) : <EOL> return target <EOL> elif isinstance ( target , mapperlib . Mapper ) : <EOL> return target . class_manager <EOL> elif target is orm . mapper : <EOL> return instrumentation . ClassManager <EOL> elif isinstance ( target , type ) : <EOL> if issubclass ( target , mapperlib . Mapper ) : <EOL> return instrumentation . ClassManager <EOL> else : <EOL> manager = instrumentation . manager_of_class ( target ) <EOL> if manager : <EOL> return manager <EOL> else : <EOL> return _InstanceEventsHold ( target ) <EOL> return None <EOL> @ classmethod <EOL> def _listen ( cls , event_key , raw = False , propagate = False , ** kw ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . _listen_fn <EOL> if not raw : <EOL> def wrap ( state , * arg , ** kw ) : <EOL> return fn ( state . obj ( ) , * arg , ** kw ) <EOL> event_key = event_key . with_wrapper ( wrap ) <EOL> event_key . base_listen ( propagate = propagate , ** kw ) <EOL> if propagate : <EOL> for mgr in target . subclass_managers ( True ) : <EOL> event_key . with_dispatch_target ( mgr ) . base_listen ( <EOL> propagate = True ) <EOL> @ classmethod <EOL> def _clear ( cls ) : <EOL> super ( InstanceEvents , cls ) . _clear ( ) <EOL> _InstanceEventsHold . _clear ( ) <EOL> def first_init ( self , manager , cls ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , target , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> def init_failure ( self , target , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> def load ( self , target , context ) : <EOL> """<STR_LIT>""" <EOL> def refresh ( self , target , context , attrs ) : <EOL> """<STR_LIT>""" <EOL> def refresh_flush ( self , target , flush_context , attrs ) : <EOL> """<STR_LIT>""" <EOL> def expire ( self , target , attrs ) : <EOL> """<STR_LIT>""" <EOL> def pickle ( self , target , state_dict ) : <EOL> """<STR_LIT>""" <EOL> def unpickle ( self , target , state_dict ) : <EOL> """<STR_LIT>""" <EOL> class _EventsHold ( event . RefCollection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , class_ ) : <EOL> self . class_ = class_ <EOL> @ classmethod <EOL> def _clear ( cls ) : <EOL> cls . all_holds . clear ( ) <EOL> class HoldEvents ( object ) : <EOL> _dispatch_target = None <EOL> @ classmethod <EOL> def _listen ( cls , event_key , raw = False , propagate = False , ** kw ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . fn <EOL> if target . class_ in target . all_holds : <EOL> collection = target . all_holds [ target . class_ ] <EOL> else : <EOL> collection = target . all_holds [ target . class_ ] = { } <EOL> event . registry . _stored_in_collection ( event_key , target ) <EOL> collection [ event_key . _key ] = ( event_key , raw , propagate ) <EOL> if propagate : <EOL> stack = list ( target . class_ . __subclasses__ ( ) ) <EOL> while stack : <EOL> subclass = stack . pop ( <NUM_LIT:0> ) <EOL> stack . extend ( subclass . __subclasses__ ( ) ) <EOL> subject = target . resolve ( subclass ) <EOL> if subject is not None : <EOL> event_key . with_dispatch_target ( subject ) . listen ( raw = raw , propagate = False , ** kw ) <EOL> def remove ( self , event_key ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . fn <EOL> if isinstance ( target , _EventsHold ) : <EOL> collection = target . all_holds [ target . class_ ] <EOL> del collection [ event_key . _key ] <EOL> @ classmethod <EOL> def populate ( cls , class_ , subject ) : <EOL> for subclass in class_ . __mro__ : <EOL> if subclass in cls . all_holds : <EOL> collection = cls . all_holds [ subclass ] <EOL> for event_key , raw , propagate in collection . values ( ) : <EOL> if propagate or subclass is class_ : <EOL> event_key . with_dispatch_target ( subject ) . listen ( raw = raw , propagate = False ) <EOL> class _InstanceEventsHold ( _EventsHold ) : <EOL> all_holds = weakref . WeakKeyDictionary ( ) <EOL> def resolve ( self , class_ ) : <EOL> return instrumentation . manager_of_class ( class_ ) <EOL> class HoldInstanceEvents ( _EventsHold . HoldEvents , InstanceEvents ) : <EOL> pass <EOL> dispatch = event . dispatcher ( HoldInstanceEvents ) <EOL> class MapperEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = mapperlib . Mapper <EOL> @ classmethod <EOL> def _new_mapper_instance ( cls , class_ , mapper ) : <EOL> _MapperEventsHold . populate ( class_ , mapper ) <EOL> @ classmethod <EOL> @ util . dependencies ( "<STR_LIT>" ) <EOL> def _accept_with ( cls , orm , target ) : <EOL> if target is orm . mapper : <EOL> return mapperlib . Mapper <EOL> elif isinstance ( target , type ) : <EOL> if issubclass ( target , mapperlib . Mapper ) : <EOL> return target <EOL> else : <EOL> mapper = _mapper_or_none ( target ) <EOL> if mapper is not None : <EOL> return mapper <EOL> else : <EOL> return _MapperEventsHold ( target ) <EOL> else : <EOL> return target <EOL> @ classmethod <EOL> def _listen ( <EOL> cls , event_key , raw = False , retval = False , propagate = False , ** kw ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . _listen_fn <EOL> if identifier in ( "<STR_LIT>" , "<STR_LIT>" ) and target is not mapperlib . Mapper : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not raw or not retval : <EOL> if not raw : <EOL> meth = getattr ( cls , identifier ) <EOL> try : <EOL> target_index = inspect_getargspec ( meth ) [ <NUM_LIT:0> ] . index ( '<STR_LIT:target>' ) - <NUM_LIT:1> <EOL> except ValueError : <EOL> target_index = None <EOL> def wrap ( * arg , ** kw ) : <EOL> if not raw and target_index is not None : <EOL> arg = list ( arg ) <EOL> arg [ target_index ] = arg [ target_index ] . obj ( ) <EOL> if not retval : <EOL> fn ( * arg , ** kw ) <EOL> return interfaces . EXT_CONTINUE <EOL> else : <EOL> return fn ( * arg , ** kw ) <EOL> event_key = event_key . with_wrapper ( wrap ) <EOL> if propagate : <EOL> for mapper in target . self_and_descendants : <EOL> event_key . with_dispatch_target ( mapper ) . base_listen ( <EOL> propagate = True , ** kw ) <EOL> else : <EOL> event_key . base_listen ( ** kw ) <EOL> @ classmethod <EOL> def _clear ( cls ) : <EOL> super ( MapperEvents , cls ) . _clear ( ) <EOL> _MapperEventsHold . _clear ( ) <EOL> def instrument_class ( self , mapper , class_ ) : <EOL> """<STR_LIT>""" <EOL> def mapper_configured ( self , mapper , class_ ) : <EOL> """<STR_LIT>""" <EOL> def before_configured ( self ) : <EOL> """<STR_LIT>""" <EOL> def after_configured ( self ) : <EOL> """<STR_LIT>""" <EOL> def before_insert ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> def after_insert ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> def before_update ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> def after_update ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> def before_delete ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> def after_delete ( self , mapper , connection , target ) : <EOL> """<STR_LIT>""" <EOL> class _MapperEventsHold ( _EventsHold ) : <EOL> all_holds = weakref . WeakKeyDictionary ( ) <EOL> def resolve ( self , class_ ) : <EOL> return _mapper_or_none ( class_ ) <EOL> class HoldMapperEvents ( _EventsHold . HoldEvents , MapperEvents ) : <EOL> pass <EOL> dispatch = event . dispatcher ( HoldMapperEvents ) <EOL> class SessionEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = Session <EOL> @ classmethod <EOL> def _accept_with ( cls , target ) : <EOL> if isinstance ( target , scoped_session ) : <EOL> target = target . session_factory <EOL> if not isinstance ( target , sessionmaker ) and ( <EOL> not isinstance ( target , type ) or <EOL> not issubclass ( target , Session ) <EOL> ) : <EOL> raise exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if isinstance ( target , sessionmaker ) : <EOL> return target . class_ <EOL> elif isinstance ( target , type ) : <EOL> if issubclass ( target , scoped_session ) : <EOL> return Session <EOL> elif issubclass ( target , Session ) : <EOL> return target <EOL> elif isinstance ( target , Session ) : <EOL> return target <EOL> else : <EOL> return None <EOL> def after_transaction_create ( self , session , transaction ) : <EOL> """<STR_LIT>""" <EOL> def after_transaction_end ( self , session , transaction ) : <EOL> """<STR_LIT>""" <EOL> def before_commit ( self , session ) : <EOL> """<STR_LIT>""" <EOL> def after_commit ( self , session ) : <EOL> """<STR_LIT>""" <EOL> def after_rollback ( self , session ) : <EOL> """<STR_LIT>""" <EOL> def after_soft_rollback ( self , session , previous_transaction ) : <EOL> """<STR_LIT>""" <EOL> def before_flush ( self , session , flush_context , instances ) : <EOL> """<STR_LIT>""" <EOL> def after_flush ( self , session , flush_context ) : <EOL> """<STR_LIT>""" <EOL> def after_flush_postexec ( self , session , flush_context ) : <EOL> """<STR_LIT>""" <EOL> def after_begin ( self , session , transaction , connection ) : <EOL> """<STR_LIT>""" <EOL> def before_attach ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def after_attach ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> @ event . _legacy_signature ( "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:result>" ] , <EOL> lambda update_context : ( <EOL> update_context . session , <EOL> update_context . query , <EOL> update_context . context , <EOL> update_context . result ) ) <EOL> def after_bulk_update ( self , update_context ) : <EOL> """<STR_LIT>""" <EOL> @ event . _legacy_signature ( "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:result>" ] , <EOL> lambda delete_context : ( <EOL> delete_context . session , <EOL> delete_context . query , <EOL> delete_context . context , <EOL> delete_context . result ) ) <EOL> def after_bulk_delete ( self , delete_context ) : <EOL> """<STR_LIT>""" <EOL> def transient_to_pending ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def pending_to_transient ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def persistent_to_transient ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def pending_to_persistent ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def detached_to_persistent ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def loaded_as_persistent ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def persistent_to_deleted ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def deleted_to_persistent ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def deleted_to_detached ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> def persistent_to_detached ( self , session , instance ) : <EOL> """<STR_LIT>""" <EOL> class AttributeEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = QueryableAttribute <EOL> @ staticmethod <EOL> def _set_dispatch ( cls , dispatch_cls ) : <EOL> dispatch = event . Events . _set_dispatch ( cls , dispatch_cls ) <EOL> dispatch_cls . _active_history = False <EOL> return dispatch <EOL> @ classmethod <EOL> def _accept_with ( cls , target ) : <EOL> if isinstance ( target , interfaces . MapperProperty ) : <EOL> return getattr ( target . parent . class_ , target . key ) <EOL> else : <EOL> return target <EOL> @ classmethod <EOL> def _listen ( cls , event_key , active_history = False , <EOL> raw = False , retval = False , <EOL> propagate = False ) : <EOL> target , identifier , fn = event_key . dispatch_target , event_key . identifier , event_key . _listen_fn <EOL> if active_history : <EOL> target . dispatch . _active_history = True <EOL> if not raw or not retval : <EOL> def wrap ( target , value , * arg ) : <EOL> if not raw : <EOL> target = target . obj ( ) <EOL> if not retval : <EOL> fn ( target , value , * arg ) <EOL> return value <EOL> else : <EOL> return fn ( target , value , * arg ) <EOL> event_key = event_key . with_wrapper ( wrap ) <EOL> event_key . base_listen ( propagate = propagate ) <EOL> if propagate : <EOL> manager = instrumentation . manager_of_class ( target . class_ ) <EOL> for mgr in manager . subclass_managers ( True ) : <EOL> event_key . with_dispatch_target ( <EOL> mgr [ target . key ] ) . base_listen ( propagate = True ) <EOL> def append ( self , target , value , initiator ) : <EOL> """<STR_LIT>""" <EOL> def remove ( self , target , value , initiator ) : <EOL> """<STR_LIT>""" <EOL> def set ( self , target , value , oldvalue , initiator ) : <EOL> """<STR_LIT>""" <EOL> def init_collection ( self , target , collection , collection_adapter ) : <EOL> """<STR_LIT>""" <EOL> def dispose_collection ( self , target , collection , collection_adpater ) : <EOL> """<STR_LIT>""" <EOL> class QueryEvents ( event . Events ) : <EOL> """<STR_LIT>""" <EOL> _target_class_doc = "<STR_LIT>" <EOL> _dispatch_target = Query <EOL> def before_compile ( self , query ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def _listen ( <EOL> cls , event_key , retval = False , ** kw ) : <EOL> fn = event_key . _listen_fn <EOL> if not retval : <EOL> def wrap ( * arg , ** kw ) : <EOL> if not retval : <EOL> query = arg [ <NUM_LIT:0> ] <EOL> fn ( * arg , ** kw ) <EOL> return query <EOL> else : <EOL> return fn ( * arg , ** kw ) <EOL> event_key = event_key . with_wrapper ( wrap ) <EOL> event_key . base_listen ( ** kw ) </s>
