o
    `f                     @   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   O/home/ubuntu/webapp/venv/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"    "
	
