o
    LjC                     @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZmZ d dlmZmZ G d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd dZG dd deZG dd dZ G dd de Z!G dd deZ"G dd dZ#G dd  d Z$G d!d" d"Z%G d#d$ d$Z&G d%d& d&Z'G d'd( d(e'Z(G d)d* d*eZ)G d+d, d,eZ*G d-d. d.eZ+G d/d0 d0eZ,G d1d2 d2eZ-G d3d4 d4eZ.G d5d6 d6eZ/dS )7    N)ABCabstractmethod)Enum)DictListOptionalTypeUnion)TokenInterface)CredentialProviderStreamingCredentialProvider)init_connection_countregister_pools_connection_count)check_protocol_versiondeprecated_functionc                   @   "   e Zd ZdZedefddZdS )EventListenerInterfacez7
    Represents a listener for given event object.
    eventc                 C      d S N selfr   r   r   I/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/redis/event.pylisten      zEventListenerInterface.listenN__name__
__module____qualname____doc__r   objectr   r   r   r   r   r          r   c                   @   r   )AsyncEventListenerInterfacez>
    Represents an async listener for given event object.
    r   c                       d S r   r   r   r   r   r   r         z"AsyncEventListenerInterface.listenNr   r   r   r   r   r#      r"   r#   c                   @   s   e Zd ZdZedefddZedefddZedee	e e
eeef  f fdd	Zedee	e e
eeef  f fd
dZdS )EventDispatcherInterfacezf
    Represents a dispatcher that dispatches events to listeners
    associated with given event.
    r   c                 C   r   r   r   r   r   r   r   dispatch*   r   z!EventDispatcherInterface.dispatchc                    r$   r   r   r   r   r   r   dispatch_async.   r%   z'EventDispatcherInterface.dispatch_asyncmappingsc                 C      dS )zRegister additional listeners.Nr   r   r)   r   r   r   register_listeners2      	z+EventDispatcherInterface.register_listenersc                 C   r*   )z3Remove previously registered listeners by identity.Nr   r+   r   r   r   unregister_listeners=   r-   z-EventDispatcherInterface.unregister_listenersN)r   r   r   r    r   r!   r'   r(   r   r   r   r	   r   r#   r,   r.   r   r   r   r   r&   $   s,    
r&   c                       s*   e Zd ZdZdedef fddZ  ZS )EventExceptionzM
    Exception wrapper that adds an event object into exception context.
    	exceptionr   c                    s   || _ || _t | d S r   )r0   r   super__init__)r   r0   r   	__class__r   r   r2   N   s   zEventException.__init__)r   r   r   r    	Exceptionr!   r2   __classcell__r   r   r3   r   r/   I   s    r/   c                   @   s   e Zd Z	ddeeee ee f  fddZ	defddZ
defdd	Zd
eee eeeef  f fddZd
eee eeeef  f fddZdS )EventDispatcherNevent_listenersc                 C   s\   t t gtt gtt gtt gtt	 gt
t gi| _t | _d| _|r,| | dS dS )z]
        Dispatcher that dispatches events to listeners associated with given event.
        N)AfterConnectionReleasedEventReAuthConnectionListener(AfterPooledConnectionsInstantiationEvent"RegisterReAuthForPooledConnections'AfterSingleConnectionInstantiationEvent!RegisterReAuthForSingleConnection'AfterPubSubConnectionInstantiationEventRegisterReAuthForPubSub#AfterAsyncClusterInstantiationEvent"RegisterReAuthForAsyncClusterNodes!AsyncAfterConnectionReleasedEventAsyncReAuthConnectionListener_event_listeners_mapping	threadingRLock_lock_async_lockr,   )r   r8   r   r   r   r2   V   s*   
zEventDispatcher.__init__r   c                 C   sT   | j  t| jt|g }W d    n1 sw   Y  |D ]}|| q d S r   )rH   listrE   gettyper   r   r   	listenerslistenerr   r   r   r'   |   s   zEventDispatcher.dispatchc              	      s   | j d u rt | _ | j 4 I d H  t| jt|g }W d   I d H  n1 I d H s.w   Y  |D ]
}||I d H  q5d S r   )rI   asyncioLockrJ   rE   rK   rL   r   rM   r   r   r   r(      s   

(zEventDispatcher.dispatch_asyncr)   c                 C   sp   | j + |D ]}|| jv rtt| j| ||  | j|< q|| | j|< qW d    d S 1 s1w   Y  d S r   )rH   rE   rJ   set)r   r)   
event_typer   r   r   r,      s   
"z"EventDispatcher.register_listenersc                    sh   | j ' | D ]\} | j|}|sq fdd|D | j|< qW d    d S 1 s-w   Y  d S )Nc                    s&   g | ] t  fd dD r qS )c                 3   s    | ]} |uV  qd S r   r   ).0targetrO   r   r   	<genexpr>   s    zBEventDispatcher.unregister_listeners.<locals>.<listcomp>.<genexpr>)all)rT   	to_removerV   r   
<listcomp>   s    z8EventDispatcher.unregister_listeners.<locals>.<listcomp>)rH   itemsrE   rK   )r   r)   rS   currentr   rY   r   r.      s   
"z$EventDispatcher.unregister_listenersr   )r   r   r   r   r   r   r!   r   r   r2   r'   r(   r	   r#   r,   r.   r   r   r   r   r7   T   s.    
&

r7   c                   @   s$   e Zd ZdZdd Zedd ZdS )r9   zA
    Event that will be fired before each command execution.
    c                 C   s
   || _ d S r   _connection)r   
connectionr   r   r   r2         
z%AfterConnectionReleasedEvent.__init__c                 C      | j S r   r^   r   r   r   r   r`         z'AfterConnectionReleasedEvent.connectionN)r   r   r   r    r2   propertyr`   r   r   r   r   r9      s
    r9   c                   @      e Zd ZdS )rC   Nr   r   r   r   r   r   r   rC          rC   c                   @   s   e Zd ZdZdS )AfterSlotsCacheRefreshEventa  
    Event fired after NodesManager's slots cache is refreshed, either via a
    full re-initialization or a MOVED-driven slot re-mapping. Signal-only;
    carries no payload. Listeners typically reconcile per-node bookkeeping
    (e.g. ClusterPubSub shard subscriptions).
    N)r   r   r   r    r   r   r   r   ri      s    ri   c                   @   rf   ) AsyncAfterSlotsCacheRefreshEventNrg   r   r   r   r   rj      rh   rj   c                   @   s   e Zd ZdZdZdS )
ClientType)sync)asyncN)r   r   r   SYNCASYNCr   r   r   r   rk      s    rk   c                   @   sf   e Zd ZdZ	ddededee fddZe	dd	 Z
e	d
efddZe	d
eedf fddZdS )r;   zQ
    Event that will be fired after pooled connection instances was created.
    Nconnection_poolsclient_typecredential_providerc                 C      || _ || _|| _d S r   )_connection_pools_client_type_credential_provider)r   rp   rq   rr   r   r   r   r2         
z1AfterPooledConnectionsInstantiationEvent.__init__c                 C   rb   r   )rt   rc   r   r   r   rp      rd   z9AfterPooledConnectionsInstantiationEvent.connection_poolsreturnc                 C   rb   r   ru   rc   r   r   r   rq      rd   z4AfterPooledConnectionsInstantiationEvent.client_typec                 C   rb   r   rv   rc   r   r   r   rr      rd   z<AfterPooledConnectionsInstantiationEvent.credential_providerr   )r   r   r   r    r   rk   r   r   r2   re   rp   rq   r	   rr   r   r   r   r   r;      s     


r;   c                   @   sj   e Zd ZdZdedeejej	f fddZ
edd Zedefd	d
Zedeejej	f fddZdS )r=   z
    Event that will be fired after single connection instances was created.

    :param connection_lock: For sync client thread-lock should be provided,
    for async asyncio.Lock
    rq   connection_lockc                 C   rs   r   )r_   ru   _connection_lock)r   r`   rq   r{   r   r   r   r2     rw   z0AfterSingleConnectionInstantiationEvent.__init__c                 C   rb   r   r^   rc   r   r   r   r`     rd   z2AfterSingleConnectionInstantiationEvent.connectionrx   c                 C   rb   r   ry   rc   r   r   r   rq     rd   z3AfterSingleConnectionInstantiationEvent.client_typec                 C   rb   r   r|   rc   r   r   r   r{     rd   z7AfterSingleConnectionInstantiationEvent.connection_lockN)r   r   r   r    rk   r	   rF   rG   rP   rQ   r2   re   r`   rq   r{   r   r   r   r   r=      s    


 r=   c                   @   sr   e Zd Zdedeejejf fddZ	e
dd Ze
dd Ze
d	efd
dZe
d	eejejf fddZdS )r?   rq   r{   c                 C   s   || _ || _|| _|| _d S r   )_pubsub_connection_connection_poolru   r|   )r   pubsub_connectionconnection_poolrq   r{   r   r   r   r2     s   
z0AfterPubSubConnectionInstantiationEvent.__init__c                 C   rb   r   )r~   rc   r   r   r   r   '  rd   z9AfterPubSubConnectionInstantiationEvent.pubsub_connectionc                 C   rb   r   )r   rc   r   r   r   r   +  rd   z7AfterPubSubConnectionInstantiationEvent.connection_poolrx   c                 C   rb   r   ry   rc   r   r   r   rq   /  rd   z3AfterPubSubConnectionInstantiationEvent.client_typec                 C   rb   r   r}   rc   r   r   r   r{   3  rd   z7AfterPubSubConnectionInstantiationEvent.connection_lockN)r   r   r   rk   r	   rF   rG   rP   rQ   r2   re   r   r   rq   r{   r   r   r   r   r?     s    


 r?   c                   @   sV   e Zd ZdZ	ddedee fddZedefdd	Z	ede
edf fd
dZdS )rA   z
    Event that will be fired after async cluster instance was created.

    Async cluster doesn't use connection pools,
    instead ClusterNode object manages connections.
    Nnodesrr   c                 C      || _ || _d S r   )_nodesrv   )r   r   rr   r   r   r   r2   @     
z,AfterAsyncClusterInstantiationEvent.__init__rx   c                 C   rb   r   )r   rc   r   r   r   r   H  rd   z)AfterAsyncClusterInstantiationEvent.nodesc                 C   rb   r   rz   rc   r   r   r   rr   L  rd   z7AfterAsyncClusterInstantiationEvent.credential_providerr   )r   r   r   r    dictr   r   r2   re   r   r	   rr   r   r   r   r   rA   8  s    

rA   c                   @   sF   e Zd ZdZdedefddZedefddZedefd	d
Z	dS )OnCommandsFailEventzD
    Event fired whenever a command fails during the execution.
    commandsr0   c                 C   r   r   )	_commands
_exception)r   r   r0   r   r   r   r2   V  r   zOnCommandsFailEvent.__init__rx   c                 C   rb   r   )r   rc   r   r   r   r   ^  rd   zOnCommandsFailEvent.commandsc                 C   rb   r   )r   rc   r   r   r   r0   b  rd   zOnCommandsFailEvent.exceptionN)
r   r   r   r    tupler5   r2   re   r   r0   r   r   r   r   r   Q  s    
r   c                   @   rf   )AsyncOnCommandsFailEventNrg   r   r   r   r   r   g  rh   r   c                   @      e Zd ZdZdefddZdS )r:   zG
    Listener that performs re-authentication of given connection.
    r   c                 C   s   |j   d S r   r`   re_authr   r   r   r   r   p  s   zReAuthConnectionListener.listenN)r   r   r   r    r9   r   r   r   r   r   r:   k      r:   c                   @   r   )rD   zM
    Async listener that performs re-authentication of given connection.
    r   c                    s   |j  I d H  d S r   r   r   r   r   r   r   y  s   z$AsyncReAuthConnectionListener.listenN)r   r   r   r    rC   r   r   r   r   r   rD   t  r   rD   c                   @   R   e Zd ZdZdd ZdefddZdd Zd	d
 Zde	fddZ
de	fddZdS )r<   z
    Listener that registers a re-authentication callback for pooled connections.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   
   d | _ d S r   _eventrc   r   r   r   r2     ra   z+RegisterReAuthForPooledConnections.__init__r   c                 C   sb   t |jtr/|| _|jtjkr|j| j |j	| j
 d S |j| j |j	| j d S d S r   )
isinstancerr   r   r   rq   rk   rn   on_next_re_authon_error_raise_on_error_re_auth_async_raise_on_error_asyncr   r   r   r   r     s   z)RegisterReAuthForPooledConnections.listenc                 C   s   | j jD ]}|| qd S r   r   rp   re_auth_callbackr   tokenpoolr   r   r   r     s   z+RegisterReAuthForPooledConnections._re_authc                    s$   | j jD ]
}||I d H  qd S r   r   r   r   r   r   r     s   z1RegisterReAuthForPooledConnections._re_auth_asyncerrorc                 C      t || jr   r/   r   r   r   r   r   r   r        z2RegisterReAuthForPooledConnections._raise_on_errorc                       t || jr   r   r   r   r   r   r        z8RegisterReAuthForPooledConnections._raise_on_error_asyncN)r   r   r   r    r2   r;   r   r   r   r5   r   r   r   r   r   r   r<   }  s    r<   c                   @   r   )r>   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   r   r   r   rc   r   r   r   r2     ra   z*RegisterReAuthForSingleConnection.__init__r   c                 C   sl   t |jjtr4|| _|jtjkr"|jj| j	 |jj
| j d S |jj| j |jj
| j d S d S r   )r   r`   rr   r   r   rq   rk   rn   r   r   r   r   r   r   r   r   r   r   r     s   z(RegisterReAuthForSingleConnection.listenc                 C   sV   | j j | j jd|d|  | j j  W d    d S 1 s$w   Y  d S NAUTHoidr   r{   r`   send_commandtry_get	get_valueread_responser   r   r   r   r   r     s   
"z*RegisterReAuthForSingleConnection._re_authc              	      sx   | j j4 I d H & | j jd|d| I d H  | j j I d H  W d   I d H  d S 1 I d H s5w   Y  d S r   r   r   r   r   r   r     s   
.z0RegisterReAuthForSingleConnection._re_auth_asyncr   c                 C   r   r   r   r   r   r   r   r     r   z1RegisterReAuthForSingleConnection._raise_on_errorc                    r   r   r   r   r   r   r   r     r   z7RegisterReAuthForSingleConnection._raise_on_error_asyncN)r   r   r   r    r2   r=   r   r   r   r5   r   r   r   r   r   r   r>     s    r>   c                   @   s>   e Zd Zdd ZdefddZdefddZd	efd
dZ	dS )rB   c                 C   r   r   r   rc   r   r   r   r2     ra   z+RegisterReAuthForAsyncClusterNodes.__init__r   c                 C   s6   t |jtr|| _|j| j |j| j d S d S r   )r   rr   r   r   r   r   r   r   r   r   r   r   r     s
   z)RegisterReAuthForAsyncClusterNodes.listenr   c                    s,   | j jD ]}| j j| |I d H  qd S r   )r   r   r   )r   r   keyr   r   r   r     s   z+RegisterReAuthForAsyncClusterNodes._re_authr   c                    r   r   r   r   r   r   r   r     r   z2RegisterReAuthForAsyncClusterNodes._raise_on_errorN)
r   r   r   r2   rA   r   r
   r   r5   r   r   r   r   r   rB     s
    rB   c                   @   sZ   e Zd Zdd ZdefddZdefddZdefd	d
Zde	fddZ
de	fddZdS )r@   c                 C   s"   d | _ d | _d | _d | _d | _d S r   )r_   r   ru   r|   r   rc   r   r   r   r2     s
   
z RegisterReAuthForPubSub.__init__r   c                 C   s   t |jjtrLt|j drN|| _|j| _|j| _	|j
| _|j| _| jtjkr:| jj| j | jj| j d S | jj| j | jj| j d S d S d S )N   )r   r   rr   r   r   get_protocolr   r_   r   r   rq   ru   r{   r|   rk   rn   r   r   r   r   r   r   r   r   r   r   r     s$   zRegisterReAuthForPubSub.listenr   c                 C   sZ   | j  | jd|d|  | j  W d    n1 s w   Y  | j| d S r   r|   r_   r   r   r   r   r   r   r   r   r   r   r     s   z RegisterReAuthForPubSub._re_authc              	      s   | j 4 I d H # | jd|d| I d H  | j I d H  W d   I d H  n1 I d H s1w   Y  | j|I d H  d S r   r   r   r   r   r   r     s   
(z&RegisterReAuthForPubSub._re_auth_asyncr   c                 C   r   r   r   r   r   r   r   r     r   z'RegisterReAuthForPubSub._raise_on_errorc                    r   r   r   r   r   r   r   r     r   z-RegisterReAuthForPubSub._raise_on_error_asyncN)r   r   r   r2   r?   r   r
   r   r   r5   r   r   r   r   r   r   r@     s    		r@   c                   @   s*   e Zd ZdZeddddefddZdS )	&InitializeConnectionCountObservabilityzC
    Listener that initializes connection count observability.
    z{Connection count is now tracked via record_connection_count(). This functionality will be removed in the next major versionz7.4.0)reasonversionr   c                 C   s   t   t|j d S r   )r   r   rp   r   r   r   r   r     s   	z-InitializeConnectionCountObservability.listenN)r   r   r   r    r   r;   r   r   r   r   r   r     s    r   )0rP   rF   abcr   r   enumr   typingr   r   r   r   r	   redis.auth.tokenr
   redis.credentialsr   r   redis.observability.recorderr   r   redis.utilsr   r   r   r#   r&   r5   r/   r7   r9   rC   ri   rj   rk   r;   r=   r?   rA   r   r   r:   rD   r<   r>   rB   r@   r   r   r   r   r   <module>   s@    

%f		#-4