a
    Šxdÿ  ã                   @   s”   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ G d	d
„ d
ƒZG dd„ deƒZddd„ZdS )é    )ÚDict)Úparse_qsÚurlparse)Úsettings)ÚImproperlyConfigured)Úimport_string)ÚRedis)ÚDefaultParserÚto_bool)ÚSentinelc                   @   sX   e Zd Zi Zdd„ Zdd„ Zeedœdd„Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ ZdS )ÚConnectionFactoryc                 C   sR   |  dd¡}t|ƒ| _|  di ¡| _|  dd¡}t|ƒ| _|  di ¡| _|| _d S )NÚCONNECTION_POOL_CLASSzredis.connection.ConnectionPoolZCONNECTION_POOL_KWARGSZREDIS_CLIENT_CLASSzredis.client.RedisZREDIS_CLIENT_KWARGS)Úgetr   Úpool_clsÚpool_cls_kwargsÚredis_client_clsÚredis_client_cls_kwargsÚoptions)Úselfr   Zpool_cls_pathZredis_client_cls_path© r   úI/var/www/html/Ranjet/env/lib/python3.9/site-packages/django_redis/pool.pyÚ__init__   s    ÿ

zConnectionFactory.__init__c                 C   sŒ   ||   ¡ dœ}| j dd¡}|r(||d< | j dd¡}|rXt|ttfƒsPJ dƒ‚||d< | j dd¡}|rˆt|ttfƒs€J d	ƒ‚||d
< |S )zm
        Given a main connection parameters, build a complete
        dict of connection parameters.
        )ÚurlÚparser_classZPASSWORDNÚpasswordZSOCKET_TIMEOUTz)Socket timeout should be float or integerÚsocket_timeoutZSOCKET_CONNECT_TIMEOUTz1Socket connect timeout should be float or integerÚsocket_connect_timeout)Úget_parser_clsr   r   Ú
isinstanceÚintÚfloat)r   r   Úkwargsr   r   r   r   r   r   Úmake_connection_params#   s.    þÿþÿþz(ConnectionFactory.make_connection_params)r   Úreturnc                 C   s   |   |¡}|  |¡}|S )zW
        Given a basic connection parameters,
        return a new connection.
        )r"   Úget_connection)r   r   ÚparamsÚ
connectionr   r   r   ÚconnectB   s    

zConnectionFactory.connectc                 C   s   |j  ¡  dS )zž
        Given a not null client connection it disconnect from the Redis server.

        The default implementation uses a pool to hold connections.
        N)Úconnection_poolÚ
disconnect)r   r&   r   r   r   r)   K   s    zConnectionFactory.disconnectc                 C   s    |   |¡}| jf d|i| j¤ŽS )z®
        Given a now preformatted params, return a
        new connection.

        The default implementation uses a cached pools
        for create new connection.
        r(   )Úget_or_create_connection_poolr   r   )r   r%   Úpoolr   r   r   r$   S   s    
ÿÿz ConnectionFactory.get_connectionc                 C   s"   | j  dd ¡}|d u rtS t|ƒS )NZPARSER_CLASS)r   r   r	   r   )r   Úclsr   r   r   r   `   s    z ConnectionFactory.get_parser_clsc                 C   s,   |d }|| j vr"|  |¡| j |< | j | S )zÕ
        Given a connection parameters and return a new
        or cached connection pool for them.

        Reimplement this method if you want distinct
        connection pool instance caching behavior.
        r   )Ú_poolsÚget_connection_pool)r   r%   Úkeyr   r   r   r*   f   s    
z/ConnectionFactory.get_or_create_connection_poolc                 C   sV   t |ƒ}| | j¡ | jjf i |¤Ž}|j dd¡du rR| dd¡|jd< | ¡  |S )zÁ
        Given a connection parameters, return a new
        connection pool for them.

        Overwrite this method if you want a custom
        behavior on creating connection pool.
        r   N)ÚdictÚupdater   r   Zfrom_urlÚconnection_kwargsr   Úreset)r   r%   Ú	cp_paramsr+   r   r   r   r.   s   s    z%ConnectionFactory.get_connection_poolN)Ú__name__Ú
__module__Ú__qualname__r-   r   r"   Ústrr   r'   r)   r$   r   r*   r.   r   r   r   r   r      s   	r   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚSentinelConnectionFactoryc                    sn   |  dd¡ tƒ  |¡ | d¡}|s.tdƒ‚|  d ¡}| d¡ | | j¡ t	|fd| d¡i|¤Ž| _
d S )Nr   z%redis.sentinel.SentinelConnectionPoolZ	SENTINELSz5SENTINELS must be provided as a list of (host, port).r   Zsentinel_kwargsZSENTINEL_KWARGS)Ú
setdefaultÚsuperr   r   r   r"   Úpopr1   r   r   Ú	_sentinel)r   r   Z	sentinelsr2   ©Ú	__class__r   r   r   ‡   s&    ÿ
ÿ

ÿþýz"SentinelConnectionFactory.__init__c                    sX   t |d ƒ}t|ƒ}|j|j| jd tƒ  |¡}t|jƒ 	d¡}|rTt
|d ƒ|_|S )zh
        Given a connection parameters, return a new sentinel connection pool
        for them.
        r   )Zservice_nameZsentinel_managerÚ	is_masterr   )r   r0   r1   Úhostnamer=   r;   r.   r   Úqueryr   r
   r@   )r   r%   r   r4   r+   r@   r>   r   r   r.   Ÿ   s    z-SentinelConnectionFactory.get_connection_pool)r5   r6   r7   r   r.   Ú__classcell__r   r   r>   r   r9   †   s   r9   Nc                 C   s(   | d u rt tddƒ} t| ƒ}||p$i ƒS )NZDJANGO_REDIS_CONNECTION_FACTORYz#django_redis.pool.ConnectionFactory)Úgetattrr   r   )Úpathr   r,   r   r   r   Úget_connection_factoryµ   s    ýrF   )NN)Útypingr   Úurllib.parser   r   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.utils.module_loadingr   Zredisr   Zredis.connectionr	   r
   Zredis.sentinelr   r   r9   rF   r   r   r   r   Ú<module>   s   z/