o
    ;ha/                     @   sv  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
 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 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)ListOptional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   >/var/www/html/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                   @   s4   e Zd ZdZedefddZede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_asyncN)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&   3   s   zEventException.__init__)r   r   r   r   	Exceptionr   r&   __classcell__r   r   r'   r   r#   .   s    r#   c                   @   s0   e Zd Zdd ZdefddZdefddZdS )	EventDispatcherc                 C   s:   t t gtt gtt gtt gtt	 gt
t gi| _dS )zY
        Mapping should be extended for any new events or listeners to be added.
        N)AfterConnectionReleasedEventReAuthConnectionListener(AfterPooledConnectionsInstantiationEvent"RegisterReAuthForPooledConnections'AfterSingleConnectionInstantiationEvent!RegisterReAuthForSingleConnection'AfterPubSubConnectionInstantiationEventRegisterReAuthForPubSub#AfterAsyncClusterInstantiationEvent"RegisterReAuthForAsyncClusterNodes!AsyncAfterConnectionReleasedEventAsyncReAuthConnectionListener_event_listeners_mappingr   r   r   r   r&   ;   s   
zEventDispatcher.__init__r   c                 C   s(   | j t|}|D ]}|| q
d S r   r8   gettyper   r   r   Z	listenersZlistenerr   r   r   r!   P   s   zEventDispatcher.dispatchc                    s0   | j t|}|D ]
}||I d H  qd S r   r:   r=   r   r   r   r"   V   s
   zEventDispatcher.dispatch_asyncN)r   r   r   r&   r   r!   r"   r   r   r   r   r+   9   s    r+   c                   @   s$   e Zd ZdZdd Zedd ZdS )r,   zA
    Event that will be fired before each command execution.
    c                 C   s
   || _ d S r   _connection)r   
connectionr   r   r   r&   b      
z%AfterConnectionReleasedEvent.__init__c                 C      | j S r   r>   r9   r   r   r   r@   e      z'AfterConnectionReleasedEvent.connectionN)r   r   r   r   r&   propertyr@   r   r   r   r   r,   ]   s
    r,   c                   @   s   e Zd ZdS )r6   N)r   r   r   r   r   r   r   r6   j   s    r6   c                   @   s   e Zd ZdZdZdS )
ClientType)sync)asyncN)r   r   r   SYNCASYNCr   r   r   r   rE   n   s    rE   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   rJ   rK   rL   r   r   r   r&   x      
z1AfterPooledConnectionsInstantiationEvent.__init__c                 C   rB   r   )rN   r9   r   r   r   rJ      rC   z9AfterPooledConnectionsInstantiationEvent.connection_poolsreturnc                 C   rB   r   rO   r9   r   r   r   rK      rC   z4AfterPooledConnectionsInstantiationEvent.client_typec                 C   rB   r   rP   r9   r   r   r   rL      rC   z<AfterPooledConnectionsInstantiationEvent.credential_providerr   )r   r   r   r   r   rE   r   r	   r&   rD   rJ   rK   r   rL   r   r   r   r   r.   s   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 )r0   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
    rK   connection_lockc                 C   rM   r   )r?   rO   _connection_lock)r   r@   rK   rU   r   r   r   r&      rQ   z0AfterSingleConnectionInstantiationEvent.__init__c                 C   rB   r   r>   r9   r   r   r   r@      rC   z2AfterSingleConnectionInstantiationEvent.connectionrR   c                 C   rB   r   rS   r9   r   r   r   rK      rC   z3AfterSingleConnectionInstantiationEvent.client_typec                 C   rB   r   rV   r9   r   r   r   rU      rC   z7AfterSingleConnectionInstantiationEvent.connection_lockN)r   r   r   r   rE   r   	threadingLockasyncior&   rD   r@   rK   rU   r   r   r   r   r0      s    


 r0   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 )r2   rK   rU   c                 C   s   || _ || _|| _|| _d S r   )_pubsub_connection_connection_poolrO   rV   )r   pubsub_connectionconnection_poolrK   rU   r   r   r   r&      s   
z0AfterPubSubConnectionInstantiationEvent.__init__c                 C   rB   r   )r[   r9   r   r   r   r]      rC   z9AfterPubSubConnectionInstantiationEvent.pubsub_connectionc                 C   rB   r   )r\   r9   r   r   r   r^      rC   z7AfterPubSubConnectionInstantiationEvent.connection_poolrR   c                 C   rB   r   rS   r9   r   r   r   rK      rC   z3AfterPubSubConnectionInstantiationEvent.client_typec                 C   rB   r   rW   r9   r   r   r   rU      rC   z7AfterPubSubConnectionInstantiationEvent.connection_lockN)r   r   r   rE   r   rX   rY   rZ   r&   rD   r]   r^   rK   rU   r   r   r   r   r2      s    


 r2   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 )r4   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nodesrL   c                 C   s   || _ || _d S r   )_nodesrP   )r   r_   rL   r   r   r   r&      s   
z,AfterAsyncClusterInstantiationEvent.__init__rR   c                 C   rB   r   )r`   r9   r   r   r   r_      rC   z)AfterAsyncClusterInstantiationEvent.nodesc                 C   rB   r   rT   r9   r   r   r   rL      rC   z7AfterAsyncClusterInstantiationEvent.credential_providerr   )r   r   r   r   dictr   r	   r&   rD   r_   r   rL   r   r   r   r   r4      s    

r4   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@   Zre_authr   r   r   r   r      s   zReAuthConnectionListener.listenN)r   r   r   r   r,   r   r   r   r   r   r-          r-   c                   @   rb   )r7   zM
    Async listener that performs re-authentication of given connection.
    r   c                    s   |j  I d H  d S r   rc   r   r   r   r   r      s   z$AsyncReAuthConnectionListener.listenN)r   r   r   r   r6   r   r   r   r   r   r7      rd   r7   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   _eventr9   r   r   r   r&      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   )
isinstancerL   r
   rh   rK   rE   rH   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   rh   rJ   re_auth_callbackr   tokenpoolr   r   r   rk     s   z+RegisterReAuthForPooledConnections._re_authc                    s$   | j jD ]
}||I d H  qd S r   rp   rr   r   r   r   rn     s   z1RegisterReAuthForPooledConnections._re_auth_asyncerrorc                 C      t || jr   r#   rh   r   ru   r   r   r   rm        z2RegisterReAuthForPooledConnections._raise_on_errorc                       t || jr   rw   rx   r   r   r   ro        z8RegisterReAuthForPooledConnections._raise_on_error_asyncN)r   r   r   r   r&   r.   r   rk   rn   r)   rm   ro   r   r   r   r   r/      s    r/   c                   @   re   )r1   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   rf   r   rg   r9   r   r   r   r&      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   )ri   r@   rL   r
   rh   rK   rE   rH   rj   rk   rl   rm   rn   ro   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 NZAUTHoidrh   rU   r@   send_commandtry_get	get_valueread_responser   rs   r   r   r   rk   2  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   rn   9  s   
.z0RegisterReAuthForSingleConnection._re_auth_asyncru   c                 C   rv   r   rw   rx   r   r   r   rm   @  ry   z1RegisterReAuthForSingleConnection._raise_on_errorc                    rz   r   rw   rx   r   r   r   ro   C  r{   z7RegisterReAuthForSingleConnection._raise_on_error_asyncN)r   r   r   r   r&   r0   r   rk   rn   r)   rm   ro   r   r   r   r   r1     s    r1   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 )r5   c                 C   rf   r   rg   r9   r   r   r   r&   H  rA   z+RegisterReAuthForAsyncClusterNodes.__init__r   c                 C   s6   t |jtr|| _|j| j |j| j d S d S r   )ri   rL   r
   rh   rj   rk   rl   rm   r   r   r   r   r   K  s
   z)RegisterReAuthForAsyncClusterNodes.listenrs   c                    s,   | j jD ]}| j j| |I d H  qd S r   )rh   r_   rq   )r   rs   keyr   r   r   rk   Q  s   z+RegisterReAuthForAsyncClusterNodes._re_authru   c                    rz   r   rw   rx   r   r   r   rm   U  r{   z2RegisterReAuthForAsyncClusterNodes._raise_on_errorN)
r   r   r   r&   r4   r   r   rk   r)   rm   r   r   r   r   r5   G  s
    r5   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 )r3   c                 C   s"   d | _ d | _d | _d | _d | _d S r   )r?   r\   rO   rV   rh   r9   r   r   r   r&   Z  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)ri   r]   rL   r
   Zget_protocolrh   r?   r^   r\   rK   rO   rU   rV   rE   rH   rj   rk   rl   rm   rn   ro   r   r   r   r   r   a  s$   zRegisterReAuthForPubSub.listenrs   c                 C   sZ   | j  | jd|d|  | j  W d    n1 s w   Y  | j| d S r|   rV   r?   r   r   r   r   r\   rq   r   r   r   r   rk   t  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   rn   }  s   
(z&RegisterReAuthForPubSub._re_auth_asyncru   c                 C   rv   r   rw   rx   r   r   r   rm     ry   z'RegisterReAuthForPubSub._raise_on_errorc                    rz   r   rw   rx   r   r   r   ro     r{   z-RegisterReAuthForPubSub._raise_on_error_asyncN)r   r   r   r&   r2   r   r   rk   rn   r)   rm   ro   r   r   r   r   r3   Y  s    		r3   )#rZ   rX   abcr   r   enumr   typingr   r   r   Zredis.auth.tokenr   Zredis.credentialsr	   r
   r   r   r    r)   r#   r+   r,   r6   rE   r.   r0   r2   r4   r-   r7   r/   r1   r5   r3   r   r   r   r   <module>   s2    

$		#-