U
    hL                      @   sJ  d dl Z d dlZ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mZmZ d dlmZ d dlmZmZmZ z0d dlZeejdd  dkZese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>ee+e"f e"e"e"ed#d$d%Z,d&gg ddfe+e+e"e"d'd(d)Z-d*d+ Z.d,d- Z/e"e0e"d.d/d0Z1e"e"ed1d2d3Z2d4d5 Z3d?ee ee ee ee e
e d6d7d8Z4d@d:d;Z5dS )A    N)contextmanagerwraps)AnyDictListMappingOptionalUnion)	DataError)
AbsExpiryT
EncodableTExpiryT.   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-4avdqdik/redis/utils.pyr   &   s    r   c                 c   s   |   }|V  |  d S N)pipelineexecute)Z	redis_objpr   r   r   r   2   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_bytes9   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_mergeC   s    r,   c                 C   s   t | |S r   )dictfromkeys)Zkey_listcallbackr   r   r   list_keys_to_dictQ   s    r0   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_resultU   s
    	r9       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    () -- Deprecated since version category
stacklevelwarningswarnDeprecationWarning)namereasonversionrA   rC   msgr   r   r   warn_deprecatedg   s    rJ   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   rA   )rJ   __name__)argsr   )funcrF   rG   rH   r   r   wrapperx   s    z7deprecated_function.<locals>.decorator.<locals>.wrapperr   rN   rO   rF   rG   rH   rN   r   	decoratorw   s    z&deprecated_function.<locals>.decoratorr   )rG   rH   rF   rS   r   rQ   r   deprecated_functionr   s    rT   )arg_namefunction_namerG   rH   rA   c                 C   sV   dd l }d| d|  d}|r.|d| d7 }|rB|d| d7 }|j|t|d	 d S )
Nr   z	Call to 'z6' function with deprecated usage of input argument/s 'z'.r<   r=   r>   r   r?   rB   )rU   rV   rG   rH   rA   rC   rI   r   r   r   warn_deprecated_arg_usage   s    rW   *)args_to_warnallowed_argsrG   rH   c                    s    fdd}|S )z
    Decorator to mark specified args of a function as deprecated.
    If '*' is in args_to_warn, all arguments will be marked as deprecated.
    c                    s    t   fdd}|S )Nc                     s   j jd j j }tt|| }|| |dd   D ]}||d  q<D ]R}|dkrt|dkrtt	|
 jdd qR||krRt|jdd qR| |S )NselfrX   r   r   rK   )__code__co_varnamesco_argcountr-   zipr)   poplenrW   r4   keysrL   )rM   r   	arg_namesZprovided_argsZallowed_argarg)rZ   rY   rN   rG   rH   r   r   rO      s0    

    z3deprecated_args.<locals>.decorator.<locals>.wrapperr   rP   rZ   rY   rG   rH   rR   r   rS      s    z"deprecated_args.<locals>.decoratorr   )rY   rZ   rG   rH   rS   r   re   r   deprecated_args   s    rf   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
loggerDictrb   	getLoggersetLevelINFOStreamHandler
addHandler)loggerhandlerr   r   r   _set_info_logger   s    
rr   c                  C   s.   zt d} W n t jk
r(   d} Y nX | S )NZredisz99.99.99)r   rH   ZPackageNotFoundError)Zlibverr   r   r   get_lib_version   s
    
rs   )
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. )rM   ra   )rt   ru   r   r   r   format_error_message   s    "rw   )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   rv   )r4   mapintsplitra   rangeappend	enumerate)rx   ry   Znum_versions1Znum_versions2Zdiff_iverr   r   r   compare_versions   s     	r   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
    


r   )expxexatpxatr   c                 C   s8  g }| d k	rx| d t| tjr6| t|   n@t| trL| |  n*t| trn|  rn| t|  ntdn|d k	r| d t|tjr| t| d  nt|tr| | ntdnf|d k	rt|tjrt|	 }|
d|g n6|d k	r4t|tjr&t|	 d }|
d|g |S )NZEXz$ex must be datetime.timedelta or intZPXi  z$px must be datetime.timedelta or intZEXATZPXAT)r   r!   datetime	timedeltar|   total_secondsr&   isdigitr   	timestampextend)r   r   r   r   Zexp_optionsr   r   r   extract_expire_flags  s4    






r   d   c                 C   s   t j| |dddS )Nz...T)textwidthplaceholderbreak_long_words)textwrapshorten)txt
max_lengthr   r   r   truncate_text0  s       r   )r:   r:   r;   )r:   r:   N)r:   r:   r;   )NNNN)r   )6r   rg   r   
contextlibr   	functoolsr   typingr   r   r   r   r	   r
   Zredis.exceptionsr   Zredis.typingr   r   r   Zhiredisr|   __version__r}   ZHIREDIS_AVAILABLEImportErrorsslZSSL_AVAILABLEZcryptographyZCRYPTOGRAPHY_AVAILABLE	importlibr   r   r   r&   r"   r%   r'   r,   r0   r9   rJ   rT   r4   rW   rf   rr   rs   BaseExceptionrw   r   r   r   r   r   r   r   r   <module>   s    



 

   
-
    %