
    1i<7                        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e      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d d      Z G d de      Z G d de      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d       Z G d! d"e      Z  G d# d$e      Z! G d% d&e      Z" G d' d(e      Z# G d) d*e      Z$ G d+ d,e      Z% G d- d.e      Z&y)/    N)ABCabstractmethod)Enum)DictListOptionalTypeUnion)TokenInterface)CredentialProviderStreamingCredentialProviderc                   &    e Zd ZdZedefd       Zy)EventListenerInterfacez7
    Represents a listener for given event object.
    eventc                      y N selfr   s     U/mnt/e/genesis-system/.venvs/voice-bridge/lib/python3.12/site-packages/redis/event.pylistenzEventListenerInterface.listen           N__name__
__module____qualname____doc__r   objectr   r   r   r   r   r      s"     F  r   r   c                   &    e Zd ZdZedefd       Zy)AsyncEventListenerInterfacez>
    Represents an async listener for given event object.
    r   c                    K   y wr   r   r   s     r   r   z"AsyncEventListenerInterface.listen   	        Nr   r   r   r   r!   r!      s"     &  r   r!   c                   r    e Zd ZdZedefd       Zedefd       Zedee	e   e
eeef      f   fd       Zy)EventDispatcherInterfacezf
    Represents a dispatcher that dispatches events to listeners
    associated with given event.
    r   c                      y r   r   r   s     r   dispatchz!EventDispatcherInterface.dispatch%   r   r   c                    K   y wr   r   r   s     r   dispatch_asyncz'EventDispatcherInterface.dispatch_async)   r#   r$   mappingsc                      y)zRegister additional listeners.Nr   )r   r+   s     r   register_listenersz+EventDispatcherInterface.register_listeners-   s     	r   N)r   r   r   r   r   r   r(   r*   r   r	   r   r
   r   r!   r-   r   r   r   r&   r&      s    
 f   &   L-/JJKLN
 r   r&   c                   ,     e Zd ZdZdedef fdZ xZS )EventExceptionzM
    Exception wrapper that adds an event object into exception context.
    	exceptionr   c                 @    || _         || _        t        |   |       y r   )r0   r   super__init__)r   r0   r   	__class__s      r   r3   zEventException.__init__>   s    "
#r   )r   r   r   r   	Exceptionr   r3   __classcell__)r4   s   @r   r/   r/   9   s     $) $F $ $r   r/   c                   |    e Zd Z	 d	deeee   ee   f      fdZ	defdZ
defdZdeee   eeeef      f   fdZy)
EventDispatcherNevent_listenersc                 .   t         t               gt        t               gt        t               gt        t               gt        t               gt        t               gi| _        t        j                         | _        d| _        |r| j#                  |       yy)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   r9   s     r   r3   zEventDispatcher.__init__F   s     )(*+ 5247 4136 46M6O5P/2T2V1W--/0
 	%& ^^%
##O4 r   r   c                     | j                   5  | j                  j                  t        |      g       }|D ]  }|j	                  |        	 d d d        y # 1 sw Y   y xY wr   )rJ   rG   gettyper   r   r   	listenerslisteners       r   r(   zEventDispatcher.dispatchh   sU    ZZ 	'5599$u+rJI% '&'	' 	' 	's   >AAc                 `  K   | j                   t        j                         | _         | j                   4 d {    | j                  j	                  t        |      g       }|D ]  }|j                  |       d {     d d d       d {    y 7 Z7 7 	# 1 d {  7  sw Y   y xY wwr   )rK   asynciorI   rG   rM   rN   r   rO   s       r   r*   zEventDispatcher.dispatch_asynco   s     #&||~D## 	- 	-5599$u+rJI% -ooe,,,-	- 	- 	- -		- 	- 	- 	-sV   6B.BB.?B;B<BB.BB.BB.B+B" B+'B.r+   c           	          | j                   5  |D ]W  }|| j                  v r5t        t        | j                  |   ||   z               | j                  |<   F||   | j                  |<   Y 	 d d d        y # 1 sw Y   y xY wr   )rJ   rG   listset)r   r+   
event_types      r   r-   z"EventDispatcher.register_listenersy   s     ZZ 
	U& 	U
!>!>>@D 99*E&z23AD11*= AI@TD11*=	U
	U 
	U 
	Us   AA44A=r   )r   r   r   r   r   r	   r   r   r   r3   r(   r*   r
   r!   r-   r   r   r   r8   r8   D   s~     	 5!ft$:;;<
 5D'f '-& -UL-/JJKLN
Ur   r8   c                   &    e Zd ZdZd Zed        Zy)r;   zA
    Event that will be fired before each command execution.
    c                     || _         y r   _connection)r   
connections     r   r3   z%AfterConnectionReleasedEvent.__init__   s
    %r   c                     | j                   S r   rZ   r   s    r   r\   z'AfterConnectionReleasedEvent.connection       r   N)r   r   r   r   r3   propertyr\   r   r   r   r;   r;      s     &    r   r;   c                       e Zd Zy)rE   Nr   r   r   r   r   r   rE   rE          r   rE   c                       e Zd ZdZdZy)
ClientType)sync)asyncN)r   r   r   SYNCASYNCr   r   r   re   re      s    DEr   re   c                   t    e Zd ZdZ	 ddededee   fdZe	d        Z
e	defd	       Ze	deedf   fd
       Zy)r=   zQ
    Event that will be fired after pooled connection instances was created.
    Nconnection_poolsclient_typecredential_providerc                 .    || _         || _        || _        y r   )_connection_pools_client_type_credential_provider)r   rk   rl   rm   s       r   r3   z1AfterPooledConnectionsInstantiationEvent.__init__   s     "2'$7!r   c                     | j                   S r   )ro   r^   s    r   rk   z9AfterPooledConnectionsInstantiationEvent.connection_pools   s    %%%r   returnc                     | j                   S r   rp   r^   s    r   rl   z4AfterPooledConnectionsInstantiationEvent.client_type          r   c                     | j                   S r   rq   r^   s    r   rm   z<AfterPooledConnectionsInstantiationEvent.credential_provider       (((r   r   )r   r   r   r   r   re   r   r   r3   r`   rk   rl   r
   rm   r   r   r   r=   r=      s     =A	88  8 &&89	8 & & !Z ! ! )U+=t+C%D ) )r   r=   c                       e Zd ZdZdedeej                  ej                  f   fdZ
ed        Zedefd       Zedeej                  ej                  f   fd       Zy	)
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
    rl   connection_lockc                 .    || _         || _        || _        y r   )r[   rp   _connection_lock)r   r\   rl   r{   s       r   r3   z0AfterSingleConnectionInstantiationEvent.__init__   s     &' /r   c                     | j                   S r   rZ   r^   s    r   r\   z2AfterSingleConnectionInstantiationEvent.connection   r_   r   rs   c                     | j                   S r   ru   r^   s    r   rl   z3AfterSingleConnectionInstantiationEvent.client_type   rv   r   c                     | j                   S r   r}   r^   s    r   r{   z7AfterSingleConnectionInstantiationEvent.connection_lock       $$$r   N)r   r   r   r   re   r
   rH   RLockrS   rI   r3   r`   r\   rl   r{   r   r   r   r?   r?      s    0  0 y<=	0     !Z ! ! %y'D!E % %r   r?   c                       e Zd Zdedeej                  ej                  f   fdZ	e
d        Ze
d        Ze
defd       Ze
deej                  ej                  f   fd       Zy	)
rA   rl   r{   c                 <    || _         || _        || _        || _        y r   )_pubsub_connection_connection_poolrp   r}   )r   pubsub_connectionconnection_poolrl   r{   s        r   r3   z0AfterPubSubConnectionInstantiationEvent.__init__   s$     #4 /' /r   c                     | j                   S r   )r   r^   s    r   r   z9AfterPubSubConnectionInstantiationEvent.pubsub_connection   s    &&&r   c                     | j                   S r   )r   r^   s    r   r   z7AfterPubSubConnectionInstantiationEvent.connection_pool   r   r   rs   c                     | j                   S r   ru   r^   s    r   rl   z3AfterPubSubConnectionInstantiationEvent.client_type   rv   r   c                     | j                   S r   r   r^   s    r   r{   z7AfterPubSubConnectionInstantiationEvent.connection_lock   r   r   N)r   r   r   re   r
   rH   r   rS   rI   r3   r`   r   r   rl   r{   r   r   r   rA   rA      s    
0  	
0
 y<=
0 ' ' % % !Z ! ! %y'D!E % %r   rA   c                   `    e Zd ZdZ	 d	dedee   fdZedefd       Z	ede
edf   fd       Zy)
rC   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nodesrm   c                      || _         || _        y r   )_nodesrq   )r   r   rm   s      r   r3   z,AfterAsyncClusterInstantiationEvent.__init__  s    
 $7!r   rs   c                     | j                   S r   )r   r^   s    r   r   z)AfterAsyncClusterInstantiationEvent.nodes  s    {{r   c                     | j                   S r   rx   r^   s    r   rm   z7AfterAsyncClusterInstantiationEvent.credential_provider  ry   r   r   )r   r   r   r   dictr   r   r3   r`   r   r
   rm   r   r   r   rC   rC      sk     =A88 &&898 t   )U+=t+C%D ) )r   rC   c                   L    e Zd ZdZdedefdZedefd       Zedefd       Z	y)	OnCommandsFailEventzD
    Event fired whenever a command fails during the execution.
    commandsr0   c                      || _         || _        y r   )	_commands
_exception)r   r   r0   s      r   r3   zOnCommandsFailEvent.__init__  s    
 "#r   rs   c                     | j                   S r   )r   r^   s    r   r   zOnCommandsFailEvent.commands"  s    ~~r   c                     | j                   S r   )r   r^   s    r   r0   zOnCommandsFailEvent.exception&  s    r   N)
r   r   r   r   tupler5   r3   r`   r   r0   r   r   r   r   r     sT    $$ $ %   9  r   r   c                       e Zd Zy)AsyncOnCommandsFailEventNrb   r   r   r   r   r   +  rc   r   r   c                       e Zd ZdZdefdZy)r<   zG
    Listener that performs re-authentication of given connection.
    r   c                 8    |j                   j                          y r   r\   re_authr   s     r   r   zReAuthConnectionListener.listen4  s      "r   N)r   r   r   r   r;   r   r   r   r   r<   r<   /  s    #8 #r   r<   c                       e Zd ZdZdefdZy)rF   zM
    Async listener that performs re-authentication of given connection.
    r   c                 T   K   |j                   j                          d {    y 7 wr   r   r   s     r   r   z$AsyncReAuthConnectionListener.listen=  s     &&(((s   (&(N)r   r   r   r   rE   r   r   r   r   rF   rF   8  s    )"C )r   rF   c                   F    e Zd ZdZd ZdefdZd Zd Zde	fdZ
de	fd	Zy
)r>   z
    Listener that registers a re-authentication callback for pooled connections.
    Required by :class:`StreamingCredentialProvider`.
    c                     d | _         y r   _eventr^   s    r   r3   z+RegisterReAuthForPooledConnections.__init__G  	    r   r   c                    t        |j                  t              r|| _        |j                  t
        j                  k(  rK|j                  j                  | j                         |j                  j                  | j                         y |j                  j                  | j                         |j                  j                  | j                         y y r   )
isinstancerm   r   r   rl   re   rh   on_next_re_authon_error_raise_on_error_re_auth_async_raise_on_error_asyncr   s     r   r   z)RegisterReAuthForPooledConnections.listenJ  s    e//1LMDK  JOO3))11$--@))2243G3GH))11$2E2EF))2243M3MN Nr   c                 \    | j                   j                  D ]  }|j                  |        y r   r   rk   re_auth_callbackr   tokenpools      r   r   z+RegisterReAuthForPooledConnections._re_authU  s)    KK00 	)D!!%(	)r   c                 x   K   | j                   j                  D ]  }|j                  |       d {     y 7 wr   r   r   s      r   r   z1RegisterReAuthForPooledConnections._re_auth_asyncY  s6     KK00 	/D''...	/.s   .:8:errorc                 .    t        || j                        r   r/   r   r   r   s     r   r   z2RegisterReAuthForPooledConnections._raise_on_error]      UDKK00r   c                 6   K   t        || j                        wr   r   r   s     r   r   z8RegisterReAuthForPooledConnections._raise_on_error_async`       UDKK00   N)r   r   r   r   r3   r=   r   r   r   r5   r   r   r   r   r   r>   r>   A  s=    
	OD 	O)/1Y 11 1r   r>   c                   F    e Zd ZdZd ZdefdZd Zd Zde	fdZ
de	fd	Zy
)r@   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                     d | _         y r   r   r^   s    r   r3   z*RegisterReAuthForSingleConnection.__init__j  r   r   r   c                    t        |j                  j                  t              r|| _        |j
                  t        j                  k(  r_|j                  j                  j                  | j                         |j                  j                  j                  | j                         y |j                  j                  j                  | j                         |j                  j                  j                  | j                         y y r   )r   r\   rm   r   r   rl   re   rh   r   r   r   r   r   r   r   s     r   r   z(RegisterReAuthForSingleConnection.listenm  s    002M
  DK  JOO3  44<<T]]K  44==d>R>RS  44<<T=P=PQ  44==..
r   c                 *   | j                   j                  5  | j                   j                  j                  d|j	                  d      |j                                | j                   j                  j                          d d d        y # 1 sw Y   y xY wNAUTHoidr   r{   r\   send_commandtry_get	get_valueread_responser   r   s     r   r   z*RegisterReAuthForSingleConnection._re_auth|  sk    [[(( 	3KK""//e,eoo.? KK""002		3 	3 	3s   A)B		Bc                   K   | j                   j                  4 d {    | j                   j                  j                  d|j	                  d      |j                                d {    | j                   j                  j                          d {    d d d       d {    y 7 7 C7 7 # 1 d {  7  sw Y   y xY wwr   r   r   s     r   r   z0RegisterReAuthForSingleConnection._re_auth_async  s     ;;.. 	9 	9++((55e,eoo.?   ++((66888		9 	9 	9 9		9 	9 	9 	9si   CB+CAB3)B-*+B3B/B3C%B1&C-B3/B31C3C9B<:CCr   c                 .    t        || j                        r   r   r   s     r   r   z1RegisterReAuthForSingleConnection._raise_on_error  r   r   c                 6   K   t        || j                        wr   r   r   s     r   r   z7RegisterReAuthForSingleConnection._raise_on_error_async  r   r   N)r   r   r   r   r3   r?   r   r   r   r5   r   r   r   r   r   r@   r@   d  s;    
C 391Y 11 1r   r@   c                   6    e Zd Zd ZdefdZdefdZdefdZ	y)	rD   c                     d | _         y r   r   r^   s    r   r3   z+RegisterReAuthForAsyncClusterNodes.__init__  r   r   r   c                     t        |j                  t              rR|| _        |j                  j	                  | j
                         |j                  j                  | j                         y y r   )r   rm   r   r   r   r   r   r   r   s     r   r   z)RegisterReAuthForAsyncClusterNodes.listen  sR    e//1LMDK%%--dmm<%%..t/C/CD Nr   r   c                    K   | j                   j                  D ]2  }| j                   j                  |   j                  |       d {    4 y 7 wr   )r   r   r   )r   r   keys      r   r   z+RegisterReAuthForAsyncClusterNodes._re_auth  sG     ;;$$ 	AC++##C(99%@@@	A@s   AAAAr   c                 6   K   t        || j                        wr   r   r   s     r   r   z2RegisterReAuthForAsyncClusterNodes._raise_on_error  r   r   N)
r   r   r   r3   rC   r   r   r   r5   r   r   r   r   rD   rD     s0    E? EAN A19 1r   rD   c                   N    e Zd Zd ZdefdZdefdZdefdZde	fdZ
de	fd	Zy
)rB   c                 J    d | _         d | _        d | _        d | _        d | _        y r   )r[   r   rp   r}   r   r^   s    r   r3   z RegisterReAuthForPubSub.__init__  s)     $  $r   r   c                    t        |j                  j                  t              rD|j                  j	                         dv r&|| _        |j                  | _        |j                  | _        |j                  | _
        |j                  | _        | j                  t        j                  k(  r_| j                  j                  j                  | j                          | j                  j                  j#                  | j$                         y | j                  j                  j                  | j&                         | j                  j                  j#                  | j(                         y y y )N)   3)r   r   rm   r   get_protocolr   r[   r   r   rl   rp   r{   r}   re   rh   r   r   r   r   r   r   r   s     r   r   zRegisterReAuthForPubSub.listen  s   ##779T
%%224@DK$66D$)$9$9D! % 1 1D$)$9$9D!  JOO3  44<<T]]K  44==d>R>RS  44<<T=P=PQ  44==.. A
r   r   c                 $   | j                   5  | j                  j                  d|j                  d      |j	                                | j                  j                          d d d        | j                  j                  |       y # 1 sw Y   %xY wr   r}   r[   r   r   r   r   r   r   r   s     r   r   z RegisterReAuthForPubSub._re_auth  su    "" 	-))e,eoo.? **,		- 	..u5	- 	-s   ABBc                   K   | j                   4 d {    | j                  j                  d|j                  d      |j	                                d {    | j                  j                          d {    d d d       d {    | j                  j                  |       d {    y 7 7 \7 <7 .# 1 d {  7  sw Y   >xY w7 "wr   r   r   s     r   r   z&RegisterReAuthForPubSub._re_auth_async  s     (( 	3 	3""//e,eoo.?   ""00222		3 	3 ##44U;;;	3 3		3 	3 	3 	3 	<st   CB0C>B8B2!B87B48B8<CB6"C*C+C2B84B86C8C
>C?C
Cr   c                 .    t        || j                        r   r   r   s     r   r   z'RegisterReAuthForPubSub._raise_on_error  r   r   c                 6   K   t        || j                        wr   r   r   s     r   r   z-RegisterReAuthForPubSub._raise_on_error_async  r   r   N)r   r   r   r3   rA   r   r   r   r   r5   r   r   r   r   r   rB   rB     sD    C &6n 6<. <1Y 11 1r   rB   )'rS   rH   abcr   r   enumr   typingr   r   r   r	   r
   redis.auth.tokenr   redis.credentialsr   r   r   r!   r&   r5   r/   r8   r;   rE   re   r=   r?   rA   rC   r   r   r<   rF   r>   r@   rD   rB   r   r   r   <module>r      s     #  4 4 + MS # s 4$Y $FU. FUR
  
 	(D 	 
) )8% %>% %<) )2 ,	2 	#5 #)$? ) 1)?  1F*1(> *1Z1)? 1$114 11r   