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   k/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/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   	listeners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   rB   e      z'AfterConnectionReleasedEvent.connectionN)r   r   r   r   r&   propertyrB   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   rG   n   s    rG   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   rL   rM   rN   r   r   r   r&   x      
z1AfterPooledConnectionsInstantiationEvent.__init__c                 C   rD   r   )rP   r9   r   r   r   rL      rE   z9AfterPooledConnectionsInstantiationEvent.connection_poolsreturnc                 C   rD   r   rQ   r9   r   r   r   rM      rE   z4AfterPooledConnectionsInstantiationEvent.client_typec                 C   rD   r   rR   r9   r   r   r   rN      rE   z<AfterPooledConnectionsInstantiationEvent.credential_providerr   )r   r   r   r   r   rG   r   r	   r&   rF   rL   rM   r   rN   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
    rM   connection_lockc                 C   rO   r   )rA   rQ   _connection_lock)r   rB   rM   rW   r   r   r   r&      rS   z0AfterSingleConnectionInstantiationEvent.__init__c                 C   rD   r   r@   r9   r   r   r   rB      rE   z2AfterSingleConnectionInstantiationEvent.connectionrT   c                 C   rD   r   rU   r9   r   r   r   rM      rE   z3AfterSingleConnectionInstantiationEvent.client_typec                 C   rD   r   rX   r9   r   r   r   rW      rE   z7AfterSingleConnectionInstantiationEvent.connection_lockN)r   r   r   r   rG   r   	threadingLockasyncior&   rF   rB   rM   rW   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   rM   rW   c                 C   s   || _ || _|| _|| _d S r   )_pubsub_connection_connection_poolrQ   rX   )r   pubsub_connectionconnection_poolrM   rW   r   r   r   r&      s   
z0AfterPubSubConnectionInstantiationEvent.__init__c                 C   rD   r   )r]   r9   r   r   r   r_      rE   z9AfterPubSubConnectionInstantiationEvent.pubsub_connectionc                 C   rD   r   )r^   r9   r   r   r   r`      rE   z7AfterPubSubConnectionInstantiationEvent.connection_poolrT   c                 C   rD   r   rU   r9   r   r   r   rM      rE   z3AfterPubSubConnectionInstantiationEvent.client_typec                 C   rD   r   rY   r9   r   r   r   rW      rE   z7AfterPubSubConnectionInstantiationEvent.connection_lockN)r   r   r   rG   r   rZ   r[   r\   r&   rF   r_   r`   rM   rW   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nodesrN   c                 C   s   || _ || _d S r   )_nodesrR   )r   ra   rN   r   r   r   r&      s   
z,AfterAsyncClusterInstantiationEvent.__init__rT   c                 C   rD   r   )rb   r9   r   r   r   ra      rE   z)AfterAsyncClusterInstantiationEvent.nodesc                 C   rD   r   rV   r9   r   r   r   rN      rE   z7AfterAsyncClusterInstantiationEvent.credential_providerr   )r   r   r   r   dictr   r	   r&   rF   ra   r   rN   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   rB   re_authr   r   r   r   r      s   zReAuthConnectionListener.listenN)r   r   r   r   r,   r   r   r   r   r   r-          r-   c                   @   rd   )r7   zM
    Async listener that performs re-authentication of given connection.
    r   c                    s   |j  I d H  d S r   re   r   r   r   r   r      s   z$AsyncReAuthConnectionListener.listenN)r   r   r   r   r6   r   r   r   r   r   r7      rg   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&      rC   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   )
isinstancerN   r
   rk   rM   rG   rJ   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   rk   rL   re_auth_callbackr   tokenpoolr   r   r   rn     s   z+RegisterReAuthForPooledConnections._re_authc                    s$   | j jD ]
}||I d H  qd S r   rs   ru   r   r   r   rq     s   z1RegisterReAuthForPooledConnections._re_auth_asyncerrorc                 C      t || jr   r#   rk   r   rx   r   r   r   rp        z2RegisterReAuthForPooledConnections._raise_on_errorc                       t || jr   rz   r{   r   r   r   rr        z8RegisterReAuthForPooledConnections._raise_on_error_asyncN)r   r   r   r   r&   r.   r   rn   rq   r)   rp   rr   r   r   r   r   r/      s    r/   c                   @   rh   )r1   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                 C   ri   r   rj   r9   r   r   r   r&      rC   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   )rl   rB   rN   r
   rk   rM   rG   rJ   rm   rn   ro   rp   rq   rr   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rk   rW   rB   send_commandtry_get	get_valueread_responser   rv   r   r   r   rn   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   rq   9  s   
.z0RegisterReAuthForSingleConnection._re_auth_asyncrx   c                 C   ry   r   rz   r{   r   r   r   rp   @  r|   z1RegisterReAuthForSingleConnection._raise_on_errorc                    r}   r   rz   r{   r   r   r   rr   C  r~   z7RegisterReAuthForSingleConnection._raise_on_error_asyncN)r   r   r   r   r&   r0   r   rn   rq   r)   rp   rr   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   ri   r   rj   r9   r   r   r   r&   H  rC   z+RegisterReAuthForAsyncClusterNodes.__init__r   c                 C   s6   t |jtr|| _|j| j |j| j d S d S r   )rl   rN   r
   rk   rm   rn   ro   rp   r   r   r   r   r   K  s
   z)RegisterReAuthForAsyncClusterNodes.listenrv   c                    s,   | j jD ]}| j j| |I d H  qd S r   )rk   ra   rt   )r   rv   keyr   r   r   rn   Q  s   z+RegisterReAuthForAsyncClusterNodes._re_authrx   c                    r}   r   rz   r{   r   r   r   rp   U  r~   z2RegisterReAuthForAsyncClusterNodes._raise_on_errorN)
r   r   r   r&   r4   r   r   rn   r)   rp   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   )rA   r^   rQ   rX   rk   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)rl   r_   rN   r
   get_protocolrk   rA   r`   r^   rM   rQ   rW   rX   rG   rJ   rm   rn   ro   rp   rq   rr   r   r   r   r   r   a  s$   zRegisterReAuthForPubSub.listenrv   c                 C   sZ   | j  | jd|d|  | j  W d    n1 s w   Y  | j| d S r   rX   rA   r   r   r   r   r^   rt   r   r   r   r   rn   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   rq   }  s   
(z&RegisterReAuthForPubSub._re_auth_asyncrx   c                 C   ry   r   rz   r{   r   r   r   rp     r|   z'RegisterReAuthForPubSub._raise_on_errorc                    r}   r   rz   r{   r   r   r   rr     r~   z-RegisterReAuthForPubSub._raise_on_error_asyncN)r   r   r   r&   r2   r   r   rn   rq   r)   rp   rr   r   r   r   r   r3   Y  s    		r3   )#r\   rZ   abcr   r   enumr   typingr   r   r   redis.auth.tokenr   redis.credentialsr	   r
   r   r   r    r)   r#   r+   r,   r6   rG   r.   r0   r2   r4   r-   r7   r/   r1   r5   r3   r   r   r   r   <module>   s2    

$		#-