o
    ri                     @  s   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 d dlmZ d dlmZ er3	 ed	d
dG dd deZdS )    )annotations)TYPE_CHECKING)versionchanged)Version)parse_storage_uri)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                   s2  t t| j|fd|i| t|}g }	|r| ni }
i }|d|j }r+||d< |d|j }r8||d< |	|j	 || _
|jrK|jddn|| _| jdu rWtd|dr^d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/ Nz'service_name' not providedr   r   z	.sentinelr   )superr   __init__r   copygetr   r   extend	locationsr   pathreplacer   r   
startswithtarget_serverdependenciesmoduleSentinelsentinel
master_forstorage	slave_forstorage_slaver   initialize_storage)selfr   r   r   r   r   r   r   storage_uri_optionssentinel_configurationsentinel_optionsparsed_authr   r   sentinel_dep	__class__ \/var/www/html/pca-backend/venv/lib/python3.10/site-packages/limits/storage/redis_sentinel.pyr"   *   sH   



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   )r*   r+   r,   
RedisErrorValkeyError)r4   r<   r<   r=   base_exceptionsn   s
   
z$RedisSentinelStorage.base_exceptionsreadonlyr	   c                 C  s   |r| j r| jS | jS )N)r   r2   r0   )r4   rB   r<   r<   r=   get_connectionx   s   z#RedisSentinelStorage.get_connection)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r>   )F)rB   r   r   r	   )__name__
__module____qualname____doc__STORAGE_SCHEMEr   DEPENDENCIESr   PREFIXr"   propertyrA   rC   __classcell__r<   r<   r:   r=   r      s$    
D	r   N)
__future__r   typingr   deprecated.sphinxr   packaging.versionr   limits._storage_schemer   limits.errorsr   limits.storage.redisr   limits.typingr	   r   r<   r<   r<   r=   <module>   s    