
    Pf,+                         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dS )    )TYPE_CHECKINGAnyDictOptionalTupleUnion)
RedisErrorResponseErrorstr_if_bytes)ClusterNodec                       e Zd Zd Zd ZdS )AbstractCommandsParserc                 0   t          |          dk     rdS d |D             }|d                                         }d}|dk    r)|d                                         }|dv r
|dd         }n)|dv rt          |dd                   }n|d	v r	|d         g}|S )
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
           Nc                 ,    g | ]}t          |          S  r   ).0args     I/var/www/html/env/lib/python3.11/site-packages/redis/_parsers/commands.py
<listcomp>z;AbstractCommandsParser._get_pubsub_keys.<locals>.<listcomp>   s     222cS!!222    r   PUBSUB   )CHANNELSNUMSUBSHARDCHANNELSSHARDNUMSUB)	SUBSCRIBE
PSUBSCRIBEUNSUBSCRIBEPUNSUBSCRIBE)PUBLISHSPUBLISH)lenupperlist)selfargscommandkeyspubsub_types        r   _get_pubsub_keysz'AbstractCommandsParser._get_pubsub_keys   s     t99q==422T222q'--//h q'--//KTTTABBxRRR QRR>>DD/// G9Dr   c                 B   i }t          |d                   }||d<   t          |d                   |d<   d |d         D             |d<   |d         |d	<   |d
         |d<   |d         |d<   t          |          dk    r!|d         |d<   |d         |d<   |d         |d<   |S )Nr   namer   arityc                 ,    g | ]}t          |          S r   r   )r   flags     r   r   z;AbstractCommandsParser.parse_subcommand.<locals>.<listcomp>-   s     GGGD\$//GGGr   r   flags   first_key_pos   last_key_pos   
step_count   tips   key_specifications	   subcommands)r   intr%   )r(   r*   optionscmd_dictcmd_names        r   parse_subcommandz'AbstractCommandsParser.parse_subcommand(   s    
++#
OOGGGAJGGG$+AJ!#*1: !(w<<!&qzHV-4QZH)*&-ajH]#r   N)__name__
__module____qualname__r-   rD   r   r   r   r   r   
   s2          :    r   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )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                 >    i | _         |                     |           d S N)commands
initialize)r(   redis_connections     r   __init__zCommandsParser.__init__A   s"    ()))))r   c                    |                                 }g }|D ]0}t          d |D                       r|                    |           1|D ],}|                    |          ||                                <   -|| _        d S )Nc              3   >   K   | ]}|                                 V  d S rK   )isupper)r   xs     r   	<genexpr>z,CommandsParser.initialize.<locals>.<genexpr>I   s*      ,,1199;;,,,,,,r   )r*   anyappendpoplowerrL   )r(   rrL   uppercase_commandscmds        r   rM   zCommandsParser.initializeE   s    99;; 	/ 	/C,,,,,,, /"))#...% 	6 	6C$,LL$5$5HSYY[[!! r   c                    t                    dk     rdS d                                         }|| j        vr|                                }|d         }|| j        v r|t	          dd                   z   nB|                     |           || j        vr$t          |                                 d          | j                            |          }d|d         v r | j	        |gR  }nd|d         v s|d	         dk    r | j
         }n|d
         dk    r||d         dk    rp|d         dk    rdd}d|v rZ| dd                                          }|d         D ]2}	t          |	d                   |k    r|                     |	          }d}3|sdS |d         }
|
dk     rt                    t          |
          z
  }
t	          t          |d         |
dz   |d
                             }fd|D             }|S )p  
        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   ( command doesn't exist in Redis commandsmovablekeysr3   pubsubr/   r9   r5   r7   Fr?   |Tc                      g | ]
}|         S r   r   r   posr)   s     r   r   z+CommandsParser.get_keys.<locals>.<listcomp>       222#DI222r   r%   rX   rL   splitr'   rM   r	   r&   get_get_moveable_keysr-   r   rD   absrange)r(   
redis_connr)   rC   cmd_name_splitr*   r+   	is_subcmdsubcmd_namesubcmdr7   keys_poss     `         r   get_keyszCommandsParser.get_keysR   sb    t99q==47==??4=(( &^^--N%a(H4=((%T!""X6 
+++4=00$#>>++UUU   -##H--GG,,,*4*:====DD)))WV_-H-H(4($/DD %**O,11N+q00!	 G++%-"A"AQ"A"AK")-"8 - -'q	22kAA&*&;&;F&C&CG(,I !  4">2La"4yy3|+<+<<go.q0@',BWXX H 3222222Dr   c                     |d                                          t          |dd                   z   }	  |j        dg|R  }n6# t          $ r)}|                                }d|v sd|v rY d}~dS |d}~ww xY w|S )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)rg   r'   execute_commandr
   __str__)r(   rl   r)   piecesr+   emessages          r   ri   z!CommandsParser._get_moveable_keys   s     a4QRR>>1
	-:-.?I&IIIDD 	 	 	iikkG#w..5@@ttttt	 s   A 
A6A1/A11A6N)rE   rF   rG   __doc__rO   rM   rr   ri   r   r   r   rI   rI   8   s[         * * *! ! !? ? ?B    r   rI   c                       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dS )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.
    )rL   nodereturnNc                     i | _         d S rK   )rL   )r(   s    r   rO   zAsyncCommandsParser.__init__   s    ?Ar   r   r   c                    K   |r|| _         | j                             d           d {V }d |                                D             | _        d S )NCOMMANDc                 >    i | ]\  }}|                                 |S r   )rX   )r   r[   r*   s      r   
<dictcomp>z2AsyncCommandsParser.initialize.<locals>.<dictcomp>   s&    SSS,#wgSSSr   )r   rw   itemsrL   )r(   r   rL   s      r   rM   zAsyncCommandsParser.initialize   s_       	DI229========SS(..BRBRSSSr   r)   .c                   K   t                    dk     rdS d                                         }|| j        vr|                                }|d         }|| j        v r|t	          dd                   z   nG|                                  d{V  || j        vr$t          |                                 d          | j                            |          }d|d         v r | j	          d{V }nd|d         v s|d	         dk    r | j
         }n|d
         dk    r||d         dk    rp|d         dk    rdd}d|v rZ| dd                                          }|d         D ]2}t          |d                   |k    r|                     |          }d}3|sdS |d         }	|	dk     rt                    t          |	          z
  }	t	          t          |d         |	dz   |d
                             }
fd|
D             }|S )r]   r   Nr   r   r^   r_   r3   r`   r/   r9   r5   r7   Fr?   ra   Tc                      g | ]
}|         S r   r   rc   s     r   r   z0AsyncCommandsParser.get_keys.<locals>.<listcomp>	  re   r   rf   )r(   r)   rC   rm   r*   r+   rn   ro   rp   r7   rq   s    `         r   rr   zAsyncCommandsParser.get_keys   s}      t99q==47==??4=(( &^^--N%a(H4=((%T!""X6 oo'''''''''4=00$#>>++UUU   -##H--GG,,,00$7777777DD)))WV_-H-H(4($/DD %**O,11N+q00!	 G++%-"A"AQ"A"AK")-"8 - -'q	22kAA&*&;&;F&C&CG(,I !  4">2La"4yy3|+<+<<go.q0@',BWXX H 3222222Dr   c                    K   	  | j         j        dg|R   d {V }n6# t          $ r)}|                                }d|v sd|v rY d }~d S |d }~ww xY w|S )Nrt   ru   rv   )r   rw   r
   rx   )r(   r)   r+   rz   r{   s        r   ri   z&AsyncCommandsParser._get_moveable_keys  s      
	223DLtLLLLLLLLLDD 	 	 	iikkG#w..5@@ttttt	 s    
AA
AA)r   NrK   )rE   rF   rG   r|   	__slots__rO   r   rM   r   r   strrr   ri   r   r   r   r~   r~      s          %IB B B BT TXm%< T T T T T?C ?HU38_,E ? ? ? ?Bc huS#X6O      r   r~   N)typingr   r   r   r   r   r   redis.exceptionsr	   r
   redis.utilsr   redis.asyncio.clusterr   r   rI   r~   r   r   r   <module>r      s   C C C C C C C C C C C C C C C C 6 6 6 6 6 6 6 6 $ $ $ $ $ $ 2111111+ + + + + + + +\r r r r r+ r r rjl l l l l0 l l l l lr   