o
    /i<7                     @   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 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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)d* d*eZ$G d+d, d,eZ%G d-d. d.eZ&dS )/    N)ABCabstractmethod)Enum)DictListOptionalTypeUnion)TokenInterface)CredentialProviderStreamingCredentialProviderc                   @   "   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   K/var/www/html/flask_server/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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   s   dS )zRegister additional listeners.Nr   )r   r%   r   r   r   register_listeners-   s   	z+EventDispatcherInterface.register_listenersN)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   )r(   r   super__init__)r   r(   r   	__class__r   r   r*   >   s   zEventException.__init__)r   r   r   r   	Exceptionr   r*   __classcell__r   r   r+   r   r'   9   s    r'   c                   @   st   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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Lock_lock_async_lockr&   )r   r0   r   r   r   r*   F   s*   
zEventDispatcher.__init__r   c                 C   sR   | j  | jt|g }|D ]}|| qW d    d S 1 s"w   Y  d S r   )r@   r=   gettyper   r   r   	listenerslistenerr   r   r   r#   h   s   "zEventDispatcher.dispatchc              	      s   | j d u rt | _ | j 4 I d H " | jt|g }|D ]
}||I d H  qW d   I d H  d S 1 I d H s:w   Y  d S r   )rA   asyncior?   r=   rB   rC   r   rD   r   r   r   r$   o   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   )r@   r=   listset)r   r%   
event_typer   r   r   r&   y   s   
"z"EventDispatcher.register_listenersr   )r   r   r   r   r   r   r   r   r   r*   r#   r$   r	   r   r&   r   r   r   r   r/   D   s     
"
r/   c                   @   s$   e Zd ZdZdd Zedd ZdS )r1   zA
    Event that will be fired before each command execution.
    c                 C   s
   || _ d S r   _connection)r   
connectionr   r   r   r*         
z%AfterConnectionReleasedEvent.__init__c                 C      | j S r   rK   r   r   r   r   rM         z'AfterConnectionReleasedEvent.connectionN)r   r   r   r   r*   propertyrM   r   r   r   r   r1      s
    r1   c                   @      e Zd ZdS )r;   Nr   r   r   r   r   r   r   r;          r;   c                   @   s   e Zd ZdZdZdS )
ClientType)sync)asyncN)r   r   r   SYNCASYNCr   r   r   r   rV      s    rV   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 )r3   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   r[   r\   r]   r   r   r   r*         
z1AfterPooledConnectionsInstantiationEvent.__init__c                 C   rO   r   )r_   rP   r   r   r   r[      rQ   z9AfterPooledConnectionsInstantiationEvent.connection_poolsreturnc                 C   rO   r   r`   rP   r   r   r   r\      rQ   z4AfterPooledConnectionsInstantiationEvent.client_typec                 C   rO   r   ra   rP   r   r   r   r]      rQ   z<AfterPooledConnectionsInstantiationEvent.credential_providerr   )r   r   r   r   r   rV   r   r   r*   rR   r[   r\   r	   r]   r   r   r   r   r3      s     


r3   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 )r5   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
    r\   connection_lockc                 C   r^   r   )rL   r`   _connection_lock)r   rM   r\   rf   r   r   r   r*      rb   z0AfterSingleConnectionInstantiationEvent.__init__c                 C   rO   r   rK   rP   r   r   r   rM      rQ   z2AfterSingleConnectionInstantiationEvent.connectionrc   c                 C   rO   r   rd   rP   r   r   r   r\      rQ   z3AfterSingleConnectionInstantiationEvent.client_typec                 C   rO   r   rg   rP   r   r   r   rf      rQ   z7AfterSingleConnectionInstantiationEvent.connection_lockN)r   r   r   r   rV   r	   r>   RLockrG   r?   r*   rR   rM   r\   rf   r   r   r   r   r5      s    


 r5   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 )r7   r\   rf   c                 C   s   || _ || _|| _|| _d S r   )_pubsub_connection_connection_poolr`   rg   )r   pubsub_connectionconnection_poolr\   rf   r   r   r   r*      s   
z0AfterPubSubConnectionInstantiationEvent.__init__c                 C   rO   r   )rj   rP   r   r   r   rl      rQ   z9AfterPubSubConnectionInstantiationEvent.pubsub_connectionc                 C   rO   r   )rk   rP   r   r   r   rm      rQ   z7AfterPubSubConnectionInstantiationEvent.connection_poolrc   c                 C   rO   r   rd   rP   r   r   r   r\      rQ   z3AfterPubSubConnectionInstantiationEvent.client_typec                 C   rO   r   rh   rP   r   r   r   rf      rQ   z7AfterPubSubConnectionInstantiationEvent.connection_lockN)r   r   r   rV   r	   r>   ri   rG   r?   r*   rR   rl   rm   r\   rf   r   r   r   r   r7      s    


 r7   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 )r9   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nodesr]   c                 C      || _ || _d S r   )_nodesra   )r   rn   r]   r   r   r   r*        
z,AfterAsyncClusterInstantiationEvent.__init__rc   c                 C   rO   r   )rp   rP   r   r   r   rn     rQ   z)AfterAsyncClusterInstantiationEvent.nodesc                 C   rO   r   re   rP   r   r   r   r]     rQ   z7AfterAsyncClusterInstantiationEvent.credential_providerr   )r   r   r   r   dictr   r   r*   rR   rn   r	   r]   r   r   r   r   r9      s    

r9   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.
    commandsr(   c                 C   ro   r   )	_commands
_exception)r   rt   r(   r   r   r   r*     rq   zOnCommandsFailEvent.__init__rc   c                 C   rO   r   )ru   rP   r   r   r   rt   "  rQ   zOnCommandsFailEvent.commandsc                 C   rO   r   )rv   rP   r   r   r   r(   &  rQ   zOnCommandsFailEvent.exceptionN)
r   r   r   r   tupler-   r*   rR   rt   r(   r   r   r   r   rs     s    
rs   c                   @   rS   )AsyncOnCommandsFailEventNrT   r   r   r   r   rx   +  rU   rx   c                   @      e Zd ZdZdefddZdS )r2   zG
    Listener that performs re-authentication of given connection.
    r   c                 C   s   |j   d S r   rM   re_authr   r   r   r   r   4  s   zReAuthConnectionListener.listenN)r   r   r   r   r1   r   r   r   r   r   r2   /      r2   c                   @   ry   )r<   zM
    Async listener that performs re-authentication of given connection.
    r   c                    s   |j  I d H  d S r   rz   r   r   r   r   r   =  s   z$AsyncReAuthConnectionListener.listenN)r   r   r   r   r;   r   r   r   r   r   r<   8  r|   r<   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 )r4   z
    Listener that registers a re-authentication callback for pooled connections.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   
   d | _ d S r   _eventrP   r   r   r   r*   G  rN   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   )
isinstancer]   r   r   r\   rV   rY   on_next_re_authon_error_raise_on_error_re_auth_async_raise_on_error_asyncr   r   r   r   r   J  s   z)RegisterReAuthForPooledConnections.listenc                 C   s   | j jD ]}|| qd S r   r   r[   re_auth_callbackr   tokenpoolr   r   r   r   U  s   z+RegisterReAuthForPooledConnections._re_authc                    s$   | j jD ]
}||I d H  qd S r   r   r   r   r   r   r   Y  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   r*   r3   r   r   r   r-   r   r   r   r   r   r   r4   A  s    r4   c                   @   r}   )r6   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   r~   r   r   rP   r   r   r   r*   j  rN   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   rM   r]   r   r   r\   rV   rY   r   r   r   r   r   r   r   r   r   r   r   m  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   rf   rM   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   r*   r5   r   r   r   r-   r   r   r   r   r   r   r6   d  s    r6   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 )r:   c                 C   r~   r   r   rP   r   r   r   r*     rN   z+RegisterReAuthForAsyncClusterNodes.__init__r   c                 C   s6   t |jtr|| _|j| j |j| j d S d S r   )r   r]   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   rn   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   r*   r9   r   r
   r   r-   r   r   r   r   r   r:     s
    r:   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 )r8   c                 C   s"   d | _ d | _d | _d | _d | _d S r   )rL   rk   r`   rg   r   rP   r   r   r   r*     s
   
z RegisterReAuthForPubSub.__init__r   c                 C   s   t |jjtrK|j dv rM|| _|j| _|j| _|j	| _
|j| _| j
tjkr9| jj| j | jj| j d S | jj| j | jj| j d S d S d S )N)   3)r   rl   r]   r   get_protocolr   rL   rm   rk   r\   r`   rf   rg   rV   rY   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   rg   rL   r   r   r   r   rk   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   r*   r7   r   r
   r   r   r-   r   r   r   r   r   r   r8     s    		r8   )'rG   r>   abcr   r   enumr   typingr   r   r   r   r	   redis.auth.tokenr
   redis.credentialsr   r   r   r   r"   r-   r'   r/   r1   r;   rV   r3   r5   r7   r9   rs   rx   r2   r<   r4   r6   r:   r8   r   r   r   r   <module>   s6    

I		#-