
    keJiMo                     .   d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZmZmZ d dlmZ erd dlmZ  G d de          Z G d d	e          Z G d
 d          Zeeeeef         f         Z G d d          Z G d de          Z G d de          ZdS )    )Enum)TYPE_CHECKINGAny	AwaitableDictOptionalTupleUnion)IncorrectPolicyType
RedisErrorResponseErrorstr_if_bytes)ClusterNodec                   .    e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
RequestPolicy	all_nodes
all_shardsall_replicasmulti_shardspecialdefault_keylessdefault_keyeddefault_nodeN)__name__
__module____qualname__	ALL_NODES
ALL_SHARDSALL_REPLICASMULTI_SHARDSPECIALDEFAULT_KEYLESSDEFAULT_KEYEDDEFAULT_NODE     ]/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/redis/_parsers/commands.pyr   r      s6        IJ!LKG'O#M!LLLr'   r   c                   6    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )ResponsePolicyone_succeededall_succeededagg_logical_andagg_logical_oragg_minagg_maxagg_sumr   r   r   N)r   r   r   ONE_SUCCEEDEDALL_SUCCEEDEDAGG_LOGICAL_ANDAGG_LOGICAL_ORAGG_MINAGG_MAXAGG_SUMr"   r#   r$   r&   r'   r(   r*   r*      s@        #M#M'O%NGGGG'O#MMMr'   r*   c                   8    e Zd Zej        ej        fdedefdZdS )CommandPoliciesrequest_policyresponse_policyc                 "    || _         || _        d S Nr;   r<   )selfr;   r<   s      r(   __init__zCommandPolicies.__init__$   s    
 -.r'   N)r   r   r   r   r#   r*   rA   r&   r'   r(   r:   r:   #   sM         )6(E*8*H/ /%/ (/ / / / / /r'   r:   c                       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&   r   ).0args     r(   
<listcomp>z;AbstractCommandsParser._get_pubsub_keys.<locals>.<listcomp>;   s     222cS!!222r'   r   PUBSUB   )CHANNELSNUMSUBSHARDCHANNELSSHARDNUMSUB)	SUBSCRIBE
PSUBSCRIBEUNSUBSCRIBEPUNSUBSCRIBE)PUBLISHSPUBLISH)lenupperlist)r@   argscommandkeyspubsub_types        r(   _get_pubsub_keysz'AbstractCommandsParser._get_pubsub_keys1   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   namerK   arityc                 ,    g | ]}t          |          S r&   r   )rG   flags     r(   rI   z;AbstractCommandsParser.parse_subcommand.<locals>.<listcomp>S   s     GGGD\$//GGGr'   rE   flags   first_key_pos   last_key_pos   
step_count   tips   key_specifications	   subcommands)r   intrV   )r@   rZ   optionscmd_dictcmd_names        r(   parse_subcommandz'AbstractCommandsParser.parse_subcommandN   s    
++#
OOGGGAJGGG$+AJ!#*1: !(w<<!&qzHV-4QZH)*&-ajH]#r'   N)r   r   r   r]   rt   r&   r'   r(   rC   rC   0   s2          :    r'   rC   c                   Z    e Zd ZdZd Zd Zd Zd Z	 ddede	e         d	e
fd
Zd	ef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                 V    i | _         || _        |                     | j                   d S r>   )commandsredis_connection
initialize)r@   ry   s     r(   rA   zCommandsParser.__init__g   s,     0-.....r'   c                    |                                 }g }|D ]0}t          d |D                       r|                    |           1|D ],}|                    |          ||                                <   -|| _        d S )Nc              3   >   K   | ]}|                                 V  d S r>   )isupper)rG   xs     r(   	<genexpr>z,CommandsParser.initialize.<locals>.<genexpr>p   s*      ,,1199;;,,,,,,r'   )rZ   anyappendpoplowerrx   )r@   rrx   uppercase_commandscmds        r(   rz   zCommandsParser.initializel   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    r||d         dk    rpd}d|v rf| dd                                          }|d         D ]>}	t          |	d                   |k    r#|                     |	          }|d         dk    rd}?|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.
        rE   Nr   rK   ( command doesn't exist in Redis commandsmovablekeysrc   pubsubr_   ri   re   rg   Fro   |Tc                      g | ]
}|         S r&   r&   rG   posrY   s     r(   rI   z+CommandsParser.get_keys.<locals>.<listcomp>       222#DI222r'   rV   r   rx   splitrX   rz   r   rW   get_get_moveable_keysr]   r   rt   absrange)r@   
redis_connrY   rs   cmd_name_splitrZ   r[   	is_subcmdsubcmd_namesubcmdrg   keys_poss     `         r(   get_keyszCommandsParser.get_keysy   sr    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 1 1'q	22kAA&*&;&;F&C&CG&7!;;,0	 !  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   rK   NCOMMAND GETKEYSInvalid arguments The command has no key arguments)r   rX   execute_commandr   __str__)r@   r   rY   piecesr[   emessages          r(   r   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command_namesubcommand_namereturnc                 f   |rt| j                             |          d         D ]>}t          |d                   |k    r#|                     |          }|d         dk    c S ?t	          d| d|           | j                             |d          }||d         dk    S t	          d| d          	a  
        Determines whether a given command or subcommand is considered "keyless".

        A keyless command does not operate on specific keys, which is determined based
        on the first key position in the command or subcommand details. If the command
        or subcommand's first key position is zero or negative, it is treated as keyless.

        Parameters:
            command_name: str
                The name of the command to check.
            subcommand_name: Optional[str], default=None
                The name of the subcommand to check, if applicable. If not provided,
                the check is performed only on the command.

        Returns:
            bool
                True if the specified command or subcommand is considered keyless,
                False otherwise.

        Raises:
            ValueError
                If the specified subcommand is not found within the command or the
                specified command does not exist in the available commands.
        ro   r   re   zSubcommand z not found in command NzCommand z not found in commandsrx   r   r   rt   
ValueErrorr@   r   r   
subcommandparsed_subcmdcommand_detailss         r(   _is_keyless_commandz"CommandsParser._is_keyless_command   s    6  	N"m//==mL ? ?

1../AA$($9$9*$E$EM(9Q>>>> B SoSS\SS   #m//dCCO*&71<<LLLLMMMr'   c                    i fd| j                                         D ]\  }}|                     |          }|rt          j        }t
          j        }nt          j        }t
          j        }|                    d          }t          |          dk    r|d         }|d         }n
d}|d         }	                    |d          |t          ||          i|<   nt          ||          |         |<   |	                    d          }	|	                    d	          }
|	r |	||           |
r|
D ]}|d         }t          |t                    r|                                }|                     ||          }|rt          j        }t
          j        }nt          j        }t
          j        }|                    d
d          }t          ||          |         |<   |dd         D ]} |||           ǐމS )  
        Retrieve and process the command policies for all commands and subcommands.

        This method traverses through commands and subcommands, extracting policy details
        from associated data structures and constructing a dictionary of commands with their
        associated policies. It supports nested data structures and handles both main commands
        and their subcommands.

        Returns:
            PolicyRecords: A collection of commands and subcommands associated with their
            respective policies.

        Raises:
            IncorrectPolicyType: If an invalid policy type is encountered during policy extraction.
        c                 r   t          | t          t          f          r4t          |                                           }|                    d          s|                    d          r|                    d          r]|                    d          d         }	 t          |          |         |         _        n # t          $ r t          d|           w xY w|                    d          r`|                    d          d         }	 t          |          |         |         _        dS # t          $ r t          d|           w xY wdS dS t          | t                    r| D ]} |||           dS t          | t                    r$|                                 D ]} |||           dS dS a  
            Recursively extract policies from nested data structures.

            Args:
                data: The data structure to search (can be list, dict, str, bytes, etc.)
                command_name: The command name to associate with found policies
            r;   r<   :rK   zIncorrect request policy type: z Incorrect response policy type: N
isinstancestrbytesr   decode
startswithr   r   r;   r   r   r*   r<   rX   dictvalues	datamodule_namer   policypolicy_typeitemvaluecommand_with_policiesextract_policiess	          r(   r   z=CommandsParser.get_command_policies.<locals>.extract_policies  B    $e-- (G%dkkmm44 $$%566 &:K:K%; ;  (()9:: 
&,ll3&7&7&: 0=[/I/I 2+> ,,n)   "5 O+ O O# # 
 (():;; 
&,ll3&7&7&: 1?{0K0K 2+> ,-oo)   "5 P; P P# # - 
 
 D$'' G  F FD$$T;EEEEF F D$'' G![[]] G GE$$UKFFFFG GG G    B< <C D/ /E.rK   r   coreNr?   rk   ro   r    rx   itemsr   r   r#   r*   r$   r   rV   r   r:   r   r   r   replacer@   rZ   details
is_keylessdefault_request_policydefault_response_policy
split_namer   r   rk   ro   subcommand_detailsr   subcommand_detailr   r   s                 @@r(   get_command_policiesz#CommandsParser.get_command_policies   s     !#0	G 0	G 0	G 0	G 0	G 0	Gd !% 3 3 5 5 F	V F	VGW11'::J G)6)F&*8*H'')6)D&*8*F' !s++J:""(m)!}$)!} %((d;;C /'=(?# # #6%k22 DS#9$;D D D%k2<@
 ;;v&&D!++m44K  B  {LAAA  V*5 V V&"4Q"7K!+u55 ;&1&8&8&:&: "&!9!9';!O!OJ! O1>1N.2@2P//1>1L.2@2N/"-"5"5c3"?"?K GV'=(?G G G)+6{C .@-C V V)(():KUUUUV %$r'   r>   )r   r   r   __doc__rA   rz   r   r   r   r   boolr   PolicyRecordsr   r&   r'   r(   rv   rv   ^   s         / / /
! ! !A A AF  4 CG(N (N(N2:3-(N	(N (N (N (NTL%m L% L% L% L% L% L%r'   rv   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de
dee
         defdZdee         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.
    )rx   noder   Nc                     i | _         d S r>   )rx   )r@   s    r(   rA   zAsyncCommandsParser.__init__  s    ?Ar'   r   r   c                    K   |r|| _         | j                             d           d {V }d |                                D             | _        d S )NCOMMANDc                 >    i | ]\  }}|                                 |S r&   )r   )rG   r   rZ   s      r(   
<dictcomp>z2AsyncCommandsParser.initialize.<locals>.<dictcomp>  s&    SSS,#wgSSSr'   )r   r   r   rx   )r@   r   rx   s      r(   rz   zAsyncCommandsParser.initialize  s_       	DI229========SS(..BRBRSSSr'   rY   .c                 0  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    r||d         dk    rpd}d|v rf| dd                                          }|d         D ]>}t          |d                   |k    r#|                     |          }|d         dk    rd}?|sdS |d         }	|	dk     rt                    t          |	          z
  }	t	          t          |d         |	dz   |d
                             }
fd|
D             }|S )r   rE   Nr   rK   r   r   rc   r   r_   ri   re   rg   Fro   r   Tc                      g | ]
}|         S r&   r&   r   s     r(   rI   z0AsyncCommandsParser.get_keys.<locals>.<listcomp>  r   r'   r   )r@   rY   rs   r   rZ   r[   r   r   r   rg   r   s    `         r(   r   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 1 1'q	22kAA&*&;&;F&C&CG&7!;;,0	 !  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 )Nr   r   r   )r   r   r   r   )r@   rY   r[   r   r   s        r(   r   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   r   c                 j  K   |rt| j                             |          d         D ]>}t          |d                   |k    r#|                     |          }|d         dk    c S ?t	          d| d|           | j                             |d          }||d         dk    S t	          d| d          r   r   r   s         r(   r   z'AsyncCommandsParser._is_keyless_command  s      6  	N"m//==mL ? ?

1../AA$($9$9*$E$EM(9Q>>>> B SoSS\SS   #m//dCCO*&71<<LLLLMMMr'   c                 $  K   i fd| j                                         D ]\  }}|                     |           d{V }|rt          j        }t
          j        }nt          j        }t
          j        }|                    d          }t          |          dk    r|d         }|d         }n
d}|d         }	                    |d          |t          ||          i|<   nt          ||          |         |<   |	                    d          }	|	                    d	          }
|	r |	||           |
r|
D ]}|d         }t          |t                    r|                                }|                     ||           d{V }|rt          j        }t
          j        }nt          j        }t
          j        }|                    d
d          }t          ||          |         |<   |dd         D ]} |||           ͐S )r   c                 r   t          | t          t          f          r4t          |                                           }|                    d          s|                    d          r|                    d          r]|                    d          d         }	 t          |          |         |         _        n # t          $ r t          d|           w xY w|                    d          r`|                    d          d         }	 t          |          |         |         _        dS # t          $ r t          d|           w xY wdS dS t          | t                    r| D ]} |||           dS t          | t                    r$|                                 D ]} |||           dS dS r   r   r   s	          r(   r   zBAsyncCommandsParser.get_command_policies.<locals>.extract_policies:  r   r   Nr   rK   r   r   r?   rk   ro   r   r   r   r   s                 @@r(   r   z(AsyncCommandsParser.get_command_policies(  s       !#0	G 0	G 0	G 0	G 0	G 0	Gd !% 3 3 5 5 F	V F	VGW#77@@@@@@@@J G)6)F&*8*H'')6)D&*8*F' !s++J:""(m)!}$)!} %((d;;C /'=(?# # #6%k22 DS#9$;D D D%k2<@
 ;;v&&D!++m44K  B  {LAAA  V*5 V V&"4Q"7K!+u55 ;&1&8&8&:&: (,'?'?'U'U!U!U!U!U!U!UJ! O1>1N.2@2P//1>1L.2@2N/"-"5"5c3"?"?K GV'=(?G G G)+6{C .@-C V V)(():KUUUUV %$r'   )r   Nr>   )r   r   r   r   	__slots__rA   r   rz   r   r	   r   r   r   r   r   r   r   r   r&   r'   r(   r   r     sA         %IB B B BT TXm%< T T T T TAC AHU38_,E A A A AFc huS#X6O     CG(N (N(N2:3-(N	(N (N (N (NTL%Im,D L% L% L% L% L% L%r'   r   N)enumr   typingr   r   r   r   r   r	   r
   redis.exceptionsr   r   r   redis.utilsr   redis.asyncio.clusterr   r   r*   r:   r   r   r   rC   rv   r   r&   r'   r(   <module>r      s         N N N N N N N N N N N N N N N N N N K K K K K K K K K K $ $ $ $ $ $ 2111111" " " " "D " " "
$ 
$ 
$ 
$ 
$T 
$ 
$ 
$/ / / / / / / / S$sO3445+ + + + + + + +\m% m% m% m% m%+ m% m% m%`	f% f% f% f% f%0 f% f% f% f% f%r'   