o
    ;h                     @  s   d dl mZ d dl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 d dlmZ er1	 ed	d
dG dd deZdS )    )annotationsN)TYPE_CHECKING)versionchanged)Version)ConfigurationError)RedisStorage)RedisClientz4.3zuAdded support for using the redis client from :pypi:`valkey` if :paramref:`uri` has the ``valkey+sentinel://`` schema)versionreasonc                      sr   e Zd ZdZddgZ	 ededededdZdddejd	fd! fddZ	e
d"ddZd#d$dd Z  ZS )%RedisSentinelStoragez
    Rate limit storage with redis sentinel as backend

    Depends on :pypi:`redis` package (or :pypi:`valkey` if :paramref:`uri` starts with
    ``valkey+sentinel://``)
    zredis+sentinelzvalkey+sentinelz3.0z6.0)rediszredis.sentinelvalkeyzvalkey.sentinelNTFuristrservice_name
str | Noneuse_replicasboolsentinel_kwargs$dict[str, float | str | bool] | None
key_prefixwrap_exceptionsoptionsfloat | str | boolreturnNonec                   s`  t t| j|fd|i| tj|}g }	|r| ni }
i }|jr(|j|d< |jr0|j|d< |j	
dd }|j	|d dD ]}|d\}}|	|t|f qB|| _|jrb|jd	d
n|| _| jdu rntd|drudnd| _| j| j d j}|j|	fdi ||
ii ||| _| j| j| _| j| j| _|| _| | dS )a  
        :param uri: url of the form
         ``redis+sentinel://host:port,host:port/service_name``

         If the uri scheme is ``valkey+sentinel`` the implementation used will be from
         :pypi:`valkey`.
        :param service_name: sentinel service name
         (if not provided in :attr:`uri`)
        :param use_replicas: Whether to use replicas for read only operations
        :param sentinel_kwargs: kwargs to pass as
         :attr:`sentinel_kwargs` to :class:`redis.sentinel.Sentinel`
        :param key_prefix: the prefix for each key created in redis
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`redis.sentinel.Sentinel`
        :raise ConfigurationError: when the redis library is not available
         or if the redis master host cannot be pinged.
        r   usernamepassword@   N,:/ z'service_name' not providedr   r   z	.sentinelr   )superr   __init__urllibparseurlparsecopyr   r   netlocfindsplitappendintr   pathreplacer   r   
startswithtarget_serverdependenciesmoduleZSentinelsentinelZ
master_forstorageZ	slave_forstorage_slaver   Zinitialize_storage)selfr   r   r   r   r   r   r   parsedZsentinel_configurationZsentinel_optionsZparsed_authseplochostportZsentinel_dep	__class__ P/var/www/html/venv/lib/python3.10/site-packages/limits/storage/redis_sentinel.pyr%   *   sJ   





zRedisSentinelStorage.__init__-type[Exception] | tuple[type[Exception], ...]c                 C  s&   | j dkr| jd jjS | jd jjS )Nr   r   )r2   r3   r4   Z
RedisErrorZValkeyError)r8   r@   r@   rA   base_exceptionsp   s
   
z$RedisSentinelStorage.base_exceptionsreadonlyr   c                 C  s   |r| j r| jS | jS )N)r   r7   r6   )r8   rD   r@   r@   rA   get_connectionz   s   z#RedisSentinelStorage.get_connection)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   rB   )F)rD   r   r   r   )__name__
__module____qualname____doc__ZSTORAGE_SCHEMEr   ZDEPENDENCIESr   PREFIXr%   propertyrC   rE   __classcell__r@   r@   r>   rA   r      s$    
F	r   )
__future__r   urllib.parser&   typingr   Zdeprecated.sphinxr   Zpackaging.versionr   Zlimits.errorsr   Zlimits.storage.redisr   Zlimits.typingr   r   r@   r@   r@   rA   <module>   s    