o
    4˜Žj½  ã                   @   sä   d dl Z d dlmZmZmZ 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 d d
lmZ d dlmZmZmZmZmZmZ ddiZedef Zedef Z G dd„ deƒZ!G dd„ deƒZ"dS )é    N)ÚAsyncGeneratorÚCallableÚMapping)Úasynccontextmanager)ÚAnyÚcast)ÚRedis)ÚConnectionPool)ÚLock)ÚExpiryT)ÚDataNotDictLikeError)ÚState)ÚBaseEventIsolationÚBaseStorageÚDefaultKeyBuilderÚ
KeyBuilderÚ	StateTypeÚ
StorageKeyÚtimeouté<   .c                   @   s  e Zd ZdZdddejejfdededB de	dB de	dB de
ded	dfd
d„Ze	d!dedeeef dB ded	d fdd„ƒZded	dfdd„Zd"dd„Z	d!deded	dfdd„Zded	edB fdd„Zdedeeef d	dfdd„Zded	eeef fdd „ZdS )#ÚRedisStoragezV
    Redis storage requires the :code:`redis` package (:code:`pip install redis`)
    NÚredisÚkey_builderÚ	state_ttlÚdata_ttlÚ
json_loadsÚ
json_dumpsÚreturnc                 C   s6   |du rt ƒ }|| _|| _|| _|| _|| _|| _dS )zí
        :param redis: instance of Redis connection
        :param key_builder: builder that helps to convert contextual key to string
        :param state_ttl: TTL for state records
        :param data_ttl: TTL for data records
        N)r   r   r   r   r   r   r   )Úselfr   r   r   r   r   r   © r   úV/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/aiogram/fsm/storage/redis.pyÚ__init__    s   
zRedisStorage.__init__ÚurlÚconnection_kwargsÚkwargsc                 K   s:   |du ri }t j|fi |¤Ž}t|d�}| dd|i|¤ŽS )ai  
        Create an instance of :class:`RedisStorage` with the specified connection url

        :param url: the connection url (i.e. :code:`redis://user:password@host:port/db`)
        :param connection_kwargs: see :code:`redis` docs
        :param kwargs: arguments passed to :class:`RedisStorage`
        :return: an instance of :class:`RedisStorage`
        N©Úconnection_poolr   r   ©r	   Úfrom_urlr   ©Úclsr"   r#   r$   Úpoolr   r   r   r    r(   8   s
   
zRedisStorage.from_urlÚRedisEventIsolationc                 K   s   t d| j| jdœ|¤ŽS )N)r   r   r   )r,   r   r   )r   r$   r   r   r    Úcreate_isolationM   s   zRedisStorage.create_isolationc                 Ã   s   �| j jdd�I d H  d S )NT)Úclose_connection_pool)r   Úaclose©r   r   r   r    ÚcloseP   s   €zRedisStorage.closeÚkeyÚstatec                 Ã   sb   �| j  |d¡}|d u r| j |¡I d H  d S | jj|ttt|tƒr%|j	n|ƒ| j
d�I d H  d S )Nr3   ©Úex)r   Úbuildr   ÚdeleteÚsetr   ÚstrÚ
isinstancer   r3   r   )r   r2   r3   Ú	redis_keyr   r   r    Ú	set_stateS   s   €ýzRedisStorage.set_statec                 Ã   sD   �| j  |d¡}| j |¡I d H }t|tƒr| d¡S ttd B |ƒS )Nr3   úutf-8)	r   r6   r   Úgetr:   ÚbytesÚdecoder   r9   ©r   r2   r;   Úvaluer   r   r    Ú	get_stateb   s   €

zRedisStorage.get_stateÚdatac                 Ã   sp   �t |tƒsdt|ƒj› �}t|ƒ‚| j |d¡}|s&| j |¡I d H  d S | jj	||  
|¡| jd�I d H  d S )Nz-Data must be a dict or dict-like object, got rD   r4   )r:   ÚdictÚtypeÚ__name__r   r   r6   r   r7   r8   r   r   )r   r2   rD   Úmsgr;   r   r   r    Úset_datal   s   €
ýzRedisStorage.set_datac                 Ã   sZ   �| j  |d¡}| j |¡I d H }|d u ri S t|tƒr!| d¡}ttt	t
f |  |¡ƒS )NrD   r=   )r   r6   r   r>   r:   r?   r@   r   rE   r9   r   r   rA   r   r   r    Úget_data   s   €

zRedisStorage.get_data©N©r   N)rG   Ú
__module__Ú__qualname__Ú__doc__ÚjsonÚloadsÚdumpsr   r   r   Ú
_JsonLoadsÚ
_JsonDumpsr!   Úclassmethodr9   rE   r   r(   r-   r1   r   r   r<   rC   r   rI   rJ   r   r   r   r    r      sx    ùþýüûúù
øýþýüû
ýþý
üþ
ý
þ
ý
üþ
ýr   c                
   @   sŽ   e Zd Z		ddededB deeef dB ddfdd„Ze		dded	eeef dB d
edd fdd„ƒZ
ededed fdd„ƒZddd„ZdS )r,   Nr   r   Úlock_kwargsr   c                 C   s0   |d u rt ƒ }|d u rt}|| _|| _|| _d S rK   )r   ÚDEFAULT_REDIS_LOCK_KWARGSr   r   rV   )r   r   r   rV   r   r   r    r!   �   s   
zRedisEventIsolation.__init__r"   r#   r$   c                 K   s:   |d u ri }t j|fi |¤Ž}t|d�}| dd|i|¤ŽS )Nr%   r   r   r'   r)   r   r   r    r(   ›   s
   
zRedisEventIsolation.from_urlr2   )NNc              	   C  sp   �| j  |d¡}| jjdd|i| j¤dti¤Ž4 I d H š d V  W d   ƒI d H  d S 1 I d H s1w   Y  d S )NÚlockÚnameÚ
lock_classr   )r   r6   r   rX   rV   r
   )r   r2   r;   r   r   r    rX   ¨   s
   €*.ÿzRedisEventIsolation.lockc                 Ã   s   �d S rK   r   r0   r   r   r    r1   ±   s   €zRedisEventIsolation.closerK   rL   )rG   rM   rN   r   r   rE   r9   r   r!   rU   r(   r   r   r   rX   r1   r   r   r   r    r,   Œ   s<    üþýü
ûýþýüûþýr,   )#rP   Úcollections.abcr   r   r   Ú
contextlibr   Útypingr   r   Úredis.asyncio.clientr   Úredis.asyncio.connectionr	   Úredis.asyncio.lockr
   Úredis.typingr   Úaiogram.exceptionsr   Úaiogram.fsm.stater   Úaiogram.fsm.storage.baser   r   r   r   r   r   rW   rS   r9   rT   r   r,   r   r   r   r    Ú<module>   s      	q