o
    Š¨Êh³  ã                   @   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	 zd dl
Z
ee
j d¡d  ƒdkZes3edƒ‚W n ey?   dZY nw zd dlZd	ZW n eyS   dZY nw zd dlZd	ZW n eyg   dZY nw d d
lmZ dd„ Zedd„ ƒZde	eef defdd„Zdd„ Zdeeef deeef fdd„Zdd„ Zdd„ Zd0dd„Zd1d d!„Z d"d#„ Z!d$d%„ Z"d&ed'e#defd(d)„Z$d*ed+edefd,d-„Z%d.d/„ Z&dS )2é    N)Úcontextmanager©Úwraps)ÚAnyÚDictÚMappingÚUnionÚ.é   z"hiredis package should be >= 3.0.0FT)Úmetadatac                 K   s   ddl m} |j| fi |¤Ž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)Úredis.clientr   Úfrom_url)ÚurlÚkwargsr   © r   ú=/var/www/html/env/lib/python3.10/site-packages/redis/utils.pyr   !   s   r   c                 c   s   � |   ¡ }|V  | ¡  d S ©N)ÚpipelineÚexecute)Ú	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:   ó   r#   Údictsc                  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)Úkey_listÚcallbackr   r   r   Úlist_keys_to_dictL   r$   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   	ÿr7   Ú é   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Úversionr<   r=   Úmsgr   r   r   Úwarn_deprecatedb   s   rD   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� ˆ | i |¤ŽS )Nr
   )r<   )rD   Ú__name__)Úargsr   )Úfuncr@   rA   rB   r   r   Úwrappers   s   z7deprecated_function.<locals>.decorator.<locals>.wrapperr   )rG   rH   ©r@   rA   rB   )rG   r   Ú	decoratorr   s   z&deprecated_function.<locals>.decoratorr   )rA   rB   r@   rJ   r   rI   r   Údeprecated_functionm   s   rK   c                  C   sN   dt jjj ¡ vr%t  d¡} |  t j¡ t  ¡ }| t j¡ |  	|¡ dS dS )zo
    Set up a logger that log info logs to stdout.
    (This is used by the default push response handler)
    Úpush_responseN)
ÚloggingÚrootÚmanagerÚ
loggerDictÚkeysÚ	getLoggerÚsetLevelÚINFOÚStreamHandlerÚ
addHandler)ÚloggerÚhandlerr   r   r   Ú_set_info_logger}   s   
ûrY   c                  C   s,   zt  d¡} W | S  t jy   d} Y | S w )NÚredisz99.99.99)r   rB   ÚPackageNotFoundError)Úlibverr   r   r   Úget_lib_versionŠ   s   þþr]   Ú
host_errorÚ	exceptionc                 C   s\   |j s	d| › d�S t|j ƒdkrd|j d › d| › d�S d|j d › d| › d|j d › d�S )NzError connecting to r	   é   zError r   z connecting to z. )rF   Úlen)r^   r_   r   r   r   Úformat_error_message’   s   ÿÿrb   Úversion1Úversion2c                 C   sÜ   t tt|  d¡ƒƒ}t tt| d¡ƒƒ}t|ƒt|ƒkr1t|ƒt|ƒ }t|ƒD ]}| d¡ q(nt|ƒt|ƒk rMt|ƒt|ƒ }t|ƒD ]}| d¡ qEt|ƒD ]\}}|| || kr` dS || || k rk dS qQdS )z—
    Compare two versions.

    :return: -1 if version1 > version2
             0 if both versions are equal
             1 if version1 < version2
    r	   r   éÿÿÿÿr`   )r2   ÚmapÚintÚsplitra   ÚrangeÚappendÚ	enumerate)rc   rd   Únum_versions1Únum_versions2ÚdiffÚ_ÚiÚverr   r   r   Úcompare_versionsž   s$   	ÿÿrr   c                 C   s*   t | tƒr
|  d¡S t | tƒr| S tdƒ‚)Nr   z$Key must be either a string or bytes)r   r   r   r"   Ú	TypeError)Úkeyr   r   r   Úensure_string¼   s
   


ru   )r8   r8   r9   )r8   r8   N)'rM   Ú
contextlibr   Ú	functoolsr   Útypingr   r   r   r   Úhiredisrg   Ú__version__rh   ÚHIREDIS_AVAILABLEÚImportErrorÚsslÚSSL_AVAILABLEÚcryptographyÚCRYPTOGRAPHY_AVAILABLEÚ	importlibr   r   r   r"   r   r!   r#   r)   r.   r7   rD   rK   rY   r]   ÚBaseExceptionrb   rr   ru   r   r   r   r   Ú<module>   sR    ÿÿÿÿ
"

