
    0WRi                         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	      Z G d
 de      ZddZy)    )parse_qs	urlencodeurlparse
urlunparse)settings)ImproperlyConfigured)import_string)Redis)ConnectionPoolDefaultParserto_bool)Sentinelc                   j    e Zd ZU i Zeeef   ed<   d Zd Z	dede
fdZde
ddfd	Zd
 Zd Zd Zd Zy)ConnectionFactory_poolsc                     |j                  dd      }t        |      | _        |j                  di       | _        |j                  dd      }t        |      | _        |j                  di       | _        || _        y )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_paths       o/var/www/html/navyabakers_fullstack/navyabakers_prod/venv_new/lib/python3.12/site-packages/django_redis/pool.py__init__zConnectionFactory.__init__   st    #%F
 &m4&{{+CRH ',@BV W -.C D'.{{3H"'M$    c                    || j                         d}| j                  j                  dd      }|r||d<   | j                  j                  dd      }|r(t        |t        t
        f      sd}t        |      ||d<   | j                  j                  dd      }|r(t        |t        t
        f      sd	}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+   s          r    make_connection_paramsz(ConnectionFactory.make_connection_params!   s      //1

 <<##J5!)F:))*:DAnsEl; K*=99'5F#$!%!1!12JD!Q!4sElC S*=99/EF+,r"   r$   returnc                 F    | j                  |      }| j                  |      S )zW
        Given a basic connection parameters,
        return a new connection.
        )r2   get_connection)r   r$   paramss      r    connectzConnectionFactory.connect@   s%    
 ,,S1""6**r"   
connectionNc                 8    |j                   j                          y)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   r8   s     r    r;   zConnectionFactory.disconnectH   s     	""--/r"   c                 `    | j                  |      } | 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.
        r:    )get_or_create_connection_poolr   r   )r   r6   pools      r    r5   z ConnectionFactory.get_connectionP   s?     11&9$t$$ 
 
$($@$@
 	
r"   c                 `    | j                   j                  dd       }|t        S t        |      S )NPARSER_CLASS)r   r   r   r	   )r   clss     r    r,   z ConnectionFactory.get_parser_cls]   s.    ll~t4;  S!!r"   c                     |d   }|| j                   vr| j                  |      | 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   r6   keys      r    r>   z/ConnectionFactory.get_or_create_connection_poolc   sA     Umdkk!#77?DKK{{3r"   c                     t        |      }|j                  | j                          | j                  j                  di |}|j
                  j                  dd      /|j                  dd      |j
                  d<   |j                          |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   r6   	cp_paramsr?   s       r    rD   z%ConnectionFactory.get_connection_poolp   s|     L	--.%t}}%%2	2!!%%j$7?17J1MD"":.JJLr"   )__name__
__module____qualname__r   rG   strr   __annotations__r!   r2   r
   r7   r;   r5   r,   r>   rD   r=   r"   r    r   r      s\     )+FDn$%*>+3 +5 +0U 0t 0
" r"   r   c                   (     e Zd Z fdZ fdZ xZS )SentinelConnectionFactoryc                 F   |j                  dd       t        | 	  |       |j                  d      }|sd}t	        |      | j                  d       }|j                  d       |j                  | j                         t        |fd|j                  d      i|| _
        y )Nr   z%redis.sentinel.SentinelConnectionPool	SENTINELSz5SENTINELS must be provided as a list of (host, port).r$   sentinel_kwargsSENTINEL_KWARGS)
setdefaultsuperr!   r   r   r2   poprH   r   r   	_sentinel)r   r   	sentinelsr1   rJ   	__class__s        r    r!   z"SentinelConnectionFactory.__init__   s    #%L	
 	!KK,	SM&}55 !77=e$  !5!56!
#KK(9:
  
r"   c                    t        |d         }t        |      }t        |j                        }|j	                  d      }|rt        |d         |d<   d|v r|d= t        |d      }t        |j                  |j                  |j                  |j                  ||j                  f      }|j                  |j                  | j                  |       t         | E  |      S )zh
        Given a connection parameters, return a new sentinel connection pool
        for them.
        r$   	is_masterr   T)doseq)service_namesentinel_managerr$   )r   rG   r   queryr   r   r   r   schemenetlocpathr6   fragmentrH   hostnamer[   rY   rD   )	r   r6   r$   rL   query_paramsr_   	new_querynew_urlr]   s	           r    rD   z-SentinelConnectionFactory.get_connection_pool   s    
 ve}% L	  		* $$[1	%,Yq\%:Ik" ,&[)l$7	ZZSXXszz9cllS
 	G 	 	
 w*955r"   )rM   rN   rO   r!   rD   __classcell__)r]   s   @r    rS   rS      s    
.6 6r"   rS   Nc                     | t        t        dd      } |j                  d      }|r|} t        |       } ||xs i       S )NDJANGO_REDIS_CONNECTION_FACTORYz#django_redis.pool.ConnectionFactoryCONNECTION_FACTORY)getattrr   r   r	   )rf   r   opt_conn_factoryrB   s       r    get_connection_factoryrr      sM    |-1

 {{#78

Cw}"r"   )NN)urllib.parser   r   r   r   django.confr   django.core.exceptionsr   django.utils.module_loadingr	   redisr
   redis.connectionr   r   r   redis.sentinelr   r   rS   rr   r=   r"   r    <module>rz      s@    B B   7 5  C C #u up66 1 66rr"   