o
    Š¨Êh  ã                   @   s¸   d dl Z d dlZd dlZd dlmZmZ d dlZd dlmZm	Z	 dedee	df dee	 fdd	„Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) é    N)ÚListÚTuple)ÚKeysTÚKeyTÚkeysÚargs.Úreturnc                 C   sV   zt | ƒ t| ttfƒr| g} nt| ƒ} W n ty!   | g} Y nw |r)|  |¡ | S ©N)ÚiterÚ
isinstanceÚbytesÚstrÚlistÚ	TypeErrorÚextend)r   r   © r   úH/var/www/html/env/lib/python3.10/site-packages/redis/commands/helpers.pyÚlist_or_args
   s   €
ÿ
r   c                 C   s*   t | tƒr|  dd¡n| }|dkrdS |S )zAReturn the decoded binary string, or a string, depending on type.úutf-8ÚreplaceÚnullN)r   r   Údecode)ÚxÚrr   r   r   Ú	nativestr   s   r   c                 C   s   | du r| S dd„ | D ƒS )z9Given a list of binaries, return the stringified version.Nc                 S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Úobjr   r   r   Ú
<listcomp>'   s    zdelist.<locals>.<listcomp>r   )r   r   r   r   Údelist#   s   r   c                 C   s�   g }| du r|S | D ];}z	|  t|ƒ¡ W q
 ty8   z	|  t|ƒ¡ W n ty5   |  t|ƒ¡ Y nw Y q
 tyE   |  d¡ Y q
w |S )z,Optimistically parse the response to a list.N)ÚappendÚintÚ
ValueErrorÚfloatr   r   )ÚresponseÚresÚitemr   r   r   Úparse_to_list*   s"   ÿ€ÿr&   c              
   C   sö   i }t dt| ƒdƒD ]n}t| | tƒr@|d  t| | ƒ¡ zt| |d  tƒr5|d  t| |d  ƒ¡ W q
 ty?   Y q
w t| |d  tƒrUt| |d  ƒg|d< q
zt| |d  ƒ|| | < W q
 tt	fyx   | |d  || | < Y q
w |S )Nr   é   zChild iteratorsé   )
ÚrangeÚlenr   r   r   Úparse_list_to_dictÚ
IndexErrorr"   r   r!   )r#   r$   Úir   r   r   r+   >   s&   €ÿÿr+   c              
   C   sÂ   | d u ri S i }| D ]T}t |tƒr|sq
t|ƒdkr!d||d < q
t |d tƒr3t|d ƒ||d < q
z"zt|d ƒ||d < W n ttfyS   |d ||d < Y nw W q
 ty^   Y q
w |S )Nr(   Tr   )r   r   r*   r+   r"   r   r!   r,   )r#   r$   Údetr   r   r   Úparse_to_dictR   s*   ÿ€ÿr/   é
   c                 C   s   d  dd„ t| ƒD ƒ¡S )z3
    Returns a random N character long string.
    Ú c                 s   s   � | ]	}t  tj¡V  qd S r	   )ÚrandomÚchoiceÚstringÚascii_lowercase)r   r   r   r   r   Ú	<genexpr>m   s   € 
ÿz random_string.<locals>.<genexpr>)Újoinr)   )Úlengthr   r   r   Úrandom_stringi   s   
ÿr9   c                 C   sV   t | tƒr
|  ¡ } nt | tƒs| S t| ƒdkrdS |  dd¡} |  dd¡} d| › d�S )zq
    RedisGraph strings must be quoted,
    quote_string wraps given v with quotes incase
    v is a string.
    r   z""ú\z\\ú"z\")r   r   r   r   r*   r   )Úvr   r   r   Úquote_stringr   s   


r=   c                 C   sB   t   | ¡}|  ¡ D ]}t|tƒr|| || d¡< | |¡ q	|S )z3Decode the keys of the given dictionary with utf-8.r   )Úcopyr   r   r   r   Úpop)r   ÚnewobjÚkr   r   r   Údecode_dict_keys†   s   


€rB   c                 C   sv   t | tƒr	t| ƒS | du rdS t | ttfƒr"dd tt| ƒ¡› d�S t | tƒr7dd dd„ |  	¡ D ƒ¡› d	�S t| ƒS )
a¥  
    Turn a parameter value into a string suitable for the params header of
    a Cypher command.
    You may pass any value that would be accepted by `json.dumps()`.

    Ways in which output differs from that of `str()`:
        * Strings are quoted.
        * None --> "null".
        * In dictionaries, keys are _not_ quoted.

    :param value: The parameter value to be turned into a string.
    :return: string
    Nr   ú[ú,ú]Ú{c                 s   s&   � | ]\}}|› d t |ƒ› �V  qdS )ú:N)Ústringify_param_value)r   rA   r<   r   r   r   r6   ¦   s   €$ z(stringify_param_value.<locals>.<genexpr>Ú})
r   r   r=   r   Útupler7   ÚmaprH   ÚdictÚitems)Úvaluer   r   r   rH   �   s   

 rH   c                 C   sH   t | tjƒst | tjjƒr| jj d¡S t | tjjƒr"| j	j d¡S d S )NÚprotocol)
r   ÚredisÚRedisÚasyncioÚconnection_poolÚconnection_kwargsÚgetÚclusterÚAbstractRedisClusterÚnodes_manager)Úclientr   r   r   Úget_protocol_version«   s
   ÿrZ   )r0   )r>   r2   r4   Útypingr   r   rP   Úredis.typingr   r   r   r   r   r&   r+   r/   r9   r=   rB   rH   rZ   r   r   r   r   Ú<module>   s"    "
	
