L i+PUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlZddlmZddlmZej*r ddlmZddlmZedefZeeeeeffZeeeefZeddZGddZ e dZ!ejDe#Z$de%d< e defZ&ejDe#Z'de%d< d'd Z( d(d!Z) d)d"Z* d*d#Z+ d+d$Z,Gd%d&e eZ-y),aJProvides 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 eZdZUdZdZded<y)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__r_/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/event/registry.pyrr2sIr!)zweakref.ref[RefCollection[Any]]weakref.ref[_ListenerFnType]z6Dict[_EventKeyTupleType, _RefCollectionToListenerType]_key_to_collectionr#z>Dict[weakref.ref[RefCollection[Any]], _ListenerToEventKeyType]_collection_to_keyctr|tvrytd|}tj|}|jD]=}|tvs t|}|j||r)tj|?y)Nz'weakref.ref[RefCollection[EventTarget]])r%rpopvaluesr$)reflistener_to_keykey dispatch_regs r"_collection_gcedr-gsw ,>!> 8# >C(,,S1O%%', $ $-c2L   S !"&&s+ ,r!c|j}t|}|j}tj|j}||vry|||<t |}|||<y)NFT)_keyr$r)weakref _listen_fnr%) event_keyownerr+r, owner_ref listen_refr*s r"_stored_in_collectionr6xs` ..C%c*L IY112JL (L(3O"%OJ r!c|j}t|}tj|j}|j}|j |d|st|=|t vrt |}|j |dyyN)r/r$r0r)r1r'r%)r2r3r+r,r5r4r*s r"_removed_from_collectionr9sy ..C%c*LY112J IY%  s #&&,Y7 J- 'r!c"|sy|j}|j}t|}t|}|D]?}tj|} ||} t| } || vr | ||k(sJ|| |<| ||<Ay#t$rYNwxYw#t$rY]wxYwr8)r)r%r0KeyErrorr$) newowneroldownerelements oldowner_ref newowner_refold_listener_to_keynew_listener_to_key listen_fnr5r+r,s r"_stored_in_collection_multirDs <A? B Bc|sy|j}t|}|D]A}tj|}||}t|}|j |d|r;t|=Cyr8)r)r%r0r$r')r3r>r4r*rCr5r+r,s r"_clearrFsi  I(3O( [[+ j))#. D)"3'(r!c"eZdZUdZdZded<ded<ded<d ed <d ed <d ed< d ddZeddZddZ d dZ d!dZ d"dZ d#dZ d$ d%dZed&dZ d'dZ d(dZ d'dZy)) _EventKeyz$Represent :func:`.listen` arguments.)target identifierfnfn_keyfn_wrapdispatch_targetrrIstrrJ_ListenerFnTyperK_ListenerFnKeyTyperLrrNOptional[_ListenerFnType]_fn_wrapNc||_||_||_t|tj r0t |jt |jf|_ nt ||_ ||_ ||_ yr8) rIrJrK isinstancetypes MethodTypeid__func____self__rLrMrN)selfrIrJrKrNrSs r"__init__z_EventKey.__init__s_ $ b%** +R[[/2bkk?:DKR&DK .r!cZt|j|j|jfSr8)rXrIrJrLr[s r"r/z_EventKey._keys4;;$++>>r!c||jur|St|j|j|j|j |SN)rS)r1rHrIrJrKrN)r[rMs r" with_wrapperz_EventKey.with_wrappersA doo %K $$  r!c||jur|St|j|j|j||j Sr`)rNrHrIrJrKrM)r[rNs r"with_dispatch_targetz_EventKey.with_dispatch_targetsA d22 2K   r!c|jdd}|jdd}|jdd}|j|j|j}}}t |j |} | j ||} |j| }t |jj j|j} t| dr| j|s|rA|jtj|j|j|i|y|jj j|g|i|y)NonceF_once_unless_exceptionnamed_sa_warn)retry_on_exception)r'rNrJr1getattrr_adjust_fn_specra_eventshasattrrhr only_oncelisten_listen) r[argskwreonce_unless_exceptionrgrIrJrKdispatch_collection adjusted_fn stub_functions r"roz_EventKey.listens6vvfe$ "'? Gw&  OO OO  &foozB)99"eD   -  ) ) 1 14??  =* -  " " $ ( D  OO8M fd  "!  " 2D ) ) 1 1$ D D Dr!c h|j}|tvr=tjd|jd|j d|j dtj|}|jD]9\}}|}|}|||j|j|;y)NzNo listeners found for event z /  ) r/r$rInvalidRequestErrorrIrJrKr'itemsremovera)r[r+r,collection_ref listener_ref collection listener_fns r"r{z_EventKey.remove<sii ( ());;:  *--c2 ,8,>,>,@ B (NL')J&.K%+*A!!$"3"3K"@A  Br!c&|jtvS)z6Return True if this event key is registered to listen.)r/r$r^s r"containsz_EventKey.containsMsyy...r!c|j|j}}t|j|}|j |j} |r| j |r| j ||y| j||yr8)rNrJrjr for_modify _set_asyncioinsertappend) r[ propagaterrgretvalasynciorIrJrtrs r" base_listenz_EventKey.base_listenQsn"114?? %foozB(33FOOD   # # %    dI .   dI .r!c6|jxs |jSr8)rMrKr^s r"r1z_EventKey._listen_fnfs||&tww&r!cTt||r|j|jyyNTF)r6rr1r[r3list_s r"append_to_listz_EventKey.append_to_listjs$ !u - LL )r!cRt|||j|jyr8)r9r{r1rs r"remove_from_listz_EventKey.remove_from_listus !u- T__%r!cTt||r|j|jyyr)r6 appendleftr1rs r"prepend_to_listz_EventKey.prepend_to_list}s& !u -   T__ -r!r8) rIrrJrOrKrPrNrrSrR)return_EventKeyTupleType)rMrPr_EventKey[_ET])rNrrr)rqrrrrrNone)rr)rbool)FFFNF) rrrrrgrrzOptional[bool]rrrr)rrP)r3RefCollection[_ET]rDeque[_ListenerFnType]rr)r3rrrrr)rrrrrr r\propertyr/rarcror{rrr1rrrrr!r"rHrHsa.I KO ''/3 ///  /  / , /$??  E@B"/  !% /// /  /  / /*'' ! &   &!&&&  & ! &   r!rH)r)zweakref.ref[Any]rr)r2rr3rrr)r2rr3rrr)r<rr=rr>Iterable[_ListenerFnType]rr)r3rr>rrr).r __future__r collectionsrVtypingrrrrrr r r r r rr0rr TYPE_CHECKINGattrrbaserrPintrQrOrrr_RefCollectionToListenerType defaultdictdictr$r _ListenerToEventKeyTyper%r-r6r9rDrFrHrr!r"rs#  # 38$3c3h/03%778e=)   $$  KD!J "KD!" ,"&8 *..&8. ..!. !. !.(!. !.H( ('( (&k kr!