
    +h,+                         d dl mZmZmZmZmZmZ d dlmZm	Z	 d dl
mZ erd dlmZ  G d d      Z G d de      Z G d	 d
e      Zy)    )TYPE_CHECKINGAnyDictOptionalTupleUnion)
RedisErrorResponseError)str_if_bytes)ClusterNodec                       e Zd Zd Zd Zy)AbstractCommandsParserc                 
   t        |      dk  ry|D cg c]  }t        |       }}|d   j                         }d}|dk(  r|d   j                         }|dv r|dd }|S |dv rt        |dd       }|S |dv r|d   g}|S c c}w )	z
        Get the keys from pubsub command.
        Although PubSub commands have predetermined key locations, they are not
        supported in the 'COMMAND's output, so the key positions are hardcoded
        in this method
           Nr   PUBSUB   )CHANNELSNUMSUBSHARDCHANNELSSHARDNUMSUB)	SUBSCRIBE
PSUBSCRIBEUNSUBSCRIBEPUNSUBSCRIBE)PUBLISHSPUBLISH)lenr   upperlist)selfargsargcommandkeyspubsub_types         J/var/www/html/venv/lib/python3.12/site-packages/redis/_parsers/commands.py_get_pubsub_keysz'AbstractCommandsParser._get_pubsub_keys   s     t9q=-12cS!22q'--/h q'--/KTTABx  RR QR>D
 	 // G9D# 3s   B c                    i }t        |d         }||d<   t        |d         |d<   |d   D cg c]  }t        |       c}|d<   |d   |d<   |d	   |d
<   |d   |d<   t        |      dkD  r|d   |d<   |d   |d<   |d   |d<   |S c c}w )Nr   namer   arityr   flags   first_key_pos   last_key_pos   
step_count   tips   key_specifications	   subcommands)r   intr   )r    r#   optionscmd_dictcmd_nameflags         r&   parse_subcommandz'AbstractCommandsParser.parse_subcommand(   s    
+#
O<CAJGD\$/G$+AJ!#*1: !(w<!&qzHV-4QZH)*&-ajH]# Hs   BN)__name__
__module____qualname__r'   r=        r&   r   r   
   s    :rB   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)CommandsParsera  
    Parses Redis commands to get command keys.
    COMMAND output is used to determine key locations.
    Commands that do not have a predefined key location are flagged with
    'movablekeys', and these commands' keys are determined by the command
    'COMMAND GETKEYS'.
    c                 4    i | _         | j                  |       y N)commands
initialize)r    redis_connections     r&   __init__zCommandsParser.__init__A   s    ()rB   c                     |j                         }g }|D ]&  }t        d |D              s|j                  |       ( |D ]$  }|j                  |      ||j	                         <   & || _        y )Nc              3   <   K   | ]  }|j                           y wrF   )isupper).0xs     r&   	<genexpr>z,CommandsParser.initialize.<locals>.<genexpr>I   s     ,1199;,s   )r#   anyappendpoplowerrG   )r    rrG   uppercase_commandscmds        r&   rH   zCommandsParser.initializeE   so    99; 	/C,,,"))#.	/ & 	6C$,LL$5HSYY[!	6 rB   c                 r   t        |      dk  ry|d   j                         }|| j                  vrp|j                         }|d   }|| j                  v r|t	        |dd       z   }n;| j                  |       || j                  vrt        |j                          d      | j                  j                  |      }d|d   v r | j                  |g| }|S d|d   v s|d	   dk(  r | j                  | }|S |d
   dk(  r`|d   dk(  rX|d   dk(  rPd}d|v rG| d|d   j                          }|d   D ]'  }	t        |	d         |k(  s| j                  |	      }d}) |sy|d   }
|
dk  rt        |      t        |
      z
  }
t	        t        |d   |
dz   |d
               }|D cg c]  }||   	 }}|S c c}w ap  
        Get the keys from the passed command.

        NOTE: Due to a bug in redis<7.0, this function does not work properly
        for EVAL or EVALSHA when the `numkeys` arg is 0.
         - issue: https://github.com/redis/redis/issues/9493
         - fix: https://github.com/redis/redis/pull/9733

        So, don't use this function with EVAL or EVALSHA.
        r   Nr   r   z( command doesn't exist in Redis commandsmovablekeysr+   pubsubr)   r1   r-   r/   Fr7   |Tr   rT   rG   splitr   rH   r	   r   get_get_moveable_keysr'   r   r=   absrange)r    
redis_connr!   r;   cmd_name_splitr#   r$   	is_subcmdsubcmd_namesubcmdr/   keys_posposs                r&   get_keyszCommandsParser.get_keysR   s    t9q=7==?4==( &^^-N%a(H4==(%T!"X6 
+4==0$#>>+,,TU  --##H-GG,,*4**:==D8 7 ))WV_-H(4(($/D4 / %*O,1N+q0!	 G+%-JaQ/@"AK")-"8 -'q	2kA&*&;&;F&CG(,I- !">2La"4y3|+<<go.q0@',BWXH *22#DI2D2 3s   $F4c                     |d   j                         t        |dd       z   }	  |j                  dg| }|S # t        $ r$}|j	                         }d|v sd|v rY d}~y|d}~ww xY w)aA  
        NOTE: Due to a bug in redis<7.0, this function does not work properly
        for EVAL or EVALSHA when the `numkeys` arg is 0.
         - issue: https://github.com/redis/redis/issues/9493
         - fix: https://github.com/redis/redis/pull/9733

        So, don't use this function with EVAL or EVALSHA.
        r   r   NCOMMAND GETKEYSInvalid arguments The command has no key arguments)r^   r   execute_commandr
   __str__)r    rc   r!   piecesr$   emessages          r&   r`   z!CommandsParser._get_moveable_keys   s{     a4QR>1
	-:--.?I&ID   	iikG#w.5@	s   9 	A&A!A!!A&N)r>   r?   r@   __doc__rJ   rH   rj   r`   rA   rB   r&   rD   rD   8   s    *!?BrB   rD   c                   t    e Zd ZdZdZddZdded   ddfdZd	edee	e
d
f      fdZd	edee	e
d
f      fdZy)AsyncCommandsParsera%  
    Parses Redis commands to get command keys.

    COMMAND output is used to determine key locations.
    Commands that do not have a predefined key location are flagged with 'movablekeys',
    and these commands' keys are determined by the command 'COMMAND GETKEYS'.

    NOTE: Due to a bug in redis<7.0, this does not work properly
    for EVAL or EVALSHA when the `numkeys` arg is 0.
     - issue: https://github.com/redis/redis/issues/9493
     - fix: https://github.com/redis/redis/pull/9733

    So, don't use this with EVAL or EVALSHA.
    )rG   nodereturnNc                     i | _         y rF   )rG   )r    s    r&   rJ   zAsyncCommandsParser.__init__   s	    ?ArB   rw   r   c                    K   |r|| _         | j                   j                  d       d {   }|j                         D ci c]  \  }}|j                         | c}}| _        y 7 <c c}}w w)NCOMMAND)rw   ro   itemsrT   rG   )r    rw   rG   rW   r#   s        r&   rH   zAsyncCommandsParser.initialize   sW     DI229==BJ..BRS,#wg-S >Ss!   (A/A'A/A)A/)A/r!   .c                   K   t        |      dk  ry|d   j                         }|| j                  vrw|j                         }|d   }|| j                  v r|t	        |dd       z   }nB| j                          d{    || j                  vrt        |j                          d      | j                  j                  |      }d|d   v r | j                  |  d{   }|S d|d   v s|d	   dk(  r | j                  | }|S |d
   dk(  r`|d   dk(  rX|d   dk(  rPd}d|v rG| d|d   j                          }|d   D ]'  }t        |d         |k(  s| j                  |      }d}) |sy|d   }	|	dk  rt        |      t        |	      z
  }	t	        t        |d   |	dz   |d
               }
|
D cg c]  }||   	 }}|S 7 F7 c c}w wrY   r]   )r    r!   r;   rd   r#   r$   re   rf   rg   r/   rh   ri   s               r&   rj   zAsyncCommandsParser.get_keys   s%     t9q=7==?4==( &^^-N%a(H4==(%T!"X6 oo'''4==0$#>>+,,TU  --##H-GG,,000$77D8 7 ))WV_-H(4(($/D4 / %*O,1N+q0!	 G+%-JaQ/@"AK")-"8 -'q	2kA&*&;&;F&CG(,I- !">2La"4y3|+<<go.q0@',BWXH *22#DI2D2I ( 84 3s?   A9G;G<A"GGA5GAG1G=GGGc                    K   	  | j                   j                  dg|  d {   }|S 7 # t        $ r$}|j                         }d|v sd|v rY d }~y |d }~ww xY ww)Nrl   rm   rn   )rw   ro   r
   rp   )r    r!   r$   rr   rs   s        r&   r`   z&AsyncCommandsParser._get_moveable_keys  si     
	2223DLtLLD  M 	iikG#w.5@	s<   A - +- A- 	AAAAAA)rx   NrF   )r>   r?   r@   rt   	__slots__rJ   r   rH   r   r   strrj   r`   rA   rB   r&   rv   rv      sp     %IBTXm%< T T?C ?HU38_,E ?Bc huS#X6O rB   rv   N)typingr   r   r   r   r   r   redis.exceptionsr	   r
   redis.utilsr   redis.asyncio.clusterr   r   rD   rv   rA   rB   r&   <module>r      sA    C C 6 $1+ +\r+ rjl0 lrB   