U
    g                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 z0d dl
Z
ee
jdd  dkZesfedW n ek
r   dZY nX zd dlZd	ZW n ek
r   dZY nX zd dlZd	ZW n ek
r   dZY nX d d
lmZ dd Zedd Ze	eef edddZdd Zeeef eeef dddZdd Zdd Zd-ddZd.dd Z d!d" Z!d#d$ Z"ee#ed%d&d'Z$eeed(d)d*Z%d+d, Z&dS )/    N)contextmanagerwraps)AnyDictMappingUnion.   z"hiredis package should be >= 3.0.0FT)metadatac                 K   s   ddl m} |j| f|S )z
    Returns an active Redis client generated from the given database URL.

    Will attempt to extract the database id from the path url fragment, if
    none is provided.
    r   )Redis)Zredis.clientr   from_url)urlkwargsr    r   //tmp/pip-unpacked-wheel-f3sx1i9r/redis/utils.pyr   !   s    r   c                 c   s   |   }|V  |  d S N)pipelineexecute)Z	redis_objpr   r   r   r   -   s    r   )valuereturnc                 C   s   t | tr| jdddS | S )Nutf-8replace)errors)
isinstancebytesdecoder   r   r   r   str_if_bytes4   s    r   c                 C   s   t t| S r   )strr   r   r   r   r   safe_str:   s    r!   )dictsr   c                  G   s   i }| D ]}| | q|S )za
    Merge all provided dicts into 1 dict.
    *dicts : `dict`
        dictionaries to merge
    )update)r"   mergeddr   r   r   
dict_merge>   s    r&   c                 C   s   t | |S r   )dictfromkeys)Zkey_listcallbackr   r   r   list_keys_to_dictL   s    r*   c                 C   s0   t  }| D ]}|D ]}|| qqt|S )z
    Merge all items in `res` into a list.

    This command is used when sending a command to multiple nodes
    and the result from each node should be merged into a single list.

    res : 'dict'
    )setvaluesaddlist)commandresresultvr   r   r   r   merge_resultP   s
    	r3       c                 C   sP   dd l }d|  d}|r(|d| d7 }|r<|d| d7 }|j|t|d d S )Nr   zCall to deprecated r	   z ()z -- Deprecated since version )category
stacklevel)warningswarnDeprecationWarning)namereasonversionr8   r9   msgr   r   r   warn_deprecatedb   s    r@   c                    s    fdd}|S )z5
    Decorator to mark a function as deprecated.
    c                    s   t   fdd}|S )Nc                     s    t p
 jdd  | |S )Nr
   )r8   )r@   __name__)argsr   )funcr<   r=   r>   r   r   wrappers   s    z7deprecated_function.<locals>.decorator.<locals>.wrapperr   )rC   rD   r<   r=   r>   )rC   r   	decoratorr   s    z&deprecated_function.<locals>.decoratorr   )r=   r>   r<   rF   r   rE   r   deprecated_functionm   s    rG   c                  C   sJ   dt jjj krFt d} | t j t  }|t j | 	| dS )zo
    Set up a logger that log info logs to stdout.
    (This is used by the default push response handler)
    Zpush_responseN)
loggingrootmanager
loggerDictkeys	getLoggersetLevelINFOStreamHandler
addHandler)loggerhandlerr   r   r   _set_info_logger}   s    
rT   c                  C   s.   zt d} W n t jk
r(   d} Y nX | S )NZredisz99.99.99)r   r>   ZPackageNotFoundError)Zlibverr   r   r   get_lib_version   s
    
rU   )
host_error	exceptionr   c                 C   s`   |j sd|  dS t|j dkr8d|j d  d|  dS d|j d  d|  d|j d  dS d S )NzError connecting to r	      zError r   z connecting to z. )rB   len)rV   rW   r   r   r   format_error_message   s    "rZ   )version1version2r   c                 C   s   t tt| d}t tt|d}t|t|krbt|t| }t|D ]}|d qPn8t|t|k rt|t| }t|D ]}|d qt|D ]4\}}|| || kr dS || || k r dS qdS )z
    Compare two versions.

    :return: -1 if version1 > version2
             0 if both versions are equal
             1 if version1 < version2
    r	   r   rX   )r.   mapintsplitrY   rangeappend	enumerate)r[   r\   Znum_versions1Znum_versions2Zdiff_iverr   r   r   compare_versions   s     	rg   c                 C   s.   t | tr| dS t | tr"| S tdd S )Nr   z$Key must be either a string or bytes)r   r   r   r    	TypeError)keyr   r   r   ensure_string   s
    


rj   )r4   r4   r5   )r4   r4   N)'rH   
contextlibr   	functoolsr   typingr   r   r   r   Zhiredisr_   __version__r`   ZHIREDIS_AVAILABLEImportErrorsslZSSL_AVAILABLEcryptographyZCRYPTOGRAPHY_AVAILABLE	importlibr   r   r   r    r   r   r!   r&   r*   r3   r@   rG   rT   rU   BaseExceptionrZ   rg   rj   r   r   r   r   <module>   sH   



 

