o
    QDi                     @   s   d dl mZmZ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mZ d dlmZ G dd	 d	ZG d
d deZdddZdS )    )parse_qs	urlencodeurlparse
urlunparse)settings)ImproperlyConfigured)import_string)Redis)ConnectionPoolDefaultParserto_bool)Sentinelc                   @   sv   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
e
ddf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   J/var/www/Datamplify/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*d}t|||d< | jdd}|rHt|ttfsDd	}t|||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floatr   )r   r    kwargsr#   r%   error_messager'   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    paramsr   r   r   connect@   s   

zConnectionFactory.connect
connectionNc                 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   r3   r   r   r   r5   H   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.
        r4   Nr   )get_or_create_connection_poolr   r   )r   r1   poolr   r   r   r0   P   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   r1   keyr   r   r   r6   c   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   r1   	cp_paramsr7   r   r   r   r:   p   s   z%ConnectionFactory.get_connection_pool)__name__
__module____qualname__r   r<   strr
   __annotations__r   r.   r	   r2   r5   r0   r(   r6   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                    sr   | dd t | |d}|sd}t|| 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-   r?   	__class__r   r   r      s$   


z"SentinelConnectionFactory.__init__c                    s   t |d }t|}t|j}|d}|rt|d |d< d|v r%|d= t|dd}t|j|j	|j
|j||jf}|j|j| j|d t |S )zh
        Given a connection parameters, return a new sentinel connection pool
        for them.
        r    	is_masterr   T)doseq)service_namesentinel_managerr    )r   r<   r   queryr   r   r   r   schemenetlocpathr1   fragmentr=   hostnamerN   rL   r:   )r   r1   r    rA   query_paramsrR   	new_querynew_urlrP   r   r   r:      s    


z-SentinelConnectionFactory.get_connection_pool)rB   rC   rD   r   r:   __classcell__r   r   rP   r   rG      s    rG   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   )rY   r   opt_conn_factoryr9   r   r   r   get_connection_factory   s   
rd   )NN)urllib.parser   r   r   r   django.confr   django.core.exceptionsr   django.utils.module_loadingr   redisr	   redis.connectionr
   r   r   redis.sentinelr   r   rG   rd   r   r   r   r   <module>   s    x9