U
    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                   @   s"   e Zd ZdZeedddZdS )EventListenerInterfacez7
    Represents a listener for given event object.
    eventc                 C   s   d S N selfr   r   r   //tmp/pip-unpacked-wheel-4avdqdik/redis/event.pylisten   s    zEventListenerInterface.listenN__name__
__module____qualname____doc__r   objectr   r   r   r   r   r      s   r   c                   @   s"   e Zd ZdZeedddZdS )AsyncEventListenerInterfacez>
    Represents an async listener for given event object.
    r   c                    s   d S r   r   r   r   r   r   r      s    z"AsyncEventListenerInterface.listenNr   r   r   r   r   r      s   r   c                   @   s4   e Zd ZdZeedddZeedddZdS )EventDispatcherInterfacezf
    Represents a dispatcher that dispatches events to listeners
    associated with given event.
    r   c                 C   s   d S r   r   r   r   r   r   dispatch%   s    z!EventDispatcherInterface.dispatchc                    s   d S r   r   r   r   r   r   dispatch_async)   s    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eed 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edddZed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   r3   gettyper   r   r   Z	listenersZlistenerr   r   r   r   P   s    zEventDispatcher.dispatchc                    s.   | j t|}|D ]}||I d H  qd S r   r5   r8   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   s    z%AfterConnectionReleasedEvent.__init__c                 C   s   | j S r   r9   r4   r   r   r   r;   e   s    z'AfterConnectionReleasedEvent.connectionN)r   r   r   r   r!   propertyr;   r   r   r   r   r'   ]   s   r'   c                   @   s   e Zd ZdS )r1   N)r   r   r   r   r   r   r   r1   j   s   r1   c                   @   s   e Zd ZdZdZdS )
ClientType)sync)asyncN)r   r   r   SYNCASYNCr   r   r   r   r=   n   s   r=   c                   @   s`   e Zd ZdZdeeee dddZe	dd Z
e	edd	d
Ze	eedf d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   s   || _ || _|| _d S r   )_connection_pools_client_type_credential_provider)r   rB   rC   rD   r   r   r   r!   x   s    z1AfterPooledConnectionsInstantiationEvent.__init__c                 C   s   | j S r   )rE   r4   r   r   r   rB      s    z9AfterPooledConnectionsInstantiationEvent.connection_poolsreturnc                 C   s   | j S r   rF   r4   r   r   r   rC      s    z4AfterPooledConnectionsInstantiationEvent.client_typec                 C   s   | j S r   rG   r4   r   r   r   rD      s    z<AfterPooledConnectionsInstantiationEvent.credential_provider)N)r   r   r   r   r   r=   r   r	   r!   r<   rB   rC   r   rD   r   r   r   r   r)   s   s    

r)   c                   @   sh   e Zd ZdZeeejejf dddZ	e
dd Ze
eddd	Ze
eejejf d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
    rC   connection_lockc                 C   s   || _ || _|| _d S r   )r:   rF   _connection_lock)r   r;   rC   rM   r   r   r   r!      s    z0AfterSingleConnectionInstantiationEvent.__init__c                 C   s   | j S r   r9   r4   r   r   r   r;      s    z2AfterSingleConnectionInstantiationEvent.connectionrH   c                 C   s   | j S r   rJ   r4   r   r   r   rC      s    z3AfterSingleConnectionInstantiationEvent.client_typec                 C   s   | j S r   rN   r4   r   r   r   rM      s    z7AfterSingleConnectionInstantiationEvent.connection_lockN)r   r   r   r   r=   r   	threadingLockasyncior!   r<   r;   rC   rM   r   r   r   r   r+      s   


r+   c                   @   sp   e Zd Zeeejejf dddZe	dd Z
e	dd Ze	edd	d
Ze	eejejf dddZdS )r-   rL   c                 C   s   || _ || _|| _|| _d S r   )_pubsub_connection_connection_poolrF   rN   )r   pubsub_connectionconnection_poolrC   rM   r   r   r   r!      s    z0AfterPubSubConnectionInstantiationEvent.__init__c                 C   s   | j S r   )rS   r4   r   r   r   rU      s    z9AfterPubSubConnectionInstantiationEvent.pubsub_connectionc                 C   s   | j S r   )rT   r4   r   r   r   rV      s    z7AfterPubSubConnectionInstantiationEvent.connection_poolrH   c                 C   s   | j S r   rJ   r4   r   r   r   rC      s    z3AfterPubSubConnectionInstantiationEvent.client_typec                 C   s   | j S r   rO   r4   r   r   r   rM      s    z7AfterPubSubConnectionInstantiationEvent.connection_lockN)r   r   r   r=   r   rP   rQ   rR   r!   r<   rU   rV   rC   rM   r   r   r   r   r-      s   

r-   c                   @   sR   e Zd ZdZdeee dddZeedddZ	ee
edf dd	d
ZdS )r/   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)nodesrD   c                 C   s   || _ || _d S r   )_nodesrG   )r   rW   rD   r   r   r   r!      s    z,AfterAsyncClusterInstantiationEvent.__init__rH   c                 C   s   | j S r   )rX   r4   r   r   r   rW      s    z)AfterAsyncClusterInstantiationEvent.nodesc                 C   s   | j S r   rK   r4   r   r   r   rD      s    z7AfterAsyncClusterInstantiationEvent.credential_provider)N)r   r   r   r   dictr   r	   r!   r<   rW   r   rD   r   r   r   r   r/      s   
 r/   c                   @   s   e Zd ZdZed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(      s   r(   c                   @   s   e Zd ZdZedddZdS )r2   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   r1   r   r   r   r   r   r2      s   r2   c                   @   sR   e Zd ZdZdd ZedddZdd Zd	d
 Ze	dddZ
e	dddZdS )r*   z
    Listener that registers a re-authentication callback for pooled connections.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   s
   d | _ d S r   _eventr4   r   r   r   r!      s    z+RegisterReAuthForPooledConnections.__init__r   c                 C   s\   t |jtrX|| _|jtjkr<|j| j |j	| j
 n|j| j |j	| j d S r   )
isinstancerD   r
   r\   rC   r=   r@   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\   rB   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   rd   rf   r   r   r   rb     s    z1RegisterReAuthForPooledConnections._re_auth_asyncerrorc                 C   s   t || jd S r   r   r\   r   rj   r   r   r   ra     s    z2RegisterReAuthForPooledConnections._raise_on_errorc                    s   t || jd S r   rk   rl   r   r   r   rc     s    z8RegisterReAuthForPooledConnections._raise_on_error_asyncN)r   r   r   r   r!   r)   r   r_   rb   r$   ra   rc   r   r   r   r   r*      s   r*   c                   @   sR   e Zd ZdZdd ZedddZdd Zd	d
 Ze	dddZ
e	dddZdS )r,   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   s
   d | _ d S r   r[   r4   r   r   r   r!      s    z*RegisterReAuthForSingleConnection.__init__r   c                 C   sf   t |jjtrb|| _|jtjkrB|jj| j	 |jj
| j n |jj| j |jj
| j d S r   )r]   r;   rD   r
   r\   rC   r=   r@   r^   r_   r`   ra   rb   rc   r   r   r   r   r   #  s     z(RegisterReAuthForSingleConnection.listenc              	   C   s@   | j j. | j jd|d|  | j j  W 5 Q R X d S NZAUTHoidr\   rM   r;   send_commandtry_get	get_valueread_responser   rg   r   r   r   r_   2  s    
  z*RegisterReAuthForSingleConnection._re_authc              
      sZ   | j j4 I d H : | j jd|d| I d H  | j j I d H  W 5 Q I d H R X d S rm   ro   rt   r   r   r   rb   9  s      
z0RegisterReAuthForSingleConnection._re_auth_asyncri   c                 C   s   t || jd S r   rk   rl   r   r   r   ra   @  s    z1RegisterReAuthForSingleConnection._raise_on_errorc                    s   t || jd S r   rk   rl   r   r   r   rc   C  s    z7RegisterReAuthForSingleConnection._raise_on_error_asyncN)r   r   r   r   r!   r+   r   r_   rb   r$   ra   rc   r   r   r   r   r,     s   r,   c                   @   s>   e Zd Zdd ZedddZedddZed	d
dZ	dS )r0   c                 C   s
   d | _ d S r   r[   r4   r   r   r   r!   H  s    z+RegisterReAuthForAsyncClusterNodes.__init__r   c                 C   s2   t |jtr.|| _|j| j |j| j d S r   )r]   rD   r
   r\   r^   r_   r`   ra   r   r   r   r   r   K  s    z)RegisterReAuthForAsyncClusterNodes.listenrg   c                    s*   | j jD ]}| j j| |I d H  qd S r   )r\   rW   re   )r   rg   keyr   r   r   r_   Q  s    z+RegisterReAuthForAsyncClusterNodes._re_authri   c                    s   t || jd S r   rk   rl   r   r   r   ra   U  s    z2RegisterReAuthForAsyncClusterNodes._raise_on_errorN)
r   r   r   r!   r/   r   r   r_   r$   ra   r   r   r   r   r0   G  s   r0   c                   @   sZ   e Zd Zdd ZedddZedddZedd	d
Ze	dddZ
e	dddZdS )r.   c                 C   s"   d | _ d | _d | _d | _d | _d S r   )r:   rT   rF   rN   r\   r4   r   r   r   r!   Z  s
    z RegisterReAuthForPubSub.__init__r   c                 C   s   t |jjtr|j dkr|| _|j| _|j| _|j	| _
|j| _| j
tjkrp| jj| j | jj| j n | jj| j | jj| j d S )N)   3)r]   rU   rD   r
   Zget_protocolr\   r:   rV   rT   rC   rF   rM   rN   r=   r@   r^   r_   r`   ra   rb   rc   r   r   r   r   r   a  s$     zRegisterReAuthForPubSub.listenru   c              	   C   sF   | j * | jd|d|  | j  W 5 Q R X | j| d S rm   rN   r:   rp   rq   rr   rs   rT   re   rt   r   r   r   r_   t  s      z RegisterReAuthForPubSub._re_authc              
      sf   | j 4 I d H 6 | jd|d| I d H  | j I d H  W 5 Q I d H R X | j|I d H  d S rm   ry   rt   r   r   r   rb   }  s      
 z&RegisterReAuthForPubSub._re_auth_asyncri   c                 C   s   t || jd S r   rk   rl   r   r   r   ra     s    z'RegisterReAuthForPubSub._raise_on_errorc                    s   t || jd S r   rk   rl   r   r   r   rc     s    z-RegisterReAuthForPubSub._raise_on_error_asyncN)r   r   r   r!   r-   r   r   r_   rb   r$   ra   rc   r   r   r   r   r.   Y  s   		r.   )#rR   rP   abcr   r   enumr   typingr   r   r   Zredis.auth.tokenr   Zredis.credentialsr	   r
   r   r   r   r$   r   r&   r'   r1   r=   r)   r+   r-   r/   r(   r2   r*   r,   r0   r.   r   r   r   r   <module>   s0   

$		#-