
    +h                        d dl Z d dlZd dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZmZmZm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+m,Z,m-Z-m.Z. d d	l/m0Z0m1Z1 d d
l2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z=m>Z>m?Z? d dl@mAZAmBZBmCZCmDZDmEZEmFZF e
eeGeGf   ge	d   f   ZH ede?      ZI ede?e>      ZJ edd      ZK edee=eLf         ZMerd dlNmOZO  G d de0      ZP G d de0      ZQeePeQf   ZR G d de(e,e+e-      ZSeSZT G d de1      ZU G d d      ZV G d d      ZW G d  d!e0      ZX G d" d#e0      ZYeeXeYf   ZZeeeeGe[f   d$f   eeGef   f   Z\ee\   Z] G d% d&eS      Z^y)'    N)TYPE_CHECKINGAnyAsyncIterator	AwaitableCallableDictIterableListMappingMutableMappingOptionalSetTupleTypeTypeVarUnioncast)_RedisCallbacks_RedisCallbacksRESP2_RedisCallbacksRESP3bool_ok)
ConnectionConnectionPoolSSLConnectionUnixDomainSocketConnectionLock)Retry)EMPTY_RESPONSENEVER_DECODEAbstractRedisCaseInsensitiveDict)AsyncCoreCommandsAsyncRedisModuleCommandsAsyncSentinelCommandslist_or_args)Protocol	TypedDict)CredentialProvider)ConnectionErrorExecAbortErrorPubSubError
RedisErrorResponseErrorTimeoutError
WatchError)ChannelT
EncodableTKeyT)HIREDIS_AVAILABLE_set_info_loggerdeprecated_functionget_lib_versionsafe_strstr_if_bytes_KeyT)bound_ArgT_RedisTRedis_NormalizeKeysT)Scriptc                       e Zd ZdefdZy)ResponseCallbackProtocolresponsec                      y N selfrC   kwargss      G/var/www/html/venv/lib/python3.12/site-packages/redis/asyncio/client.py__call__z!ResponseCallbackProtocol.__call__S           N__name__
__module____qualname__r   rK   rF   rM   rJ   rB   rB   R   s     rM   rB   c                       e Zd ZdefdZy)AsyncResponseCallbackProtocolrC   c                    K   y wrE   rF   rG   s      rJ   rK   z&AsyncResponseCallbackProtocol.__call__X   	        NrN   rF   rM   rJ   rS   rS   W   s    s rM   rS   c            D           e Zd ZU dZeeeef   ef   e	d<   e
	 	 dldededee   fd       Ze
d	ed    d
edd fd       Zddddddddddddddddddddddddddd e       ddddddd"dededeeef   dee   dee   dee   dee   deeeeeef   f      d
ee   dee   ded ed!ed"ed#ee   d$ed%ee   d&ee   d'ed(ee   d)ee   d*ed+ee   ded,ed-ee   d.ee   d/ee   d0ee   d1ee   dee   d2ee   d3ee   fBd4Zd5 Zd6 Zd7edefd8Zd9ed:efd;Zd< Zd= Zded>   fd?Z dmd@Z!dA Z"	 dndBedCee   ddDfdEZ#ddddFdGe$dDgee%e&e%   f   f   dHe'dCee   dIedJee   f
dKZ(	 	 	 	 	 	 dodLe'dMee   dNedOedPee   dQeee)      dRede)fdSZ*dpdTZ+dqdUZ,drdVZ-d7edefdWZ.dX Z/dYZ0e1jd                  e3jh                  fdZe%d[e%ddfd\Z5dsd]ee   ddfd^Z6 e7d_d`dab      dsd]ee   ddfdc       Z8dd Z9dee:dfe;fdgZ<dh Z=die:djeeef   fdkZ>y)tr>   a  
    Implementation of the Redis protocol.

    This abstract class provides a Python interface to all Redis commands
    and an implementation of the Redis protocol.

    Pipelines derive from this, implementing how
    the commands are sent and received to the Redis server. Based on
    configuration, an instance will either use a ConnectionPool, or
    Connection object to talk to redis.
    response_callbacksFNurlsingle_connection_clientauto_close_connection_poolc                     t        j                  |fi |} | ||      }|t        j                  t	        d             nd}||_        |S )a  
        Return a Redis client object configured from the given URL

        For example::

            redis://[[username]:[password]]@localhost:6379/0
            rediss://[[username]:[password]]@localhost:6379/0
            unix://[username@]/path/to/socket.sock?db=0[&password=password]

        Three URL schemes are supported:

        - `redis://` creates a TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/redis>
        - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/rediss>
        - ``unix://``: creates a Unix Domain Socket connection.

        The username, password, hostname, path and all querystring values
        are passed through urllib.parse.unquote in order to replace any
        percent-encoded values with their corresponding characters.

        There are several ways to specify a database number. The first value
        found will be used:

        1. A ``db`` querystring option, e.g. redis://localhost?db=0

        2. If using the redis:// or rediss:// schemes, the path argument
               of the url, e.g. redis://localhost/0

        3. A ``db`` keyword argument to this function.

        If none of these options are specified, the default db=0 is used.

        All querystring options are cast to their appropriate Python types.
        Boolean arguments can be specified with string values "True"/"False"
        or "Yes"/"No". Values that cannot be properly cast cause a
        ``ValueError`` to be raised. Once parsed, the querystring arguments
        and keyword arguments are passed to the ``ConnectionPool``'s
        class initializer. In the case of conflicting arguments, querystring
        arguments always win.

        connection_poolrZ   "auto_close_connection_pool" is deprecated since version 5.0.0. Please create a ConnectionPool explicitly and provide to the Redis() constructor instead.T)r   from_urlwarningswarnDeprecationWarningr[   )clsrY   rZ   r[   rI   r^   clients          rJ   r`   zRedis.from_urlp   sa    d )11#@@+%=
 &1MM"B *.&,F)rM   rd   r^   returnc                 &     | |      }d|_         |S )z
        Return a Redis client from the given connection pool.
        The Redis client will take ownership of the connection pool and
        close it when the Redis client is closed.
        )r^   T)r[   )rd   r^   re   s      rJ   	from_poolzRedis.from_pool   s      +
 -1)rM   	localhosti  r   zutf-8strictrequiredzredis-py   )"hostportdbpasswordsocket_timeoutsocket_connect_timeoutsocket_keepalivesocket_keepalive_optionsr^   unix_socket_pathencodingencoding_errorsdecode_responsesretry_on_timeoutretry_on_errorsslssl_keyfilessl_certfilessl_cert_reqsssl_ca_certsssl_ca_datassl_check_hostnamemax_connectionsrZ   health_check_intervalclient_namelib_namelib_versionusernameretryr[   redis_connect_funccredential_providerprotocolrm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   c       "   
      F   |t        j                  t        d             nd}|	s|sg }|du r|j                  t               i d|d|d|d|!d|d	|d
|d|d|d|dt        j                  |      d|d|d|d|d|d| d|"i}#|
|#j                  |
t        d       n6|#j                  |||||d       |r|#j                  t        ||||||d       || _
        t        di |#}	nd| _
        |	| _        || _        d| _        t        t               | _        | j                  j$                  j'                  d      dv r | j"                  j                  t(               n| j"                  j                  t*               t-        j.                         | _        y)a4  
        Initialize a new Redis client.
        To specify a retry policy for specific errors, first set
        `retry_on_error` to a list of the error/s to retry on, then set
        `retry` to a valid `Retry` object.
        To retry on TimeoutError, `retry_on_timeout` can also be set to `True`.
        Nr_   Tro   r   rp   r   rq   rv   rw   rx   ry   rz   r   r   r   r   r   r   r   r   )pathconnection_class)rm   rn   rr   rs   rt   )r   r|   r}   r~   r   r   r   F)3   rF   )ra   rb   rc   appendr/   copydeepcopyupdater   r   r[   r   r^   rZ   
connectionr"   r   rX   connection_kwargsgetr   r   asyncior   _single_conn_lock)$rH   rm   rn   ro   rp   rq   rr   rs   rt   r^   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   rZ   r   r   r   r   r   r   r[   r   r   r   rI   s$                                       rJ   __init__zRedis.__init__   sB   b &1MM"B *.&!!#4'%%l3bH H &':	
 !. H "? #$4 #$4 !. u- "? ()> { H  {!" %&8#$ H%F*  + 0,F  $ $2H,<4L MM0=+6,8-:,8+62D
 /ID+,6v6O /4D+.(@%04"5o"F1155jAXM##**+?@##**+?@
 ")rM   c                 N    | j                   j                   d| j                  dS )N<>)	__class__rO   r^   rH   s    rJ   __repr__zRedis.__repr__S  s'    ..))*!D,@,@+C1EErM   c                 >    | j                         j                         S rE   )
initialize	__await__r   s    rJ   r   zRedis.__await__V  s     **,,rM   rH   c                   K   | j                   r[| j                  4 d {    | j                  (| j                  j	                  d       d {   | _        d d d       d {    | S | S 7 L7 7 # 1 d {  7  sw Y   | S xY ww)N_)rZ   r   r   r^   get_connectionr   s    rJ   r   zRedis.initializeY  s     ((-- U U??*,0,@,@,O,OPS,T&TDOU U tU&TU U U U sV   BA,B+A2A.	A2B#A0$	B.A20B2B8A;9B Bcommandcallbackc                 "    || j                   |<   y)zSet a custom Response CallbackN)rX   )rH   r   r   s      rJ   set_response_callbackzRedis.set_response_callback`  s    +3(rM   c                 6    | j                   j                         S )z!Get the connection pool's encoder)r^   get_encoderr   s    rJ   r   zRedis.get_encoderd  s    ##//11rM   c                 .    | j                   j                  S )z'Get the connection's key-word arguments)r^   r   r   s    rJ   get_connection_kwargszRedis.get_connection_kwargsh  s    ##555rM   r   c                 @    | j                         j                  d      S Nr   )r   r   r   s    rJ   	get_retryzRedis.get_retryl  s    ))+//88rM   c                 |    | j                         j                  d|i       | j                  j                  |       y r   )r   r   r^   	set_retry)rH   r   s     rJ   r   zRedis.set_retryo  s2    ""$++We,<=&&u-rM   c                     t        | ||       y)a  
        This function can be used to add externally defined redis modules,
        and their namespaces to the redis client.

        funcname - A string containing the name of the function to create
        func - The function, being added to this class.

        ex: Assume that one has a custom redis module named foomod that
        creates command named 'foo.dothing' and 'foo.anotherthing' in redis.
        To load function functions into this namespace:

        from redis import Redis
        from foomodule import F
        r = Redis()
        r.load_external_module("foo", F)
        r.foo().dothing('your', 'arguments')

        For a concrete example see the reimport of the redisjson module in
        tests/test_connection.py::test_loading_external_modules
        N)setattr)rH   funcnamefuncs      rJ   load_external_modulezRedis.load_external_modules  s    * 	h%rM   transaction
shard_hintPipelinec                 F    t        | j                  | j                  ||      S )a_  
        Return a new pipeline object that can queue multiple commands for
        later execution. ``transaction`` indicates whether all commands
        should be executed atomically. Apart from making a group of operations
        atomic, pipelines are useful for reducing the back-and-forth overhead
        between the client and server.
        )r   r^   rX   )rH   r   r   s      rJ   pipelinezRedis.pipeline  s&       $"9"9;

 	
rM   )r   value_from_callablewatch_delayr   watchesr   r   c                  K   | j                  d|      4 d{   }	 	 |r |j                  |  d{     ||      }t        j                  |      r
| d{   }|j	                          d{   }|r|n|	 cddd      d{    S 7 v7 ]7 87 "7 # t
        $ r( |#|dkD  rt        j                  |       d{  7   Y w xY w# 1 d{  7  sw Y   yxY ww)z
        Convenience method for executing the callable `func` as a transaction
        while watching all keys specified in `watches`. The 'func' callable
        should expect a single argument which is a Pipeline object.
        TNr   )r   watchinspectisawaitableexecuter0   r   sleep)	rH   r   r   r   r   r   pipe
func_value
exec_values	            rJ   r   zRedis.transaction  s      ==z2 	 	d(djj'222!%dJ**:6+5%5
'+||~!5J)<:*L	 	 	 3 &6!5	 " ".;?%mmK888	 	 	s   C#BC#CBB&BBB3B4	B>C#
BC#BBBC#(CCCC
CCC CC C#nametimeoutr   blockingblocking_timeout
lock_classthread_localc           	      0    |t         } || ||||||      S )aL  
        Return a new Lock object using key ``name`` that mimics
        the behavior of threading.Lock.

        If specified, ``timeout`` indicates a maximum life for the lock.
        By default, it will remain locked until release() is called.

        ``sleep`` indicates the amount of time to sleep per loop iteration
        when the lock is in blocking mode and another client is currently
        holding the lock.

        ``blocking`` indicates whether calling ``acquire`` should block until
        the lock has been acquired or to fail immediately, causing ``acquire``
        to return False and the lock not being acquired. Defaults to True.
        Note this value can be overridden by passing a ``blocking``
        argument to ``acquire``.

        ``blocking_timeout`` indicates the maximum amount of time in seconds to
        spend trying to acquire the lock. A value of ``None`` indicates
        continue trying forever. ``blocking_timeout`` can be specified as a
        float or integer, both representing the number of seconds to wait.

        ``lock_class`` forces the specified lock implementation. Note that as
        of redis-py 3.0, the only lock class we implement is ``Lock`` (which is
        a Lua-based lock). So, it's unlikely you'll need this parameter, unless
        you have created your own custom lock class.

        ``thread_local`` indicates whether the lock token is placed in
        thread-local storage. By default, the token is placed in thread local
        storage so that a thread only sees its token, not a token set by
        another thread. Consider the following timeline:

            time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
                     thread-1 sets the token to "abc"
            time: 1, thread-2 blocks trying to acquire `my-lock` using the
                     Lock instance.
            time: 5, thread-1 has not yet completed. redis expires the lock
                     key.
            time: 5, thread-2 acquired `my-lock` now that it's available.
                     thread-2 sets the token to "xyz"
            time: 6, thread-1 finishes its work and calls release(). if the
                     token is *not* stored in thread local storage, then
                     thread-1 would see the token value as "xyz" and would be
                     able to successfully release the thread-2's lock.

        In some use cases it's necessary to disable thread local storage. For
        example, if you have code where one thread acquires a lock and passes
        that lock instance to a worker thread to release later. If thread
        local storage isn't disabled in this case, the worker thread won't see
        the token set by the thread that acquired the lock. Our assumption
        is that these cases aren't common and as such default to using
        thread local storage.)r   r   r   r   r   r   )rH   r   r   r   r   r   r   r   s           rJ   lockz
Redis.lock  s2    | J-%
 	
rM   c                 .    t        | j                  fi |S )z
        Return a Publish/Subscribe object. With this object, you can
        subscribe to channels and listen for messages that get published to
        them.
        )PubSubr^   )rH   rI   s     rJ   pubsubzRedis.pubsub  s     d**5f55rM   c                 ,    t        | j                        S rE   )Monitorr^   r   s    rJ   monitorzRedis.monitor  s    t++,,rM   c                 <    | j                  | j                  d      S )NTr]   )r   r^   r   s    rJ   re   zRedis.client
  s#    ~~ 004  
 	
rM   c                 >   K   | j                          d {   S 7 wrE   )r   r   s    rJ   
__aenter__zRedis.__aenter__  s     __&&&&s   c                 @   K   | j                          d {    y 7 wrE   acloserH   exc_type	exc_value	tracebacks       rJ   	__aexit__zRedis.__aexit__       kkm   zUnclosed Redis client_warn_grlc                     t        | d      rF| j                  9 |d| t        |        	 | | j                  d} |       j	                  |       y y y # t
        $ r Y y w xY w)Nr   zUnclosed client session )source)re   message)hasattrr   ResourceWarning_DEL_MESSAGEcall_exception_handlerRuntimeError)rH   r   r   contexts       rJ   __del__zRedis.__del__  sm    
 4&DOO,G,TH5tT%)d6G6GH--g6	 -H&
   s   %A 	A A close_connection_poolc                    K   | j                   }|r*d| _         | j                  j                  |       d{    |s|0| j                  r#| j                  j	                          d{    yyy7 97 	w)aB  
        Closes Redis client connection

        :param close_connection_pool: decides whether to close the connection pool used
        by this Redis client, overriding Redis.auto_close_connection_pool. By default,
        let Redis.auto_close_connection_pool decide whether to close the connection
        pool.
        N)r   r^   releaser[   
disconnect)rH   r   conns      rJ   r   zRedis.aclose&  sr      "DO&&..t444 !)d.M.M&&11333 /N) 5 4s!   4A4A01A4(A2)A42A45.0.0Use aclose() insteadcloseversionreasonr   c                 B   K   | j                  |       d{    y7 w)zA
        Alias for aclose(), for backwards compatibility
        Nr   )rH   r   s     rJ   r   zRedis.close8  s     
 kk/000   c                 x   K    |j                   |  d{     | j                  ||fi | d{   S 7  7 w)z7
        Send a command and parse the response
        N)send_commandparse_response)rH   r   command_nameargsoptionss        rJ   _send_command_parse_responsez"Redis._send_command_parse_response?  sD       d&&&(T((|GwGGG 	'Gs   :6:8::r   errorc                    K   |j                          d{    |j                  !t        |t        |j                              du r|y7 4w)z
        Close the connection and raise an exception
        if retry_on_error is not set or the error
        is not one of the specified error types
        NF)r   rz   
isinstancetuplerH   r   r  s      rJ   _disconnect_raisezRedis._disconnect_raiseF  sN      oo'%t':':!;<EK F 	 s   AA5Ac                    K    j                          d{     j                  }d    j                  xs  |j                  fi  d{    j                  r" j
                  j                          d{    	 j                  j                   fd fd       d{    j                  r j
                  j                           j                  s|j                         d{    S S 7 7 7 7 V7 #  j                  r j
                  j                           j                  s|j                         d{  7   w w xY ww)z.Execute a command and return a parsed responseNr   c                  2     j                   g i S rE   r  r  r  r   r  rH   s   rJ   <lambda>z'Redis.execute_command.<locals>.<lambda>_  s*    999,)-18 rM   c                 (    j                  |       S rE   )r
  r  r   rH   s    rJ   r  z'Redis.execute_command.<locals>.<lambda>b  s    d44T5A rM   )
r   r^   r   r   rZ   r   acquirer   call_with_retryr   )rH   r  r  poolr  r   s   ``` @@rJ   execute_commandzRedis.execute_commandT  s,    oo##AwT(;(;(;L(TG(T"T((((00222	)33 B	  ,,&&..0??ll4((( ## 	  #U 3 ) ,,&&..0??ll4((( #s}   E D9E D-E DE 	*D 3D
4D 7AE =D>E E E 
D E AEEEE r   r  c                   K   	 t         |v r0|j                  d       d{   }|j                  t                n|j                          d{   }t        |v r|j                  t               || j
                  v rGt        t        |      } | j
                  |   |fi |}t        j                  |      r
| d{   S |S |S 7 7 z# t        $ r t        |v r|t           cY S  w xY w7 -w)z'Parses a response from the Redis serverT)disable_decodingN)
r    read_responsepopr.   r   rX   r   strr   r   )rH   r   r  r  rC   retvals         rJ   r   zRedis.parse_responsej  s     		w&!+!9!94!9!PPL)!+!9!9!;; W$KK'4222\2L:T,,\:8OwOF#*#6#6v#><JFJ# Q < 	(~..	  sW   C/C C-C C
C A+C/?C- C/C 
C C*&C/(C**C/)FN)r   r   rf   N)TN)Ng?TNNT)rf   r   )rf   r   )rf   r>   rE   )?rO   rP   rQ   __doc__r   r   r  bytesResponseCallbackT__annotations__classmethodboolr   r`   r   r   rh   r7   intfloatr   listr   r)   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r3   r   r   r   r   r   re   r   r   r   ra   rb   r   get_running_loopr   r   r6   r   r  r   	Exceptionr
  r  r   rF   rM   rJ   r>   r>   _   s7   
 'uS%Z'8:K'KLL */59	BB #'B %-TN	B BH ']' 
 $  "&*.26+/NR48*.'!&!&)-%)&*'&*%)#()-).%&%)",%4%6"&!%59<@"#IL0 L0 	L0
 #s(OL0 3-L0 !L0 !)L0 #4.L0 #+73c5j8I3I+J"KL0 ".1L0 #3-L0 L0 L0 L0  !L0" !#L0$ %L0& c]'L0( sm)L0* +L0, sm-L0. c]/L00 !1L02 "#3L04 #'5L06  #7L08 c]9L0: 3-;L0< c]=L0> 3-?L0@ AL0B %-TNCL0F &&89GL0H 3-IL0\F-w 7 4S 4<M 42698G, 9.&0 EI

4<SM
	
$ %)$)'+
|U3	#+>%??@  SM	
 " e_@ $(,0+/!H
H
 %H
 	H

 H
 #5/H
 T$Z(H
 H
 
H
T6-

'w '7 ' +L ]],,  
	4(4. 4D 4$ 1GgV1$ 14 1 W1HJ y ),$49#u*4ErM   c                   J    e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   y)MonitorCommandInfotimero   client_addressclient_portclient_typer   N)rO   rP   rQ   r#  r  r"  r  rF   rM   rJ   r(  r(    s#    
KGLrM   r(  c                       e Zd ZdZ ej
                  d      Z ej
                  d      ZdefdZ	d Z
d Zd Zd	efd
Zd	ee   fdZy)r   z
    Monitor is useful for handling the MONITOR command to the redis server.
    next_command() method returns one command from monitor
    listen() method yields commands from monitor.
    z\[(\d+) (.*?)\] (.*)z"(.*?)(?<!\\)"r^   c                      || _         d | _        y rE   )r^   r   )rH   r^   s     rJ   r   zMonitor.__init__  s    .04rM   c                 z   K   | j                   )| j                  j                  d       d {   | _         y y 7 w)NMONITOR)r   r^   r   r   s    rJ   connectzMonitor.connect  s3     ??"$($8$8$G$G	$RRDO #Rs   +;9;c                   K   | j                          d {    | j                  j                  d       d {    | j                  j                          d {   }t	        |      st        d|       | S 7 d7 C7 #w)Nr0  zMONITOR failed: )r1  r   r   r  r   r-   rH   rC   s     rJ   r   zMonitor.__aenter__  so     llnoo**95556688x /z:;; 	58s1   BA;"BA=!BA? B=B?Bc                    K   | j                   j                          d {    | j                  j                  | j                          d {    y 7 27 wrE   )r   r   r^   r   )rH   r  s     rJ   r   zMonitor.__aexit__  sC     oo((***""**4??;;; 	+;s!   AA,AAAArf   c                   K   | j                          d{    | j                  j                          d{   }t        |t              r'| j                  j
                  j                  |d      }|j                  dd      \  }}| j                  j                  |      }|j                         \  }}}dj                  | j                  j                  |            }|j                  dd      }|dk(  rd}d	}	d}
n2|j                  d
      r
d
}|dd }	d
}
n|j!                  dd      \  }}	d}
t#        |      t%        |      ||	|
|dS 7 77 w)z)Parse the response from a monitor commandNTforce    z\""lua unix   :tcp)r)  ro   r*  r+  r,  r   )r1  r   r  r  r  encoderdecodesplit
monitor_rematchgroupsjoin
command_refindallreplace
startswithrsplitr#  r"  )rH   rC   command_timecommand_datamdb_idclient_infor   r*  r+  r,  s              rJ   next_commandzMonitor.next_command  sC    lln6688h&..55hd5KH%-^^C%;"lOO!!,/&'hhj#{G((4??227;< //%-%"NKK##F+#N%ab/K K +6*<*<S!*D'NKK,'e*,&&
 	
3 	8s    EE!EEDEEc                H   K   	 | j                          d{    7 w)z)Listen for commands coming to the server.N)rR  r   s    rJ   listenzMonitor.listen  s#     ))+++ +s   " 	"N)rO   rP   rQ   r  recompilerD  rH  r   r   r1  r   r   r(  rR  r   rT  rF   rM   rJ   r   r     sg     34J-.J5 5S<"
$6 "
H,m,>? ,rM   r   c            
          e Zd ZdZdZdZdZ	 	 	 	 d4dedee	   de
d	ee   fd
Zd Zd Zd Zd Z eddd      d5d       Z eddd      d5d       ZdefdZed        ZdefdZd Zd Zd Zd6de
d efd!Zd" Zd#e de fd$Z!de"d%e#fd&Z$de"de%fd'Z&de"d%efd(Z'de%fd)Z(de)fd*Z*	 d7de
d ee   fd+Z+d8de%fd,Z,d9d-Z-dd.d/d0ed1   d2eddfd3Z.y):r   a*  
    PubSub provides publish, subscribe and listen support to Redis channels.

    After subscribing to one or more channels, the listen() method will block
    until a message arrives on one of the subscribed channels. That message
    will be returned and it's safe to start listening again.
    )r   pmessage)unsubscribepunsubscribezredis-py-health-checkNr^   r   ignore_subscribe_messagespush_handler_funcc                 r   || _         || _        || _        d | _        || _        || _        | j                  | j                   j                         | _        | j                  j                  r d| j                  g| j                  g| _	        nQd| j                  j                  | j                        g| j                  j                  | j                        g| _	        | j
                  
t                i | _        t               | _        i | _        t               | _        t#        j$                         | _        y )Npong   pong)r^   r   r[  r   rA  r\  r   rx   HEALTH_CHECK_MESSAGEhealth_check_responseencoder5   channelssetpending_unsubscribe_channelspatternspending_unsubscribe_patternsr   r   _lock)rH   r^   r   r[  rA  r\  s         rJ   r   zPubSub.__init__  s     /$)B& !2<<//;;=DL<<((223))*D& $,,--d.G.GHI##D$=$=>*D& !!),/E),/E)\\^
rM   c                    K   | S wrE   rF   r   s    rJ   r   zPubSub.__aenter__
          c                 @   K   | j                          d {    y 7 wrE   r   r   s       rJ   r   zPubSub.__aexit__  r   r   c                 h    | j                   r&| j                   j                  | j                         y y rE   )r   _deregister_connect_callback
on_connectr   s    rJ   r   zPubSub.__del__  s$    ??OO88I rM   c                   K   t        | d      sy | j                  4 d {    | j                  r{| j                  j                          d {    | j                  j	                  | j
                         | j                  j                  | j                         d {    d | _        i | _        t               | _
        i | _        t               | _        d d d       d {    y 7 7 7 L7 # 1 d {  7  sw Y   y xY ww)Nr   )r   rh  r   r   rn  ro  r^   r   rc  rd  re  rf  rg  r   s    rJ   r   zPubSub.aclose  s      t\*:: 		6 		6oo00222<<T__M**224??CCC"&DM03D-DM03D-		6 		6 		62C			6 		6 		6 		6si   DC)D*C1C+AC1 C-!7C1D#C/$D+C1-C1/D1D7C:8D?Dr   r   r   r   rf   c                 @   K   | j                          d{    y7 wz/Alias for aclose(), for backwards compatibilityNr   r   s    rJ   r   zPubSub.close%        kkmr   resetc                 @   K   | j                          d{    y7 wrr  r   r   s    rJ   rt  zPubSub.reset*  rs  r   r   c                 &  K   | j                   j                          | j                  j                          | j                  r^i }| j                  j	                         D ]%  \  }}||| j
                  j                  |d      <   '  | j                  di | d{    | j                  r_i }| j                  j	                         D ]%  \  }}||| j
                  j                  |d      <   '  | j                  di | d{    yy7 p7 w)zBRe-subscribe to any channels and patterns previously subscribed toTr6  NrF   )
re  clearrg  rc  itemsrA  rB  	subscriberf  
psubscribe)rH   r   rc  kvrf  s         rJ   ro  zPubSub.on_connect/  s    
 	))//1))//1==H++- A1?@,,Qd,;<A $..,8,,,==H++- A1?@,,Qd,;<A!$//-H---	  -
 .s%   BDDA)DDDDc                 H    t        | j                  xs | j                        S )z@Indicates if there are subscriptions to any channels or patterns)r!  rc  rf  r   s    rJ   
subscribedzPubSub.subscribedA  s     DMM2T]]33rM   r  c                    K   | j                          d{    | j                  }d| j                   i} | j                  ||j                  g|i | d{    y7 I7 w)z#Execute a publish/subscribe commandNcheck_health)r1  r   r~  _executer   )rH   r  r   rI   s       rJ   r  zPubSub.execute_commandF  sc      lln__
 doo"56dmmJ
(?(?Q$Q&QQQ 	 	Rs"   A$A AA$A"A$"A$c                   K   | j                   Y| j                  j                  d| j                         d{   | _         | j                   j	                  | j
                         n"| j                   j                          d{    | j                  7t        s0| j                   j                  j                  | j                         yyy7 7 Jw)z5
        Ensure that the PubSub is connected
        Nr   )r   r^   r   r   _register_connect_callbackro  r1  r\  r4   _parserset_push_handlerr   s    rJ   r1  zPubSub.connectR  s      ??"$($8$8$G$G$//% DO
 OO66tG//))+++!!-6GOO##44T5K5KL 7H- ,s#   6CCACCA	CCc                    K   |j                          d{    |j                  rt        |t              s||j	                          d{    y7 ;7 w)z
        Close the connection and raise an exception
        if retry_on_timeout is not set or the error
        is not a TimeoutError. Otherwise, try to reconnect
        N)r   ry   r  r/   r1  r	  s      rJ   _disconnect_raise_connectz PubSub._disconnect_raise_connectb  sI      oo%%*UL*IKlln 	  	s!   AA5AAAAc                 r    K   j                   j                  fd fd       d{   S 7 w)aU  
        Connect manually upon disconnection. If the Redis server is down,
        this will fail and raise a ConnectionError as desired.
        After reconnection, the ``on_connect`` callback should have been
        called by the # connection to resubscribe us to any channels and
        patterns we were previously listening to
        c                        i S rE   rF   )r  r   rI   s   rJ   r  z!PubSub._execute.<locals>.<lambda>v  s    GT,V, rM   c                 (    j                  |       S rE   )r  r  s    rJ   r  z!PubSub._execute.<locals>.<lambda>w  s    $88uE rM   N)r   r  )rH   r   r   r  rI   s   `````rJ   r  zPubSub._executem  s0      ZZ//,E
 
 	
 
s   )757blockr   c                 V  K   | j                   }|t        d      | j                          d{    |j                  s|j	                          d{    |rdn|}| j                  ||j                  |dd       d{   }|j                  r|| j                  v ry|S 7 s7 Q7 %w)z3Parse the response from a publish/subscribe commandNNpubsub connection not set: did you forget to call subscribe() or psubscribe()?FT)r   disconnect_on_errorpush_request)	r   r   r  is_connectedr1  r  r  r   ra  )rH   r  r   r   read_timeoutrC   s         rJ   r   zPubSub.parse_responsez  s     <F 
 !!!  ,,.  $t'  % ' 
 
 %%(d6P6P*P# 	" !
s3   -B)B##B)B%-B)B'"B)%B)'B)c                   K   | j                   }|t        d      |j                  rWt        j                         j                         |j                  kD  r'|j                  d| j                  d       d {    y y y 7 w)Nr  PINGF)r  )	r   r   r   r   r%  r)  next_health_checkr   r`  )rH   r   s     rJ   r  zPubSub.check_health  s     <F  &&((*//1D4J4JJ##11 $    K 's   A6B8B 9Bdatac           	          | j                   j                  }| j                   j                  }|j                         D ci c]  \  }} | ||            | c}}S c c}}w )z
        normalize channel/pattern names to be either bytes or strings
        based on whether responses are automatically decoded. this saves us
        from coercing the value for each message coming in.
        )rA  rb  rB  rx  )rH   r  rb  rB  r{  r|  s         rJ   _normalize_keyszPubSub._normalize_keys  sN     $$$$15>Avay!1$>>>s    ArI   c                 l  K   |rt        |d   f|dd       n|}t        j                  |      }|j                  |        | j                  dg|j                           d{   }| j                  |      }| j                  j                  |       | j                  j                  |       |S 7 Mw)aE  
        Subscribe to channel patterns. Patterns supplied as keyword arguments
        expect a pattern name as the key and a callable as the value. A
        pattern's callable will be invoked automatically when a message is
        received on that pattern rather than producing a message via
        ``listen()``.
        r   r9  N
PSUBSCRIBE)
r&   dictfromkeysr   r  keysr  rf  rg  difference_update)rH   r  rI   parsed_argsnew_patternsret_vals         rJ   rz  zPubSub.psubscribe  s      =AlDG:tABx8d6:mmK6PF#,,,\PL<M<M<OPP ++L9\*));;LI Q   A"B4$B2%AB4c                 
   |rFt        |d   f|dd       }| j                  t        j                  |            j	                         }ng }| j
                  }| j                  j                  |        | j                  dg| S )zj
        Unsubscribe from the supplied patterns. If empty, unsubscribe from
        all patterns.
        r   r9  NPUNSUBSCRIBE)	r&   r  r  r  r  rf  rg  r   r  )rH   r  r  rf  s       rJ   rZ  zPubSub.punsubscribe  s{     &Qz48<K++DMM+,FGLLNHK}}H))00:#t##NA[AArM   c                 l  K   |rt        |d   f|dd       nd}t        j                  |      }|j                  |        | j                  dg|j                           d{   }| j                  |      }| j                  j                  |       | j                  j                  |       |S 7 Mw)aR  
        Subscribe to channels. Channels supplied as keyword arguments expect
        a channel name as the key and a callable as the value. A channel's
        callable will be invoked automatically when a message is received on
        that channel rather than producing a message via ``listen()`` or
        ``get_message()``.
        r   r9  NrF   	SUBSCRIBE)
r&   r  r  r   r  r  r  rc  re  r  )rH   r  rI   r  new_channelsr  s         rJ   ry  zPubSub.subscribe  s      =AlDG:tABx8b}}[1F#,,,[O<;L;L;NOO ++L9\*));;LI Pr  c                     |r7t        |d   |dd       }| j                  t        j                  |            }ng }| j                  }| j
                  j                  |        | j                  dg| S )zi
        Unsubscribe from the supplied channels. If empty, unsubscribe from
        all channels
        r   r9  NUNSUBSCRIBE)r&   r  r  r  rc  re  r   r  )rH   r  r  rc  s       rJ   rY  zPubSub.unsubscribe  sp    
 &tAwQR9K++DMM+,FGHK}}H))00:#t##M@K@@rM   c                   K   | j                   rF| j                  | j                  d       d{          d{   }|| | j                   rEyy7 &7 w)zBListen for messages on channels this client has been subscribed toT)r  N)r~  handle_messager   r3  s     rJ   rT  zPubSub.listen  sM     oo!00t7J7JQU7J7V1VWWH# oo1VWs%   -AAAAAAAc                    K   | j                  |du |       d{   }|r| j                  ||       d{   S y7 !7 w)a  
        Get the next message if one is available, otherwise None.

        If timeout is specified, the system will wait for `timeout` seconds
        before returning. Timeout should be specified as a floating point
        number or None to wait indefinitely.
        N)r  r   )r   r  )rH   r[  r   rC   s       rJ   get_messagezPubSub.get_message  sK      ,,GtOg,VV,,X7PQQQ WQs   A=A?AAc                 2    |d|gndg} | j                   | S )z'
        Ping the Redis server
        r  r  )rH   r   r  s      rJ   pingzPubSub.ping  s*     %,$7 fX#t##T**rM   c                 V  K   |yt        |t              r|dk7  rd|gnddg}t        |d         }|dk(  r||d   |d   |d	   d
}n|dk(  r|dd|d   d
}n|d|d   |d   d
}|| j                  v r|dk(  rK|d   }|| j                  v r| j                  j                  |       | j                  j                  |d       nJ|d   }|| j                  v r7| j                  j                  |       | j                  j                  |d       || j                  v rw|dk(  r | j                  j                  |d   d      }n| j                  j                  |d   d      }|r/t        j                  |      r ||       d{    y ||       y|S |dk7  r|s| j                  ry|S 7 &w)z
        Parses a pub/sub message. If the channel or pattern was subscribed to
        with a message handler, the handler is invoked instead of a parsed
        message being returned.
        Ns   PONGr_  rM   r   rX  r9  rl   r   )typepatternchannelr  r^  rZ  r  r  )r  r  r9   UNSUBSCRIBE_MESSAGE_TYPESrg  removerf  r  re  rc  PUBLISH_MESSAGE_TYPESr   r   iscoroutinefunctionr[  )rH   rC   r[  message_typer   r  r  handlers           rJ   r  zPubSub.handle_message  s     h&.6'.A*QT~H#HQK0:%$#A;#A; 	G V#$ 	G %#A; 	G 4999~-"1+d???55<<WEMM%%gt4"1+d???55<<WEMM%%gt44555z)--++GI,>E--++GI,>E..w7!'***  G$  V# )D,J,J +s   E>F) F''F)g      ?)exception_handlerpoll_timeoutr  PSWorkerThreadExcHandlerTr  c                2  K   | j                   j                         D ]  \  }}|	t        d| d       | j                  j                         D ]  \  }}|	t        d| d       | j	                          d{    	 	 | j                  d|       d{    t        j                  d       d{    :7 ?7 $# t        j                  $ r  t        $ r6}|  |||       }t        j                  |      r| d{  7   Y d}~od}~ww xY w7 ^w)a  Process pub/sub messages using registered callbacks.

        This is the equivalent of :py:meth:`redis.PubSub.run_in_thread` in
        redis-py, but it is a coroutine. To launch it as a separate task, use
        ``asyncio.create_task``:

            >>> task = asyncio.create_task(pubsub.run())

        To shut it down, use asyncio cancellation:

            >>> task.cancel()
            >>> await task
        Nz
Channel: 'z' has no handler registeredz
Pattern: 'T)r[  r   r   )rc  rx  r,   rf  r1  r  r   CancelledErrorBaseExceptionr   r   r   )rH   r  r  r  r  r  eress           rJ   runz
PubSub.runV  s6    & !% 3 3 5 	UGW!Jwi7R"STT	U !% 3 3 5 	UGW!Jwi7R"STT	U lln&&.2L '    --"""  	 ))   $,'40&&s+II #sp   #D2D#D<B<=DC  B>C  D6D7D>C   D&DDDDDD)NFNNrf   N)Tr   )Fg        rE   )F)/rO   rP   rQ   r  r  r  r`  r   r   r  r!  r   r   r   r   r   r   r6   r   rt  r   ro  propertyr~  r2   r  r1  r  r  r#  r   r  r?   r  r1   PubSubHandlerrz  r   rZ  ry  rY  r   rT  r  r  r  r  rF   rM   rJ   r   r     s    4 ?2
 %)*/04"$'"$ SM"$ $(	"$ $H-"$HJ6" 1GgV W 1GgV W.: .$ 4 4
R: 
RM 	
$  8 ?O ? ?h - *B( By BX  *AI Am  SV)-@H+I +@J DH!	*# $$?@*# 	*#
 
*#rM   r   c                       e Zd ZdedefdZy)PubsubWorkerExceptionHandlerr  r   c                      y rE   rF   rH   r  r   s      rJ   rK   z%PubsubWorkerExceptionHandler.__call__  rL   rM   NrO   rP   rQ   r  r   rK   rF   rM   rJ   r  r    s    -  rM   r  c                       e Zd ZdedefdZy)!AsyncPubsubWorkerExceptionHandlerr  r   c                    K   y wrE   rF   r  s      rJ   rK   z*AsyncPubsubWorkerExceptionHandler.__call__  rU   rV   Nr  rF   rM   rJ   r  r    s     v rM   r  .c            	       x    e Zd ZdZh dZdedeeee	f   e
f   dedee   fdZded	efd
Zd Zd ZdZd Zd Zd Zd Zd/dZd Zd	ed ed    f   fdZd Zd Zd ZdedefdZ dededefdZ!dede"e#   fdZ$d e%d!e&d"e"e'   d	dfd#Z(ded$eee	f   f fd%Z)d& Z*d'ed(e%fd)Z+d0defd*Z,d+ Z-d,e.fd-Z/d. Z0 xZ1S )1r   a_  
    Pipelines provide a way to transmit multiple commands to the Redis server
    in one transmission.  This is convenient for batch processing, such as
    saving all the values in a list to Redis.

    All commands executed within a pipeline are wrapped with MULTI and EXEC
    calls. This guarantees all commands executed in the pipeline will be
    executed atomically.

    Any command raising an exception does *not* halt the execution of
    subsequent commands in the pipeline. Instead, the exception is caught
    and its instance is placed into the response list returned by execute().
    Code iterating over the response list should be able to deal with an
    instance of an exception as a potential value. In general, these will be
    ResponseError exceptions, such as those raised when issuing a command
    on a key of a different datatype.
    >   EXECDISCARDUNWATCHr^   rX   r   r   c                     || _         d | _        || _        || _        || _        d| _        g | _        t               | _        d| _	        y )NF)
r^   r   rX   is_transactionr   watchingcommand_stackrd  scriptsexplicit_transaction)rH   r^   rX   r   r   s        rJ   r   zPipeline.__init__  sJ      /"4)$,.&)e$)!rM   rH   rf   c                    K   | S wrE   rF   r   s    rJ   r   zPipeline.__aenter__  rj  rk  c                 @   K   | j                          d {    y 7 wrE   rt  r   s       rJ   r   zPipeline.__aexit__  s     jjlr   c                 >    | j                         j                         S rE   )_async_selfr   r   s    rJ   r   zPipeline.__await__  s    !++--rM   zUnclosed Pipeline clientc                 ,    t        | j                        S rE   )lenr  r   s    rJ   __len__zPipeline.__len__  s    4%%&&rM   c                      y)z1Pipeline instances should always evaluate to TrueTrF   r   s    rJ   __bool__zPipeline.__bool__  s    rM   c                    K   | S wrE   rF   r   s    rJ   r  zPipeline._async_self  rj  rk  c                   K   g | _         t               | _        | j                  rR| j                  rF	 | j                  j                  d       d {    | j                  j                          d {    d| _        d| _	        | j                  r5| j                  j                  | j                         d {    d | _        y y 7 v7 V# t        $ r2 | j                  r#| j                  j                          d {  7   Y w xY w7 Ow)Nr  F)r  rd  r  r  r   r   r  r*   r   r  r^   r   r   s    rJ   rt  zPipeline.reset  s     u ==T__7 oo229===oo33555 $)! ??&&..t???"DO  >5" 7??//446667 @se   /DC C!C 2C	3C 7AD9D	:DC 	C 2D=D >DDDDNc                 @   K   | j                          d{    y7 w)z5Alias for reset(), a standard method name for cleanupNr  r   s    rJ   r   zPipeline.aclose  s     jjlr   c                 n    | j                   rt        d      | j                  rt        d      d| _         y)z
        Start a transactional block of the pipeline after WATCH commands
        are issued. End the transactional block with `execute`.
        z"Cannot issue nested calls to MULTIz:Commands without an initial WATCH have already been issuedTN)r  r-   r  r   s    rJ   multizPipeline.multi  s<    
 $$ABBL  %)!rM   c                     | j                   s|d   dk(  r| j                  s | j                  |i |S  | j                  |i |S )Nr   WATCH)r  r  immediate_execute_commandpipeline_execute_command)rH   r  rI   s      rJ   r  zPipeline.execute_command  sN     MMT!W/9R9R14114B6BB,t,,d=f==rM   c                   K   |j                          d{    | j                  r#| j                          d{    t        d      |j                  rt        |t              s| j                          d{     y7 i7 G7 
w)z
        Close the connection, reset watching state and
        raise an exception if we were watching,
        retry_on_timeout is not set,
        or the error is not a TimeoutError
        N=A ConnectionError occurred on while watching one or more keys)r   r  r   r0   ry   r  r/   r	  s      rJ   _disconnect_reset_raisez Pipeline._disconnect_reset_raise  s}      oo ==++-O 
 %%*UL*I++- +J 	 
    s1   BB #BB>B9B:BBBc                 
   K   d    j                   s5 j                  j                   j                         d{    _         j                  j                   fd fd       d{   S 7 :7 w)z
        Execute a command immediately, but don't auto-retry on a
        ConnectionError if we're already WATCHing a variable. Used when
        issuing WATCH or subsequent commands retrieving their values but before
        MULTI is called.
        r   Nc                  2     j                   g i S rE   r  r  s   rJ   r  z4Pipeline.immediate_execute_command.<locals>.<lambda>(  s*    5D55l%)-4 rM   c                 (    j                  |       S rE   )r  r  s    rJ   r  z4Pipeline.immediate_execute_command.<locals>.<lambda>+  s    $66tUC rM   )r   r^   r   r   r   r  )rH   r  r  r  r   s   ```@@rJ   r  z"Pipeline.immediate_execute_command  s}      Aw--<<doo D #DOZZ// D	
 
 	


s#   =BA?5B:B;BBc                 @    | j                   j                  ||f       | S )ar  
        Stage a command to be executed when execute() is next called

        Returns the current Pipeline object back so commands can be
        chained together, such as:

        pipe = pipe.set('foo', 'bar').incr('baz').decr('bang')

        At some other point, you can then run: pipe.execute(),
        which will execute all commands queued in the pipe.
        )r  r   )rH   r  r  s      rJ   r  z!Pipeline.pipeline_execute_command.  s!     	!!4/2rM   r   commandsc                   K   di f}di f}|g||}|j                  d |D              }|j                  |       d {    g }	 | j                  |d       d {    t        |      D ]I  \  }
}t        |d   v r|j	                  |
|d   t           f       /	 | j                  |d       d {    K 	 | j                  |d       d {   }d| _	        |t        d      d |D ]  \  }
}|j                  |
|        t        |      t        |      k7  r:| j                  r"| j                  j                          d {    t        d	      d |r| j                  ||       g }t!        ||      D ]s  \  }}t#        |t$              sM|\  }}|d   }|| j&                  v r5 | j&                  |   |fi |}t)        j*                  |      r
| d {   }|j	                  |       u |S 7 7 # t        $ r}	|j	                  d|	f       Y d }	~	d }	~	ww xY w7 n# t        $ r7}	| j                  |	|
dz   |d          |j	                  |
|	f       Y d }	~	d }	~	ww xY w7 # t        $ r}	|r	|d   d   |	 d }	~	ww xY w7 A7 w)
N)MULTI)r  c              3   8   K   | ]  \  }}t         |vs|  y wrE   )r   ).0r  r  s      rJ   	<genexpr>z0Pipeline._execute_transaction.<locals>.<genexpr>C  s       ,
"T7nG.KD,
s   r   r   r9  FzWatched variable changed.z6Wrong number of response items from pipeline execution)pack_commandssend_packed_commandr   r.   r   	enumerater   annotate_exceptionr+   r  r0   insertr  r   r   raise_first_errorzipr  r&  rX   r   r   )rH   r   r  raise_on_errorprepostcmdsall_cmdserrorserrir   rC   r  r  rcmdr  r  r  s                       rJ   _execute_transactionzPipeline._execute_transaction=  s     $R(#R%h%%++ ,
&*,
 
 ,,X666	$%%j#666
 $H- 	,JAw+q'!*^"<=>,--j#>>>	,	!00SAAH 89tC  	"DAqOOAq!	" x=CM)oo00222H
 ""8X6 (H- 	FAsa+ #g#Aw4#:#::=//=aK7KA**1-"#GKKN	  	7 7 	$MM1c(##	$ ?$ ,++CQ
CMM1c(++, B 	Qil+	" 3$ $s   =J
G/ J
G5 G2G5 !:J
H"1H2H"6J
9I( I%I( A1J
JBJ
JJ
2G5 5	H>HJ
HJ
H""	I"+,IJ
I""J
%I( (	J1I==JJ
J
r  c                   K   |j                  |D cg c]  \  }}|	 c}}      }|j                  |       d {    g }|D ]4  \  }}	 |j                   | j                  ||d   fi | d {          6 |r| j                  ||       |S c c}}w 7 [7 (# t        $ r}	|j                  |	       Y d }	~	vd }	~	ww xY ww)Nr   )r  r  r   r   r.   r  )
rH   r   r  r  r  r   r  rC   r  r  s
             rJ   _execute_pipelinezPipeline._execute_pipeline  s      ++,JgdAT,JK,,X666% 	#MD'#-$--j$q'MWMM	# ""8X6 -K6 N  #""#sU   CB
CBC%B0B
1B9CB	B?$B:5C:B??CrC   c                     t        |      D ]3  \  }}t        |t              s| j                  ||dz   ||   d          | y )Nr9  r   )r  r  r.   r  )rH   r  rC   r  r  s        rJ   r  zPipeline.raise_first_error  sG    h' 	DAq!]+''1q5(1+a.A	rM   	exceptionnumberr   c                     dj                  t        t        |            }d| d| d|j                   }|f|j                  dd  z   |_        y )Nr8  z
Command # z (z) of pipeline caused error: r9  )rG  mapr8   r  )rH   r  r  r   r  msgs         rJ   r  zPipeline.annotate_exception  sP     hhs8W-.6("SE)EinnEUV).."44	rM   r  c                    K   t        |   ||fi | d {   }|| j                  v r	d| _        |S |dk(  rd| _        |S 7 )w)NFr  T)superr   UNWATCH_COMMANDSr  )rH   r   r  r  resultr   s        rJ   r   zPipeline.parse_response  sZ      w-j,R'RR4000!DM  W$ DM Ss   AA*Ac                 :  K   t        | j                        }| j                  }|D cg c]  }|j                   }} |dg|  d {   }t	        |      s8t        ||      D ](  \  }}|r	 |d|j                         d {   |_        * y y c c}w 7 M7 w)NzSCRIPT EXISTSzSCRIPT LOAD)r$  r  r  shaallr  script)rH   r  	immediatesshasexistsexists          rJ   load_scriptszPipeline.load_scripts  s     t||$22	&'!'' !84886{0 E5"+M188"DDAEE 	 ( 9 Es3   &BBB	B
#B.BBBBr   r  c                    K   |j                          d{    | j                  rt        d      |j                  rt	        |t
              s| j                          d{     y7 Q7 w)z
        Close the connection, raise an exception if we were watching,
        and raise an exception if retry_on_timeout is not set,
        or the error is not a TimeoutError
        Nr  )r   r  r0   ry   r  r/   rt  r	  s      rJ   _disconnect_raise_resetz Pipeline._disconnect_raise_reset  sh      oo ==O 
 %%*UL*I**, +J 	  s"   A,A(A
A,!A*"A,*A,c                 v   K    j                   s j                  sg S  j                  r j                          d{     j                  s j
                  r j                  n j                   j                  s5 j                  j                  d j                         d{    _        t        t              	 j                  j                  fd fd       d{    j!                          d{    S 7 7 d7  7 #  j!                          d{  7   w xY ww)z0Execute all the commands in the current pipelineNr  c                              S rE   rF   )r   r   r  stacks   rJ   r  z"Pipeline.execute.<locals>.<lambda>  s    e^< rM   c                 (    j                  |       S rE   )r!  r  s    rJ   r  z"Pipeline.execute.<locals>.<lambda>  s    d::4G rM   )r  r  r  r  r  r  r	  r  r   r^   r   r   r   r   r   r  rt  )rH   r  r   r   r$  s   ``@@@rJ   r   zPipeline.execute  s     ""T]]I<<##%%%$";";//G,,G--<<WdooVVD #DOJ%	33<G 
 **,) & W
 $**,sl   <D9DA,D90D1D9)D 6D7D :D9DD9D9D D9D6/D20D66D9c                 B   K   | j                  d       d{    y7 w)z^Flushes all previously queued commands
        See: https://redis.io/commands/DISCARD
        r  Nr  r   s    rJ   discardzPipeline.discard  s      ""9---r   namesc                 r   K   | j                   rt        d       | j                  dg|  d{   S 7 w)z$Watches the values at keys ``names``z"Cannot issue a WATCH after a MULTIr  N)r  r-   r  )rH   r(  s     rJ   r   zPipeline.watch  s9     $$ABB)T))':E::::s   .757c                 d   K   | j                   xr | j                  d       d{   xs dS 7 w)z'Unwatches all previously specified keysr  NT)r  r  r   s    rJ   unwatchzPipeline.unwatch  s+     }}Ft';';I'F!FN$N!Fs   #0.	0r  )T)2rO   rP   rQ   r  r  r   r   r   r  r  r  r!  r   r   r=   r   r   r   r   r  r  r  rt  r   r  r   r  r  r  r  r   CommandStackTr	  r  r	   r   r  r&  r"  objectr  r   r  r!  r   r'  r3   r   r+  __classcell__)r   s   @rJ   r   r     s   $ 6*'* +5e+<>O+OP* 	*
 SM*"w 7 . .L'#0)>	z9Z00	1>,
.H$H0=HT$0=OS(- 8C= 5"5,/5:B6:J5	5$49#u*4EE* Y (D 8.;$ ;OrM   r   )_r   r   r   rU  ra   typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   redis._parsers.helpersr   r   r   r   redis.asyncio.connectionr   r   r   r   redis.asyncio.lockr   redis.asyncio.retryr   redis.clientr   r    r!   r"   redis.commandsr#   r$   r%   r&   redis.compatr'   r(   redis.credentialsr)   redis.exceptionsr*   r+   r,   r-   r.   r/   r0   redis.typingr1   r2   r3   redis.utilsr4   r5   r6   r7   r8   r9   r  r  r:   r<   r=   r-  r?   redis.commands.corer@   rB   rS   r  r>   StrictRedisr(  r   r   r  r  r  r  CommandTr,  r   rF   rM   rJ   <module>r>     s      	     (   $ %   - 0   4 3  $sCx.)9T?:;t$z*
)7
++78V;K3LM*x 
H 
 24QQR b+->@UbJ  F, F,Rg# g#T8 
 
 " "CC 
 uS%Z(#-.S0AABXgOu gOrM   