
    x'h&                         d Z 	 ddlZddlmZmZ ddlZddlmZm	Z	m
Z
mZ ddlZddlmZmZ de
def   d	e
def   fd
Zde
def   d	e
def   fdZ G d de      Zy# e$ r dZ Y Uw xY w)T    N)RedisConnectionPoolF)OptionalUnionCallable	Coroutine)StateStorageBaseStateDataContextfunc.returnc                       fd}|S )Nc                    K   | j                   4 d {     | g|i | d {   cd d d       d {    S 7 )7 7 	# 1 d {  7  sw Y   y xY wwN)lock)selfargskwargsr   s      ]/var/www/flask-api/venv/lib/python3.12/site-packages/telebot/asyncio_storage/redis_storage.pywrapperz async_with_lock.<locals>.wrapper   sV     99 	5 	5d4T4V44	5 	5 	54	5 	5 	5 	5sP   A>AAA AAAA AAA
AAA r   r   s   ` r   async_with_lockr      s    5 N    c                       fd}|S )Nc                 ,  K   | j                   j                         4 d {   }|j                           | |g|i | d {   }|j                          d {    |cd d d       d {    S 7 T7 17 7 # 1 d {  7  sw Y   y xY wwr   )redispipelinemultiexecute)r   r   r   piperesultr   s        r   r   z$async_with_pipeline.<locals>.wrapper   s     ::&&( 	 	DJJLd<T<V<<F,,.  		 	 	< 	 	 	 	sh   BA7B!A?A9A?A; A?%B1A=2B9A?;A?=B?BBBBr   r   s   ` r   async_with_pipeliner"      s     Nr   c                      e Zd ZdZ	 	 	 	 	 	 	 	 ddddee   ddfdZee	 	 	 dde	d	e	d
edee   dee	   dee	   de
fd              Z	 	 	 dde	d	e	dee   dee	   dee	   deedf   fdZ	 	 	 dde	d	e	dee   dee	   dee	   de
fdZee	 	 	 dde	d	e	dedeee	eef   dee   dee	   dee	   de
fd              Z	 	 	 dde	d	e	dee   dee	   dee	   defdZee	 	 	 dde	d	e	dee   dee	   dee	   de
fd              Z	 	 	 dde	d	e	dee   dee	   dee	   dee   fdZee	 	 	 dde	d	e	dedee   dee	   dee	   de
fd              Zdde	dee   fdZdefdZy)StateRedisStorageaP  
    State storage based on Redis.

    .. code-block:: python3

        storage = StateRedisStorage(...)
        bot = AsyncTeleBot(token, storage=storage)

    :param host: Redis host, default is "localhost".
    :type host: str

    :param port: Redis port, default is 6379.
    :type port: int

    :param db: Redis database, default is 0.
    :type db: int

    :param password: Redis password, default is None.
    :type password: Optional[str]

    :param prefix: Prefix for keys, default is "telebot".
    :type prefix: Optional[str]

    :param redis_url: Redis URL, default is None.
    :type redis_url: Optional[str]

    :param connection_pool: Redis connection pool, default is None.
    :type connection_pool: Optional[ConnectionPool]

    :param separator: Separator for keys, default is ":".
    :type separator: Optional[str]

    Nconnection_poolr   	separatorr   c	                 @   t         st        d      || _        || _        | j                  st	        d      |r%t
        j                  j                  |      | _        n(|rt        |      | _        nt        ||||      | _        t        j                         | _
        y )Nz.Please install redis using `pip install redis`zPrefix cannot be empty)r%   )hostportdbpassword)redis_installedImportErrorr&   prefix
ValueErrorr   asynciofrom_urlr   Lockr   )	r   r(   r)   r*   r+   r.   	redis_urlr%   r&   s	            r   __init__zStateRedisStorage.__init__E   s{     NOO"{{566//	:DJ?DJDtXNDJLLN	r   chat_iduser_idstatebusiness_connection_idmessage_thread_idbot_idc           	      ~  K   t        |d      r|j                  }| j                  ||| j                  | j                  |||      }|j                  |d       |j                          d {   }	|	d   }
|
&|j                  |dt        j                  i              |j                  |d|       d {    y7 M7 w)Nnamedatar   r7   T)
hasattrr<   _get_keyr.   r&   hgetr   hsetjsondumps)r   r    r5   r6   r7   r8   r9   r:   _keyr!   r=   s              r   	set_statezStateRedisStorage.set_stateb   s      5&!JJE}}KKNN"
 			$||~%ay<IIdFDJJrN3iigu--- &
 	.s%   A)B=+B9,AB=3B;4B=;B=c           	         K   | j                  ||| j                  | j                  |||      }| j                  j	                  |d       d {   }|r|j                  d      S d S 7 w)Nr7   utf-8)r?   r.   r&   r   r@   decode)r   r5   r6   r8   r9   r:   rD   state_bytess           r   	get_statezStateRedisStorage.get_state   si      }}KKNN"
 !JJOOD'::.9{!!'*CtC ;s   AA)A'A)c           	         K   | j                  ||| j                  | j                  |||      }| j                  j	                  |       d {   }|dkD  S 7 	w)Nr   )r?   r.   r&   r   delete)r   r5   r6   r8   r9   r:   rD   r!   s           r   delete_statezStateRedisStorage.delete_state   s[      }}KKNN"
 zz((..z /s   A
AA
Akeyvaluec	           	        K   | j                  ||| j                  | j                  |||      }	|j                  |	d       d {   }
|j	                          d {   }
|
d   }
|
t        d|	 d      t        j                  |
      }
||
|<   |j                  |	dt        j                  |
             d {    y7 {7 e7 	w)Nr=   r   zStateRedisStorage: key z does not exist.T)
r?   r.   r&   r@   r   RuntimeErrorrB   loadsrA   rC   )r   r    r5   r6   rN   rO   r8   r9   r:   rD   r=   s              r   set_datazStateRedisStorage.set_data   s      }}KKNN"
 YYtV,,\\^#Aw<!8>NOPP::d#DDI))D&$**T*:;;; -# <s7   ACB?CCAC9C:CCCc           	         K   | j                  ||| j                  | j                  |||      }| j                  j	                  |d       d {   }|rt        j                  |      S i S 7 w)Nr=   )r?   r.   r&   r   r@   rB   rR   )r   r5   r6   r8   r9   r:   rD   r=   s           r   get_datazStateRedisStorage.get_data   sg      }}KKNN"
 ZZ__T622#'tzz$/R/ 3s   AA-A+A-c           	         K   | j                  ||| j                  | j                  |||      }|j                  |       d {   r|j	                  |dd       d {    yy7 !7 w)Nr=   z{}FT)r?   r.   r&   existsrA   )r   r    r5   r6   r8   r9   r:   rD   s           r   
reset_datazStateRedisStorage.reset_data   sp      }}KKNN"
 T"""))D&$///   #/s$   A A(A$A(A&A(&A(c                 $    t        | |||||      S )N)r5   r6   r8   r9   r:   )r
   )r   r5   r6   r8   r9   r:   s         r   get_interactive_dataz&StateRedisStorage.get_interactive_data   s#      #9/
 	
r   r=   c           	         K   | j                  ||| j                  | j                  |||      }|j                  |       d {   r/|j	                  |dt        j                  |             d {    yy7 47 w)Nr=   FT)r?   r.   r&   rW   rA   rB   rC   )	r   r    r5   r6   r=   r8   r9   r:   rD   s	            r   savezStateRedisStorage.save  sy      }}KKNN"
 T"""))D&$**T*:;;;   #;s$   A A;A7-A;0A91A;9A;r.   c                    | j                   j                  | d      }|D ]"  }|j                  d      }| j                   j                  |      }t	        j
                  |      }|t        |      d }t        |j                               d   }||   d   }	||   d   }
| j                  t        |      t        |      | j                  | j                  |      }| j                   j                  |d|	       | j                   j                  |dt	        j                  |
             | j                   j                  |       % y)a_  
        Migrate from old to new format of keys.
        Run this function once to migrate all redis existing keys to new format.

        Starting from version 4.23.0, the format of keys has been changed:
        <key>:value
        - Old format: {prefix}chat_id: {user_id: {'state': None, 'data': {}}, ...}
        - New format:
        {prefix}{separator}{bot_id}{separator}{business_connection_id}{separator}{message_thread_id}{separator}{chat_id}{separator}{user_id}: {'state': ..., 'data': {}}

        This function will help you to migrate from the old format to the new one in order to avoid data loss.

        :param bot_id: Bot ID; To get it, call a getMe request and grab the id from the response.
        :type bot_id: int

        :param prefix: Prefix for keys, default is "telebot_"(old default value)
        :type prefix: Optional[str]
        *rG   Nr   r7   r=   )r:   )r   keysrH   getrB   rR   lenlistr?   intr.   r&   rA   rC   rL   )r   r:   r.   r_   rN   old_keyrO   r5   r6   r7   
state_datanew_keys               r   migrate_formatz StateRedisStorage.migrate_format'  s   & zz&|, 	'Cjj)GJJNN7+EJJu%Ec&km,G5::<(+G'N7+Ew/J mmGc'lDKKPV $ G JJOOGWe4JJOOGVTZZ
-CD JJg&'	'r   c                 "    d| j                    dS )NzStateRedisStorage())r   )r   s    r   __str__zStateRedisStorage.__str__Q  s    #DJJ<q11r   )	localhosti  r   NtelebotNN:)NNN)telebot_)__name__
__module____qualname____doc__r   strr4   r   r"   rc   boolrE   r   rJ   rM   floatdictrS   rU   rX   rZ   r\   rg   rj   r   r   r   r$   r$   "   s    H ,0#&# *# C=# 
#:  15+/ $  	
  !) $C=  
  H 15+/ $DD D !)	D
 $C=D D 
sDy	D0 15+/ $  !)	
 $C=  
(  15+/ $  	
  S#ud*+ !) $C=  
  F 15+/ $00 0 !)	0
 $C=0 0 
0(  15+/ $  	
 !) $C=  
  8 15+/ $

 
 !)	

 $C=
 
 
$
"  15+/ $  	
  !) $C=  
  2('S ('(3- ('T2 2r   r$   )r,   r   redis.asyncior   r   r-   rB   typingr   r   r   r   r0   $telebot.asyncio_storage.base_storager	   r
   r   r"   r$   r   r   r   <module>rz      s   3  7 7  S(3	>2 xY7O hsI~6 8CN;S q2( q2;  Os   A A&%A&