o
    gQ                     @  s  d Z ddlmZ ddlZddlmZ ddlZddlmZ ddl	Z	ddl	m
Z
 ddl	mZ ddl	mZ dd	l	mZ dd
l	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddl	mZ ddlZddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# dd l$m%Z% dd!l&m'Z' ed"e
d#Z(e	j)rdd$l*m+Z+ dd%l*m,Z, dd&l*m-Z- G d'd( d(e#j.ee Z/G d)d* d*ee( Z0eee( e0e( f Z1G d+d, d,e/e Z2G d-d. d.e/e ee! Z3G d/d0 d0e3e Z4G d1d2 d2e'Z5G d3d4 d4e3e Z6G d5d6 d6e6e Z7G d7d8 d8e6e Z8dS )9a  Attribute implementation for _Dispatch classes.

The various listener targets for a particular event class are represented
as attributes, which refer to collections of listeners to be fired off.
These collections can exist at the class level as well as at the instance
level.  An event is fired off using code like this::

    some_object.dispatch.first_connect(arg1, arg2)

Above, ``some_object.dispatch`` would be an instance of ``_Dispatch`` and
``first_connect`` is typically an instance of ``_ListenerCollection``
if event listeners are present, or ``_EmptyListener`` if none are present.

The attribute mechanics here spend effort trying to ensure listener functions
are available with a minimum of function call overhead, that unnecessary
objects aren't created (i.e. many empty per-instance listener collections),
as well as that everything is garbage collectable when owning references are
lost.  Other features such as "propagation" of listener functions across
many ``_Dispatch`` instances, "joining" of multiple ``_Dispatch`` instances,
as well as support for subclass propagation (e.g. events assigned to
``Pool`` vs. ``QueuePool``) are all implemented here.

    )annotationsN)chain)TracebackType)Any)cast)
Collection)Deque)	FrozenSet)Generic)Iterator)MutableMapping)MutableSequence)NoReturn)Optional)Sequence)Set)Tuple)Type)TypeVar)Union   )legacy)registry)_ET)	_EventKey)_ListenerFnType   )exc)util)AsyncAdaptedLock)Protocol_T)bound)	_Dispatch)_DispatchCommon)_HasEventsDispatchc                   @  s$   e Zd ZU dZded< dddZdS )	RefCollection)refweakref.ref[RefCollection[_ET]]r'   returnc                 C  s   t | tjS N)weakrefr'   r   _collection_gcedself r/   W/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/event/attr.py_memoized_attr_refP      z RefCollection._memoized_attr_refN)r)   r(   )__name__
__module____qualname__	__slots____annotations__r1   r/   r/   r/   r0   r&   K   s   
 r&   c                   @  s\   e Zd ZdddZdddZdddZdddZdddZdddZd ddZ	d!ddZ
dS )"_empty_collectionelementr!   r)   Nonec                 C     d S r*   r/   r.   r9   r/   r/   r0   appendU      z_empty_collection.appendc                 C  r;   r*   r/   r<   r/   r/   r0   
appendleftX   r>   z_empty_collection.appendleftotherSequence[_T]c                 C  r;   r*   r/   )r.   r@   r/   r/   r0   extend[   r>   z_empty_collection.extendc                 C  r;   r*   r/   r<   r/   r/   r0   remove^   r>   z_empty_collection.remover   boolc                 C     dS NFr/   r<   r/   r/   r0   __contains__a   r>   z_empty_collection.__contains__Iterator[_T]c                 C  s   t g S r*   )iterr-   r/   r/   r0   __iter__d      z_empty_collection.__iter__c                 C  r;   r*   r/   r-   r/   r/   r0   clearg   r>   z_empty_collection.clearintc                 C  rE   Nr   r/   r-   r/   r/   r0   __len__j   r>   z_empty_collection.__len__N)r9   r!   r)   r:   )r@   rA   r)   r:   )r9   r   r)   rD   )r)   rH   r)   r:   r)   rM   )r3   r4   r5   r=   r?   rB   rC   rG   rJ   rL   rO   r/   r/   r/   r0   r8   T   s    






r8   c                   @  s   e Zd ZU dZdZded< ded< ded< ded	< d
ed< ded< d3ddZd4ddZd5ddZd6ddZ	d7d!d"Z
d7d#d$Zd8d'd(Zd9d)d*Zd:d+d,Zd;d0d1Zd2S )<_ClsLevelDispatchz2Class-level events on :class:`._Dispatch` classes.)clsnamename	arg_nameshas_kwlegacy_signatures	_clslevel__weakref__strrS   rT   zSequence[str]rU   rD   rV   z,MutableSequence[legacy._LegacySignatureType]rW   zCMutableMapping[Type[_ET], _ListenerFnSequenceType[_ListenerFnType]]rX   parent_dispatch_clsType[_HasEventsDispatch[_ET]]fnr   c                 C  sv   |j | _|j | _t|}|jdd  | _t|j| _	t
ttt|dg dd d| _t| |||_t | _d S )Nr   _legacy_signaturesc                 S  s   | d S rN   r/   )sr/   r/   r0   <lambda>   s    z,_ClsLevelDispatch.__init__.<locals>.<lambda>)key)r3   rT   rS   r   inspect_getfullargspecargsrU   rD   varkwrV   listreversedsortedgetattrrW   r   _augment_fn_docs__doc__r+   WeakKeyDictionaryrX   )r.   r[   r]   argspecr/   r/   r0   __init__   s   
z_ClsLevelDispatch.__init__namedr)   c                 C  sN   |r|  |}| jr%z	tj|dd}W n
 ty   Y |S w t| ||}|S )NT)no_self)_wrap_fn_for_kwrW   r   get_callable_argspec	TypeErrorr   _wrap_fn_for_legacy)r.   r]   rn   rl   r/   r/   r0   _adjust_fn_spec   s   
z!_ClsLevelDispatch._adjust_fn_specc                   s   d fdd}|S )Nrc   r   kwr)   c                    s(   t tj| }||  di |S )Nr/   )dictziprU   update)rc   ru   argdictr]   r.   r/   r0   wrap_kw   s   
z2_ClsLevelDispatch._wrap_fn_for_kw.<locals>.wrap_kw)rc   r   ru   r   r)   r   r/   )r.   r]   r{   r/   rz   r0   rp      s   z!_ClsLevelDispatch._wrap_fn_for_kw	event_key_EventKey[_ET]	is_appendr:   c                 C  s   |j }t|tsJ dt|ddstd| dt|D ]0}||ur1|| jvr1| 	| q || jvr;| 	| |rG| j| 
|j q | j| |j q t||  d S )Nz*Class-level Event targets must be classes._sa_propagate_class_eventsTz&Can't assign an event directly to the z class)dispatch_target
isinstancetyperh   r   InvalidRequestErrorr   walk_subclassesrX   update_subclassr=   
_listen_fnr?   r   _stored_in_collection)r.   r|   r~   targetclsr/   r/   r0   _do_insert_or_append   s&   


z&_ClsLevelDispatch._do_insert_or_append	propagatec                 C     | j |dd d S )NFr~   r   r.   r|   r   r/   r/   r0   insert      z_ClsLevelDispatch.insertc                 C  r   )NTr   r   r   r/   r/   r0   r=      r   z_ClsLevelDispatch.appendr   	Type[_ET]c                   s|   || j vrt|ddrt | j |< nt | j |< | j |  |jdd  D ]}|| j v r;  fdd| j | D  q%d S )Nr   Tr   c                   s   g | ]}| vr|qS r/   r/   ).0r]   clslevelr/   r0   
<listcomp>   s    z5_ClsLevelDispatch.update_subclass.<locals>.<listcomp>)rX   rh   collectionsdequer8   __mro__rB   )r.   r   r   r/   r   r0   r      s   


z!_ClsLevelDispatch.update_subclassc                 C  sB   |j }t|D ]}|| jv r| j| |j qt||  d S r*   )r   r   r   rX   rC   r   r   _removed_from_collection)r.   r|   r   r   r/   r/   r0   rC      s   
z_ClsLevelDispatch.removec                 C  s8   t  }| j D ]}|| |  qt| | dS )zClear all class level listenersN)setrX   valuesrx   rL   r   _clear)r.   to_clear
dispatcherr/   r/   r0   rL      s
   

z_ClsLevelDispatch.clearobj_Dispatch[_ET]_ClsLevelDispatch[_ET]c                 C     | S zReturn an event collection which can be modified.

        For _ClsLevelDispatch at the class level of
        a dispatcher, this returns self.

        r/   r.   r   r/   r/   r0   
for_modify   s   z_ClsLevelDispatch.for_modifyN)r[   r\   r]   r   r]   r   rn   rD   r)   r   )r]   r   r)   r   )r|   r}   r~   rD   r)   r:   r|   r}   r   rD   r)   r:   )r   r   r)   r:   r|   r}   r)   r:   rP   )r   r   r)   r   )r3   r4   r5   rj   r6   r7   rm   rt   rp   r   r   r=   r   rC   rL   r   r/   r/   r/   r0   rR   q   s&   
 









	rR   c                   @  s   e Zd ZU dZded< d1d	d
Zd2ddZd3ddZd4ddZd5ddZ	d6ddZ
d6ddZd6ddZd6d d!Zd7d%d&Zd7d'd(Zd8d)d*Zd9d.d/Zd0S ):_InstanceLevelDispatchr/   r   parentr]   r   rn   rD   r)   c                 C     | j ||S r*   )r   rt   r.   r]   rn   r/   r/   r0   rt        z&_InstanceLevelDispatch._adjust_fn_specitemr   c                 C     t  r*   NotImplementedErrorr.   r   r/   r/   r0   rG        z#_InstanceLevelDispatch.__contains__rM   c                 C  r   r*   r   r-   r/   r/   r0   rO   	  r   z_InstanceLevelDispatch.__len__Iterator[_ListenerFnType]c                 C  r   r*   r   r-   r/   r/   r0   rJ     r   z_InstanceLevelDispatch.__iter__c                 C  r   r*   r   r-   r/   r/   r0   __bool__  r   z_InstanceLevelDispatch.__bool__rc   ru   r:   c                 O  r   r*   r   r.   rc   ru   r/   r/   r0   	exec_once  r   z _InstanceLevelDispatch.exec_oncec                 O  r   r*   r   r   r/   r/   r0   exec_once_unless_exception  r   z1_InstanceLevelDispatch.exec_once_unless_exceptionc                 O  r   r*   r   r   r/   r/   r0   _exec_w_sync_on_first_run  r   z0_InstanceLevelDispatch._exec_w_sync_on_first_runc                 O  r   r*   r   r   r/   r/   r0   __call__  r   z_InstanceLevelDispatch.__call__r|   r}   r   c                 C  r   r*   r   r   r/   r/   r0   r     r   z_InstanceLevelDispatch.insertc                 C  r   r*   r   r   r/   r/   r0   r=   !  r   z_InstanceLevelDispatch.appendc                 C  r   r*   r   r.   r|   r/   r/   r0   rC   $  r   z_InstanceLevelDispatch.remover   _DispatchCommon[_ET]_InstanceLevelDispatch[_ET]c                 C  r   r   r/   r   r/   r/   r0   r   '     	z!_InstanceLevelDispatch.for_modifyNr   r   r   r)   rD   rQ   r)   r   r)   rD   rc   r   ru   r   r)   r:   r   r   )r   r   r)   r   )r3   r4   r5   r6   r7   rt   rG   rO   rJ   r   r   r   r   r   r   r=   rC   r   r/   r/   r/   r0   r      s    
 











r   c                   @  s   e Zd ZU dZdZe Zded< dZded< ded	< d
ed< ded< d:ddZ	d;ddZ
d<ddZd<ddZd<d d!Zd<d"d#Zd<d$d%Zd<d&d'Zd<d(d)Zd=d+d,Zd>d/d0Zd?d2d3Zd@d5d6ZdAd7d8Zd9S )B_EmptyListenerzServes as a proxy interface to the events
    served by a _ClsLevelDispatch, when there are no
    instance-level events present.

    Is replaced by _ListenerCollection when instance-level
    events are added.

    )r   parent_listenersrT   zFrozenSet[_ListenerFnType]r   r/   z	Tuple[()]	listenersr   r   z(_ListenerFnSequenceType[_ListenerFnType]r   rZ   rT   
target_clsr   c                 C  s2   ||j vr
|| || _|j | | _|j| _d S r*   )rX   r   r   r   rT   r.   r   r   r/   r/   r0   rm   E  s
   

z_EmptyListener.__init__r   r   r)   _ListenerCollection[_ET]c                 C  sb   t d|}|jdusJ t| j|j}t|| j| u r$t|| j| |S tt|| jts/J |S )zReturn an event collection which can be modified.

        For _EmptyListener at the instance level of
        a dispatcher, this generates a new
        _ListenerCollection, applies it to the instance,
        and returns it.

        r   N)	r   _instance_cls_ListenerCollectionr   rh   rT   setattrr   _JoinedListener)r.   r   resultr/   r/   r0   r   L  s   
z_EmptyListener.for_modifyrc   r   ru   r   c                 O  s   t d)Nzneed to call for_modify()r   r   r/   r/   r0   _needs_modifya  rK   z_EmptyListener._needs_modifyc                 O     | j |i | d S r*   r   r   r/   r/   r0   r   d     z_EmptyListener.exec_oncec                 O  r   r*   r   r   r/   r/   r0   r   g  r   z)_EmptyListener.exec_once_unless_exceptionc                 O  r   r*   r   r   r/   r/   r0   r   j  r   z_EmptyListener.insertc                 O  r   r*   r   r   r/   r/   r0   r=   m  r   z_EmptyListener.appendc                 O  r   r*   r   r   r/   r/   r0   rC   p  r   z_EmptyListener.removec                 O  r   r*   r   r   r/   r/   r0   rL   s  r   z_EmptyListener.clearr:   c                 O  s   | j D ]	}||i | qdS zExecute this event.Nr   r.   rc   ru   r]   r/   r/   r0   r   v  s   
z_EmptyListener.__call__r   rD   c                 C  s
   || j v S r*   r   r   r/   r/   r0   rG   |     
z_EmptyListener.__contains__rM   c                 C  
   t | jS r*   )lenr   r-   r/   r/   r0   rO     r   z_EmptyListener.__len__r   c                 C  r   r*   )rI   r   r-   r/   r/   r0   rJ     r   z_EmptyListener.__iter__c                 C  r   r*   )rD   r   r-   r/   r/   r0   r     r   z_EmptyListener.__bool__Nr   r   r   r   r   r   r)   r   )rc   r   ru   r   r)   r   r   r   rQ   r   r   )r3   r4   r5   rj   r6   	frozensetr   r7   r   rm   r   r   r   r   r   r=   rC   rL   r   rG   rO   rJ   r   r/   r/   r/   r0   r   3  s,   
 	












r   c                   @  s    e Zd ZdddZdddZdS )_MutexProtocolr)   rD   c                 C  r;   r*   r/   r-   r/   r/   r0   	__enter__  s    z_MutexProtocol.__enter__exc_typeOptional[Type[BaseException]]exc_valOptional[BaseException]exc_tbOptional[TracebackType]Optional[bool]c                 C  r;   r*   r/   )r.   r   r   r   r/   r/   r0   __exit__  s   z_MutexProtocol.__exit__Nr   )r   r   r   r   r   r   r)   r   )r3   r4   r5   r   r   r/   r/   r/   r0   r     s    
r   c                      s   e Zd ZU dZded< ded< ded< ded< ded	< d, fddZd-ddZd.ddZd/ddZd0ddZ	d0ddZ
d0ddZd0dd Zd1d"d#Zd2d%d&Zd3d(d)Zd4d*d+Z  ZS )5_CompoundListener)_exec_once_mutex
_exec_once_exec_w_sync_once_is_asyncior   r   Collection[_ListenerFnType]r   r   rD   r   r   argr   ru   c                   s   t  j|i | d| _d S rF   )superrm   r   )r.   r   ru   	__class__r/   r0   rm     s   
z_CompoundListener.__init__r)   r:   c                 C  s
   d| _ d S )NT)r   r-   r/   r/   r0   _set_asyncio  r   z_CompoundListener._set_asyncioc                 C  s   | j rt S t S r*   )r   r   	threadingLockr-   r/   r/   r0   _memoized_attr__exec_once_mutex  s   z1_CompoundListener._memoized_attr__exec_once_mutexretry_on_exceptionrc   c              	   O  s   | j 8 | js+zz| |i | d}W n   d} W |r|s"d| _n|r'|s*d| _w W d    d S W d    d S 1 s>w   Y  d S )NFT)r   r   )r.   r   rc   ru   	exceptionr/   r/   r0   _exec_once_impl  s$   " z!_CompoundListener._exec_once_implc                 O  &   | j s| jdg|R i | dS dS )z]Execute this event, but only if it has not been
        executed already for this collection.FNr   r   r   r/   r/   r0   r     s   z_CompoundListener.exec_oncec                 O  r   )at  Execute this event, but only if it has not been
        executed already for this collection, or was called
        by a previous exec_once_unless_exception call and
        raised an exception.

        If exec_once was already called, then this method will never run
        the callable regardless of whether it raised or not.

        .. versionadded:: 1.3.8

        TNr   r   r/   r/   r0   r     s   z,_CompoundListener.exec_once_unless_exceptionc                 O  sf   | j s*| j z	| |i | W n    d| _ W d   dS 1 s#w   Y  dS | |i | dS )an  Execute this event, and use a mutex if it has not been
        executed already for this collection, or was called
        by a previous _exec_w_sync_on_first_run call and
        raised an exception.

        If _exec_w_sync_on_first_run was already called and didn't raise an
        exception, then a mutex is not used.

        .. versionadded:: 1.4.11

        TN)r   r   r   r/   r/   r0   r     s   "z+_CompoundListener._exec_w_sync_on_first_runc                 O  s8   | j D ]	}||i | q| jD ]	}||i | qdS r   r   r   r   r/   r/   r0   r     s
   

z_CompoundListener.__call__r   c                 C  s   || j v p	|| jv S r*   r   r   r/   r/   r0   rG     r   z_CompoundListener.__contains__rM   c                 C  s   t | jt | j S r*   )r   r   r   r-   r/   r/   r0   rO     r   z_CompoundListener.__len__r   c                 C     t | j| jS r*   )r   r   r   r-   r/   r/   r0   rJ     r2   z_CompoundListener.__iter__c                 C  s   t | jp| jS r*   )rD   r   r   r-   r/   r/   r0   r        z_CompoundListener.__bool__)r   r   ru   r   rP   )r)   r   )r   rD   rc   r   ru   r   r)   r:   r   r   rQ   r   r   )r3   r4   r5   r6   r7   rm   r   r   r   r   r   r   r   rG   rO   rJ   r   __classcell__r/   r/   r   r0   r     s&   
 









r   c                      s   e Zd ZU dZdZded< ded< ded< d	ed
< ded< d( fddZd)ddZ	d*d+ddZd,d d!Z	d,d"d#Z
d-d$d%Zd.d&d'Z  ZS )/r   zInstance-level attributes on instances of :class:`._Dispatch`.

    Represents a collection of listeners.

    As of 0.7.9, _ListenerCollection is only first
    created via the _EmptyListener.for_modify() method.

    )r   r   rT   r   r   rY   r   r   r   r   rZ   rT   zDeque[_ListenerFnType]r   zSet[_ListenerFnType]r   r   r   c                   sZ   t    ||jvr|| d| _d| _|j| | _|| _|j| _t	
 | _t | _d S rF   )r   rm   rX   r   r   r   r   r   rT   r   r   r   r   r   r   r   r/   r0   rm     s   



z_ListenerCollection.__init__r   r   r)   r   c                 C  r   )zReturn an event collection which can be modified.

        For _ListenerCollection at the instance level of
        a dispatcher, this returns self.

        r/   r   r/   r/   r0   r   %  r   z_ListenerCollection.for_modifyTr@   only_propagaterD   r:   c                   sj   j }t| j|j  fdd|j D }|| |jr&  |j|}t	|| dS )zIPopulate from the listeners in another :class:`_Dispatch`
        object.c                   s&   g | ]}| vr
r|j v r|qS r/   )r   )r   lexisting_listener_setr   r.   r/   r0   r   8  s    
z/_ListenerCollection._update.<locals>.<listcomp>N)
r   r   r   rx   rB   r   r   unionr   _stored_in_collection_multi)r.   r@   r   existing_listenersother_listenersto_associater/   r   r0   _update0  s   
z_ListenerCollection._updater|   r}   c                 C  ,   | | | jr|r| j|j d S d S d S r*   )prepend_to_listr   r   addr   r   r/   r/   r0   r   H  
   z_ListenerCollection.insertc                 C  r  r*   )append_to_listr   r   r	  r   r   r/   r/   r0   r=   M  r
  z_ListenerCollection.appendc                 C  s,   | j |j | j|j t||  d S r*   )r   rC   r   r   discardr   r   r   r/   r/   r0   rC   R  s   z_ListenerCollection.removec                 C  s&   t | | j | j  | j  d S r*   )r   r   r   r   rL   r-   r/   r/   r0   rL   W  s   
z_ListenerCollection.clearr   r   )T)r@   r   r   rD   r)   r:   r   r   rP   )r3   r4   r5   rj   r6   r7   rm   r   r  r   r=   rC   rL   r   r/   r/   r   r0   r      s    
 		



r   c                   @  s   e Zd ZU dZded< ded< ded< ded	< d'ddZejs&ed(ddZ	d)ddZ
d*ddZd+ddZd+d d!Zd,d"d#Zd-d$d%Zd&S ).r   )parent_dispatchrT   localr   r   r  rZ   rT   r   r  r   r   _EmptyListener[_ET]c                 C  s$   d| _ || _|| _|| _| j| _d S rF   )r   r  rT   r  r   )r.   r  rT   r  r/   r/   r0   rm   e  s
   z_JoinedListener.__init__r)   c                 C  r   r*   )rh   r  rT   r-   r/   r/   r0   r   x  s   z_JoinedListener.listenersr]   r   rn   rD   c                 C  r   r*   )r  rt   r   r/   r/   r0   rt   |  r   z_JoinedListener._adjust_fn_specr   _JoinedListener[_ET]c                 C  s   | j | | _ | _| S r*   )r  r   r   r   r/   r/   r0   r     s   z_JoinedListener.for_modifyr|   r}   r   r:   c                 C     | j || d S r*   )r  r   r   r/   r/   r0   r     r   z_JoinedListener.insertc                 C  r  r*   )r  r=   r   r/   r/   r0   r=     r   z_JoinedListener.appendc                 C  s   | j | d S r*   )r  rC   r   r/   r/   r0   rC     r   z_JoinedListener.removec                 C  r   r*   r   r-   r/   r/   r0   rL     r   z_JoinedListener.clearN)r  r   rT   rZ   r  r  )r)   r   r   )r   r   r)   r  r   r   rP   )r3   r4   r5   r6   r7   rm   typingTYPE_CHECKINGpropertyr   rt   r   r   r=   rC   rL   r/   r/   r/   r0   r   ]  s    
 





r   )9rj   
__future__r   r   	itertoolsr   r   typesr   r  r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r+    r   r   r   r   r   r   r   util.concurrencyr   util.typingr    r!   r  baser#   r$   r%   MemoizedSlotsr&   r8   _ListenerFnSequenceTyperR   r   r   r   r   r   r   r/   r/   r/   r0   <module>   sd   	 7Vl]