o
    Phh                     @   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                   @   sl   e Zd ZU i Zeeef ed< dd Zdd Z	dedefdd	Z
d
d Zdd Zdd Zdd Zdd ZdS )ConnectionFactory_poolsc                 C   sR   | dd}t|| _| di | _| dd}t|| _| di | _|| _d S )NCONNECTION_POOL_CLASSzredis.connection.ConnectionPoolCONNECTION_POOL_KWARGSREDIS_CLIENT_CLASSzredis.client.RedisREDIS_CLIENT_KWARGS)getr   pool_clspool_cls_kwargsredis_client_clsredis_client_cls_kwargsoptions)selfr   pool_cls_pathredis_client_cls_path r   ]/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/django_redis/pool.py__init__   s   


zConnectionFactory.__init__c                 C   s   ||   d}| jdd}|r||d< | jdd}|r,t|ttfs(J d||d< | jdd}|rDt|ttfs@J d	||d
< |S )zm
        Given a main connection parameters, build a complete
        dict of connection parameters.
        )urlparser_classPASSWORDNpasswordSOCKET_TIMEOUTz)Socket timeout should be float or integersocket_timeout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_paramsr   returnc                 C   s   |  |}| |}|S )zW
        Given a basic connection parameters,
        return a new connection.
        )r+   get_connection)r   r   params
connectionr   r   r   connectA   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   r2   J   s   zConnectionFactory.disconnectc                 C   s    |  |}| jd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.
        r1   Nr   )get_or_create_connection_poolr   r   )r   r.   poolr   r   r   r-   R   s   
z ConnectionFactory.get_connectionc                 C   s"   | j dd }|d u rtS t|S )N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   )r   get_connection_pool)r   r.   keyr   r   r   r3   e   s   

z/ConnectionFactory.get_or_create_connection_poolc                 C   sV   t |}|| j | jjdi |}|jdddu r)|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!   Nr   )dictupdater   r   from_urlconnection_kwargsr   reset)r   r.   	cp_paramsr4   r   r   r   r7   r   s   z%ConnectionFactory.get_connection_poolN)__name__
__module____qualname__r   r   strr   __annotations__r   r+   r0   r2   r-   r&   r3   r7   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.SentinelConnectionPool	SENTINELSz5SENTINELS must be provided as a list of (host, port).r   sentinel_kwargsSENTINEL_KWARGS)
setdefaultsuperr   r   r   r+   popr:   r   r   	_sentinel)r   r   	sentinelsr<   	__class__r   r   r      s&   


z"SentinelConnectionFactory.__init__c                    sX   t |d }t|}|j|j| jd t |}t|j	d}|r*t
|d |_|S )zh
        Given a connection parameters, return a new sentinel connection pool
        for them.
        r   )service_namesentinel_manager	is_masterr   )r   r9   r:   hostnamerK   rI   r7   r   queryr   r
   rQ   )r   r.   r   r>   r4   rQ   rM   r   r   r7      s   z-SentinelConnectionFactory.get_connection_pool)r?   r@   rA   r   r7   __classcell__r   r   rM   r   rD      s    rD   Nc                 C   s:   | d u r
t tdd} |d}|r|} t| }||pi S )NDJANGO_REDIS_CONNECTION_FACTORYz#django_redis.pool.ConnectionFactoryCONNECTION_FACTORY)getattrr   r   r   )pathr   opt_conn_factoryr6   r   r   r   get_connection_factory   s   
rZ   )NN)typingr   urllib.parser   r   django.confr   django.core.exceptionsr   django.utils.module_loadingr   redisr   redis.connectionr	   r
   redis.sentinelr   r   rD   rZ   r   r   r   r   <module>   s    y/