U
    d`                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZmZ eeedf ee d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S )    N)ListTuple)KeysTKeyT.)keysargsreturnc                 C   sX   z*t |  t| ttfr | g} nt| } W n tk
rD   | g} Y nX |rT| | | S N)iter
isinstancebytesstrlist	TypeErrorextend)r   r    r   :/tmp/pip-unpacked-wheel-xj5sloo9/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k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kr|S | D ]~}z| t| W q tk
rt   z| t| W n" tk
rn   | t| Y nX Y q tk
r   | d Y qX q|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 ]}t| | tr>|d t| |  qt| |d  trht| |d  g|d< qzt| |d  || | < W q ttfk
r   | |d  || | < Y qX q|S )Nr      zChild iterators   )	rangelenr   r   r   parse_list_to_dictr"   r   r!   )r#   r$   ir   r   r   r+   =   s    r+   c              
   C   s   | d kri S i }| D ]}t |d tr<t|d ||d < qzFzt|d ||d < W n( ttfk
r~   |d ||d < Y nX W q tk
r   Y qX q|S )Nr(   r   )r   r   r+   r"   r   r!   
IndexError)r#   r$   Zdetr   r   r   parse_to_dictL   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>c   s    z random_string.<locals>.<genexpr>)joinr)   )lengthr   r   r   random_string_   s    
r8   c                 C   sV   t | tr|  } nt | ts"| S t| dkr2d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_stringh   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   Znewobjkr   r   r   decode_dict_keys|   s    

r@   c                 C   sz   t | trt| S | dkrdS t | ttfrDddtt|  dS t | trndddd | 	 D  d	S t| S d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   r?   r;   r   r   r   r5      s     z(stringify_param_value.<locals>.<genexpr>})
r   r   r<   r   tupler6   maprF   dictitems)valuer   r   r   rF      s    

 rF   )r/   )r=   r1   r3   typingr   r   Zredis.typingr   r   r   r   r   r&   r+   r.   r8   r<   r@   rF   r   r   r   r   <module>   s   
	
