U kfS*@sUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlZddlmZddlmZejrddlmZddlmZedefZeeeeeffZeeeefZedddZGdddZ e dZ!e"e#Z$de%d<e defZ&e"e#Z'de%d<d d!d"d#d$Z(d%d&d'd(d)d*Z)d%d&d!d(d+d,Z*d&d&d-d!d.d/d0Z+d&d-d!d1d2d3Z,Gd4d5d5e eZ-dS)6aJProvides managed registration services on behalf of :func:`.listen` arguments. By "managed registration", we mean that event listening functions and other objects can be added to various collections in such a way that their membership in all those collections can be revoked at once, based on an equivalent :class:`._EventKey`. ) annotationsN)Any)Callable)cast)Deque)Dict)Generic)Iterable)Optional)Tuple)TypeVar)Union)exc)util) RefCollection) dispatcher._ET EventTarget)boundc@seZdZUdZdZded<dS)rzrepresents an event target, that is, something we can listen on either with that target as a class or as an instance. Examples include: Connection, Mapper, Table, Session, InstrumentedAttribute, Engine, Pool, Dialect. zdispatcher[Any]dispatchN)__name__ __module__ __qualname____doc__ __slots____annotations__rrrI/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/event/registry.pyr2s )zweakref.ref[RefCollection[Any]]weakref.ref[_ListenerFnType]z6Dict[_EventKeyTupleType, _RefCollectionToListenerType]_key_to_collectionr z>Dict[weakref.ref[RefCollection[Any]], _ListenerToEventKeyType]_collection_to_keyzweakref.ref[Any]None)refreturncCs^tr |tkrdStd|}t|}|D],}|tkr,t|}|||s,t|q,dS)Nz'weakref.ref[RefCollection[EventTarget]])r"rpopvaluesr!)r$listener_to_keykey dispatch_regrrr_collection_gcedgs     r+_EventKey[_ET]RefCollection[_ET]bool) event_keyownerr%cCsH|j}t|}|j}t|j}||kr,dS|||<t|}|||<dS)NFT)_keyr!r$weakref _listen_fnr")r/r0r)r* owner_ref listen_refr(rrr_stored_in_collectionxs r6cCsT|j}t|}t|j}|j}||d|s6t|=|tkrPt|}||dSN)r1r!r2r$r3r&r")r/r0r)r*r5r4r(rrr_removed_from_collections  r8zIterable[_ListenerFnType])newowneroldownerelementsr%c Cs|sdS|j}|j}t|}t|}|D]}t|}z ||} Wntk rZYq(YnXz t| } Wntk rYq(YnX|| kr| ||kstn|| |<| ||<q(dSr7)r$r"r2KeyErrorr!AssertionError) r9r:r;Z oldowner_refZ newowner_refZold_listener_to_keyZnew_listener_to_key listen_fnr5r)r*rrr_stored_in_collection_multis(     r?)r0r;r%cCsT|sdS|j}t|}|D]4}t|}||}t|}||d|st|=qdSr7)r$r"r2r!r&)r0r;r4r(r>r5r)r*rrr_clears  r@c@s eZdZUdZdZded<ded<ded<d ed <d ed <d ed<d7dddd d dddZeddddZdddddZ d ddddZ d d ddd d!Z ddd"d#Z d$dd%d&Z d8d$d$d$d(d$dd)d*d+Zeddd,d-Zd.d/d$d0d1d2Zd.d/dd0d3d4Zd.d/d$d0d5d6ZdS)9 _EventKeyz$Represent :func:`.listen` arguments.)target identifierfnfn_keyfn_wrapdispatch_targetrrBstrrC_ListenerFnTyperD_ListenerFnKeyTyperErrGzOptional[_ListenerFnType]_fn_wrapN)rBrCrDrGrKcCsP||_||_||_t|tjr6t|jt|jf|_ n t||_ ||_ ||_ dSr7) rBrCrD isinstancetypes MethodTypeid__func____self__rErFrG)selfrBrCrDrGrKrrr__init__s  z_EventKey.__init___EventKeyTupleType)r%cCst|j|j|jfSr7)rOrBrCrErRrrrr1sz_EventKey._keyr,)rFr%cCs,||jkr|St|j|j|j|j|dSdSN)rK)r3rArBrCrDrG)rRrFrrr with_wrappers z_EventKey.with_wrapper)rGr%cCs,||jkr|St|j|j|j||jdSdSrV)rGrArBrCrDrF)rRrGrrrwith_dispatch_target s z_EventKey.with_dispatch_targetr#)argskwr%c Os|dd}|dd}|dd}|j|j|j}}}t|j|} | ||} || }t|jjj|j} t | dr| |s|r|t j |j|dj ||n|jjj|f||dS)NonceFZ_once_unless_exceptionnamed_sa_warn)Zretry_on_exception)r&rGrCr3getattrrZ_adjust_fn_specrWZ_eventshasattrr]rZ only_oncelistenZ_listen) rRrYrZr[Zonce_unless_exceptionr\rBrCrDdispatch_collectionZ adjusted_fnZ stub_functionrrrr`s6        z_EventKey.listencCst|j}|tkr(td|j|j|jft|}|D]4\}}|}|}|dk r:|dk r:| | |q:dS)Nz*No listeners found for event %s / %r / %s ) r1r!rZInvalidRequestErrorrBrCrDr&itemsremoverW)rRr)r*Zcollection_refZ listener_refZ collectionZ listener_fnrrrrc8s z_EventKey.remover.cCs |jtkS)z6Return True if this event key is registered to listen.)r1r!rUrrrcontainsIsz_EventKey.containsFzOptional[bool]) propagateinsertr\retvalasyncior%c CsT|j|j}}t|j|}||j} |r2| |rD| ||n | ||dSr7)rGrCr^r for_modifyZ _set_asynciorfappend) rRrerfr\rgrhrBrCrarirrr base_listenMs  z_EventKey.base_listencCs |jp |jSr7)rFrDrUrrrr3bsz_EventKey._listen_fnr-zDeque[_ListenerFnType])r0list_r%cCs"t||r||jdSdSdSNTF)r6rjr3rRr0rlrrrappend_to_listfs  z_EventKey.append_to_listcCst||||jdSr7)r8rcr3rnrrrremove_from_listqs z_EventKey.remove_from_listcCs"t||r||jdSdSdSrm)r6 appendleftr3rnrrrprepend_to_listys  z_EventKey.prepend_to_list)N)FFFNF)rrrrrrrSpropertyr1rWrXr`rcrdrkr3rorprrrrrrrAs8      rA).r __future__r collectionsrMtypingrrrrrrr r r r r r2rr TYPE_CHECKINGattrrbaserrIintrJrHrTrrZ_RefCollectionToListenerType defaultdictdictr!rZ_ListenerToEventKeyTyper"r+r6r8r?r@rArrrrsX                    $