
    soj                        d dl Z d dlmZ d dl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	ej        d
e	dedededefdZ	 dd	ej        d
e	dedededdfdZ	 dd	ej        d
e	dededdf
dZ	 dd	edededdfdZ	 	 dd	ededededef
dZ	 dd	ededededdf
dZ	 	 dd	ededededef
dZ	 dd	ededededdf
dZd	edefdZdS )    N)datetime)CallbackContextContextTypes)phrases)ContextDataKeyContextDataType)CommonChatException)get_random_stringTcontext	data_typekey	inner_keytolerate_key_exceptionreturnc                 "   |t           j        u r| j        n| j        }	 |||         d         |         d         S ||         d         S # t          $ r<}|r3t          j        d| d           t          t          j	        d          |d}~ww xY w)a!  
    Get the bot context data
    :param context: The context
    :param data_type: The type
    :param key: The key
    :param inner_key: The inner key
    :param tolerate_key_exception: If the key is not found, raise CommonChatException instead of
    KeyError
    :return: The data
    NvaluezKey zF not found in context user data, this might be due to a system restartT)only_message)
r   BOTbot_data	user_dataKeyErrorloggingdebugr	   r   RESTART_BOT)r   r   r   r   r   dataes          /app/src/utils/context_utils.pyget_context_datar      s    $  )O,???7WEVD 9W%i0999W%%   ! 	Mbsbbb   &g&9MMMMGs   A A 
B7B		Br   c                     |t           j        u r| j        n| j        }||vrdi i||<   |=d|i||         d         |<   t	          j                    ||         d         |         d<   dS |||         d<   t	          j                    ||         d<   dS )z
    Set the bot context data
    :param context: The context
    :param data_type: The type
    :param key: The key
    :param value: The value
    :param inner_key: The inner key
    :return: None
    r   Nlast_updated)r   r   r   r   r   now)r   r   r   r   r   r   s         r   set_context_datar"   /   s    "  )O,???7WEVD
$bMS	)0%(8S	'9%8@S	'9%n555"S	'$,LNNS	.!!!    c                     |t           j        u r| j        n| j        }||vrdS |#||         d                             |           dS |                    |           dS )z
    Remove the bot context data
    :param context: The context
    :param data_type: The type
    :param key: The key
    :param inner_key: The inner key
    :return: None
    Nr   )r   r   r   r   pop)r   r   r   r   r   s        r   remove_context_datar&   M   sk      )O,???7WEVD
$S	'y)))))r#   c                 >    t          | t          j        ||           dS )z
    Remove the user context data
    :param context: The context
    :param key: The key
    :param inner_key: The inner key
    :return: None
    N)r&   r   USER)r   r   r   s      r   remove_user_context_datar)   g   s!     !5sIFFFFFr#   c                 <    t          | t          j        |||          S )a  
    Get the bot context data
    :param context: The context
    :param key: The key
    :param inner_key: The inner key
    :param tolerate_key_exception: If the key is not found, raise CommonChatException instead of
    KeyError
    :return: The data
    )r   r   r   r   r   r   r   s       r   get_bot_context_datar,   u   s      G_%8#yJ`aaar#   c                 @    t          | t          j        |||           dS )z
    Set the bot context data
    :param context: The context
    :param key: The key
    :param value: The value
    :param inner_key: The inner key
    :return: None
    N)r"   r   r   r   r   r   r   s       r   set_bot_context_datar/      s#     Wo13yIIIIIr#   c                 <    t          | t          j        |||          S )a  
    Get the user context data
    :param context: The context
    :param key: The key
    :param inner_key: The inner key
    :param tolerate_key_exception: If the key is not found, raise CommonChatException instead of
    KeyError
    :return: The data
    )r   r   r(   r+   s       r   get_user_context_datar1      s      G_%93	Kabbbr#   c                 @    t          | t          j        |||           dS )z
    Set the user context data
    :param context: The context
    :param key: The key
    :param value: The value
    :param inner_key: The inner key
    :return: None
    N)r"   r   r(   r.   s       r   set_user_context_datar3      s#     Wo2C	JJJJJr#   c                     t          d          D ]B}t                      }	 t          | t          j        |d           /# t
          $ r |cY c S w xY wt          j        d           t          d          )zu
    Get a random inner key for the user context data
    :param context: The context
    :return: The inner key
    d   F)r   r   z;Could not find a random inner key for the user context datazFailed to generate an inner key)	ranger
   r1   r   INLINE_QUERYr   r   errorr	   )r   ir   s      r   'get_random_user_context_inner_query_keyr:      s     3ZZ  %''			!+#',	     	 	 		 MOPPP
?
@
@@s   ?AA)NT)N)r   r   src.tg_compat.extr   r   	resourcesr   src.model.enums.ContextDataKeyr   r   src.model.error.CommonChatErrorr	   src.utils.download_utilsr
   DEFAULT_TYPEstrboolanyr   r"   r&   r)   r,   r/   r1   r3   r:    r#   r   <module>rE      s_          ; ; ; ; ; ; ; ;       J J J J J J J J ? ? ? ? ? ? 6 6 6 6 6 6 #'   &   
  	 
 !  	       P 3 3&33 
3 	3
 3 
3 3 3 3D 	 & 
 	
 
   6 EIG GG#1G>AG	G G G G" #'	b bb	b b !	b
 	b b b b( QUJ JJ#1J:=JJMJ	J J J J$ #'	c cc	c c !	c
 	c c c c( QUK KK#1K:=KJMK	K K K KA_ A A A A A A Ar#   