
    iJ              
           d Z ddlZddlmZ ddlmZmZmZmZm	Z	 ddl
mZmZ ddlmZmZmZ erddlmZ  G d d	eeeef         eeef         eeef         f                   ZdS )
z/This module contains the DictPersistence class.    N)deepcopy)TYPE_CHECKINGAnyDictOptionalcast)BasePersistencePersistenceInput)CDCDataConversationDictConversationKey)JSONDictc                   |    e Zd ZdZdZ	 	 	 	 	 	 	 d7dee         deded	ed
ededef fdZ	e
deeeeeef         f                  fd            Ze
defd            Ze
deeeeeef         f                  fd            Ze
defd            Ze
deeeef                  fd            Ze
defd            Ze
dee         fd            Ze
defd            Ze
deeeef                  fd            Ze
defd            Zdeeeeef         f         fdZdeeeeef         f         fdZdeeef         fdZdee         fdZdedefdZdede d ee         ddfd!Z!d"ed#eeef         ddfd$Z"d%ed#eeef         ddfd&Z#d#eeef         ddfd'Z$d#eddfd(Z%d%eddfd)Z&d"eddfd*Z'd"ed+eeef         ddfd,Z(d%ed-eeef         ddfd.Z)d/eeef         ddfd0Z*d8d1Z+e,d2eeef         defd3            Z-e,d4edeeef         fd5            Z.e,d#edeeeeef         f         fd6            Z/ xZ0S )9DictPersistenceay
  Using Python's :obj:`dict` and :mod:`json` for making your bot persistent.

    Attention:
        The interface provided by this class is intended to be accessed exclusively by
        :class:`~telegram.ext.Application`. Calling any of the methods below manually might
        interfere with the integration of persistence into :class:`~telegram.ext.Application`.

    Note:
        * Data managed by :class:`DictPersistence` is in-memory only and will be lost when the bot
          shuts down. This is, because :class:`DictPersistence` is mainly intended as starting
          point for custom persistence classes that need to JSON-serialize the stored data before
          writing them to file/database.

        * This implementation of :class:`BasePersistence` does not handle data that cannot be
          serialized by :func:`json.dumps`.

    .. seealso:: :wiki:`Making Your Bot Persistent <Making-your-bot-persistent>`

    .. versionchanged:: 20.0
        The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`.

    Args:
        store_data (:class:`~telegram.ext.PersistenceInput`, optional): Specifies which kinds of
            data will be saved by this persistence instance. By default, all available kinds of
            data will be saved.
        user_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
            user_data on creating this persistence. Default is ``""``.
        chat_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
            chat_data on creating this persistence. Default is ``""``.
        bot_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
            bot_data on creating this persistence. Default is ``""``.
        conversations_json (:obj:`str`, optional): JSON string that will be used to reconstruct
            conversation on creating this persistence. Default is ``""``.
        callback_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct
            callback_data on creating this persistence. Default is ``""``.

            .. versionadded:: 13.6
        update_interval (:obj:`int` | :obj:`float`, optional): The
            :class:`~telegram.ext.Application` will update
            the persistence in regular intervals. This parameter specifies the time (in seconds) to
            wait between two consecutive runs of updating the persistence. Defaults to 60 seconds.

            .. versionadded:: 20.0
    Attributes:
        store_data (:class:`~telegram.ext.PersistenceInput`): Specifies which kinds of data will
            be saved by this persistence instance.
    )
	_bot_data_bot_data_json_callback_data_callback_data_json
_chat_data_chat_data_json_conversations_conversations_json
_user_data_user_data_jsonN <   
store_datauser_data_jsonchat_data_jsonbot_data_jsonconversations_jsoncallback_data_jsonupdate_intervalc                    t                                          ||           d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _	        d | _
        d | _        |rL	 |                     |          | _        || _        n)# t          t          f$ r}t          d          |d }~ww xY w|rL	 |                     |          | _        || _        n)# t          t          f$ r}t          d          |d }~ww xY w|rt	 t!          j        |          | _        || _	        n)# t          t          f$ r}t          d          |d }~ww xY wt%          | j        t&                    st          d          |r	 t!          j        |          }	n)# t          t          f$ r}t          d          |d }~ww xY w	 |	d | _        n2t)          t*          d |	d         D             |	d	         f          | _        || _
        n)# t          t,          f$ r}t          d
          |d }~ww xY w| j        St/          d | j        d         D                       r t%          | j        d	         t&                    st          d
          |rM	 |                     |          | _        || _        d S # t          t          f$ r}t          d          |d }~ww xY wd S )N)r   r#   z4Unable to deserialize user_data_json. Not valid JSONz4Unable to deserialize chat_data_json. Not valid JSONz3Unable to deserialize bot_data_json. Not valid JSONz%bot_data_json must be serialized dictz8Unable to deserialize callback_data_json. Not valid JSONc                 :    g | ]\  }}}|t          |          |fS  )float).0onetwothrees       c/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/telegram/ext/_dictpersistence.py
<listcomp>z,DictPersistence.__init__.<locals>.<listcomp>   s+    RRRsC3c

E2RRR    r      z0callback_data_json is not in the required formatc              3      K   | ]:}t          |d          t                    ot          |d         t                    V  ;dS )   r   N)
isinstancedictstr)r(   entrys     r,   	<genexpr>z+DictPersistence.__init__.<locals>.<genexpr>   sW         uQx..L:eAh3L3L     r.   z8Unable to deserialize conversations_json. Not valid JSON)super__init__r   r   r   r   r   r   r   r   r   r    _decode_user_chat_data_from_json
ValueErrorAttributeError	TypeErrorjsonloadsr2   r3   r   r   
IndexErrorall_decode_conversations_from_json)selfr   r   r   r    r!   r"   r#   excdata	__class__s             r,   r8   zDictPersistence.__init__]   s    	JPPP"".2.2-126 26  	aa"&"G"G"W"W'5$$/ a a a VWW]``a 	aa"&"G"G"W"W'5$$/ a a a VWW]``a 	I`!%M!:!:&3##/ ` ` ` UVV\__`dnd33 I GHHH 	Tz"455/   N 
]<*.D''*.RR$q'RRRTXYZT[\+ +D' ,>((
+ ] ] ] RSSY\\]".  !%!4Q!7     /
 "$"5a"8$?? /   RSSS 	&*&J&JK]&^&^#+=(((/   N 		 	s   .!B B6!B11B6<!C D/C??D
 D+ +E<EEF F<'F77F< AH H*H%%H*
!J- -K>KKreturnc                     | j         S )z%:obj:`dict`: The user_data as a dict.)r   rB   s    r,   	user_datazDictPersistence.user_data        r.   c                 P    | j         r| j         S t          j        | j                  S )z6:obj:`str`: The user_data serialized as a JSON-string.)r   r=   dumpsrI   rH   s    r,   r   zDictPersistence.user_data_json   *      	(''z$.)))r.   c                     | j         S )z%:obj:`dict`: The chat_data as a dict.)r   rH   s    r,   	chat_datazDictPersistence.chat_data   rJ   r.   c                 P    | j         r| j         S t          j        | j                  S )z6:obj:`str`: The chat_data serialized as a JSON-string.)r   r=   rL   rO   rH   s    r,   r   zDictPersistence.chat_data_json   rM   r.   c                     | j         S )z$:obj:`dict`: The bot_data as a dict.)r   rH   s    r,   bot_datazDictPersistence.bot_data   s     ~r.   c                 P    | j         r| j         S t          j        | j                  S )z5:obj:`str`: The bot_data serialized as a JSON-string.)r   r=   rL   rR   rH   s    r,   r    zDictPersistence.bot_data_json   s*      	'&&z$-(((r.   c                     | j         S )zTuple[List[Tuple[:obj:`str`, :obj:`float`, Dict[:obj:`str`, :class:`object`]]],         Dict[:obj:`str`, :obj:`str`]]: The metadata on the stored callback data.

        .. versionadded:: 13.6
        )r   rH   s    r,   callback_datazDictPersistence.callback_data   s     ""r.   c                 P    | j         r| j         S t          j        | j                  S )zo:obj:`str`: The metadata on the stored callback data as a JSON-string.

        .. versionadded:: 13.6
        )r   r=   rL   rU   rH   s    r,   r"   z"DictPersistence.callback_data_json   s+     # 	,++z$,---r.   c                     | j         S )z):obj:`dict`: The conversations as a dict.)r   rH   s    r,   conversationszDictPersistence.conversations   s     ""r.   c                     | j         r| j         S | j        r|                     | j                  S t          j        | j                  S )z::obj:`str`: The conversations serialized as a JSON-string.)r   rX   _encode_conversations_to_jsonr=   rL   rH   s    r,   r!   z"DictPersistence.conversations_json   sN     # 	,++ 	J55d6HIIIz$,---r.   c                 J   K   | j         i | _        t          | j                   S )zReturns the user_data created from the ``user_data_json`` or an empty :obj:`dict`.

        Returns:
            :obj:`dict`: The restored user data.
        )rI   r   r   rH   s    r,   get_user_datazDictPersistence.get_user_data   '       >! DO'''r.   c                 J   K   | j         i | _        t          | j                   S )zReturns the chat_data created from the ``chat_data_json`` or an empty :obj:`dict`.

        Returns:
            :obj:`dict`: The restored chat data.
        )rO   r   r   rH   s    r,   get_chat_datazDictPersistence.get_chat_data   r]   r.   c                 J   K   | j         i | _        t          | j                   S )zReturns the bot_data created from the ``bot_data_json`` or an empty :obj:`dict`.

        Returns:
            :obj:`dict`: The restored bot data.
        )rR   r   r   rH   s    r,   get_bot_datazDictPersistence.get_bot_data  s'       = DN&&&r.   c                 N   K   | j         	d| _        dS t          | j                   S )ad  Returns the callback_data created from the ``callback_data_json`` or :obj:`None`.

        .. versionadded:: 13.6

        Returns:
            Tuple[List[Tuple[:obj:`str`, :obj:`float`, Dict[:obj:`str`, :class:`object`]]],                 Dict[:obj:`str`, :obj:`str`]]: The restored metadata or :obj:`None`,                 if no data was stored.
        N)rU   r   r   rH   s    r,   get_callback_dataz!DictPersistence.get_callback_data  s/       %"&D4*+++r.   namec                 |   K   | j         i | _        | j                             |i                                           S )zReturns the conversations created from the ``conversations_json`` or an empty
        :obj:`dict`.

        Returns:
            :obj:`dict`: The restored conversations data.
        )rX   r   getcopy)rB   rd   s     r,   get_conversationsz!DictPersistence.get_conversations  s>       %"$D!%%dB//44666r.   key	new_statec                    K   | j         si | _         | j                             |i                               |          |k    rdS || j         |         |<   d| _        dS )a  Will update the conversations for the given handler.

        Args:
            name (:obj:`str`): The handler's name.
            key (:obj:`tuple`): The key the state is changed for.
            new_state (:obj:`tuple` | :class:`object`): The new state for the given key.
        N)r   
setdefaultrf   r   )rB   rd   ri   rj   s       r,   update_conversationz#DictPersistence.update_conversation)  sl       " 	%"$D))$3377<<	IIF)2D!#&#'   r.   user_idrD   c                    K   | j         i | _         | j                             |          |k    rdS || j         |<   d| _        dS )zWill update the user_data (if changed).

        Args:
            user_id (:obj:`int`): The user the data might have been changed for.
            data (:obj:`dict`): The :attr:`telegram.ext.Application.user_data` ``[user_id]``.
        N)r   rf   r   )rB   rn   rD   s      r,   update_user_dataz DictPersistence.update_user_data:  Q       ?" DO?w''4//F#' #r.   chat_idc                    K   | j         i | _         | j                             |          |k    rdS || j         |<   d| _        dS )zWill update the chat_data (if changed).

        Args:
            chat_id (:obj:`int`): The chat the data might have been changed for.
            data (:obj:`dict`): The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``.
        N)r   rf   r   )rB   rr   rD   s      r,   update_chat_dataz DictPersistence.update_chat_dataH  rq   r.   c                 @   K   | j         |k    rdS || _         d| _        dS )zWill update the bot_data (if changed).

        Args:
            data (:obj:`dict`): The :attr:`telegram.ext.Application.bot_data`.
        N)r   r   rB   rD   s     r,   update_bot_datazDictPersistence.update_bot_dataV  s/       >T!!F"r.   c                 @   K   | j         |k    rdS || _         d| _        dS )aK  Will update the callback_data (if changed).

        .. versionadded:: 13.6

        Args:
            data (Tuple[List[Tuple[:obj:`str`, :obj:`float`, Dict[:obj:`str`, :class:`object`]]],                 Dict[:obj:`str`, :obj:`str`]]): The relevant data to restore
                :class:`telegram.ext.CallbackDataCache`.
        N)r   r   rv   s     r,   update_callback_dataz$DictPersistence.update_callback_dataa  s1       $&&F"#'   r.   c                 `   K   | j         dS | j                             |d           d| _        dS )zWill delete the specified key from the :attr:`chat_data`.

        .. versionadded:: 20.0

        Args:
            chat_id (:obj:`int`): The chat id to delete from the persistence.
        N)r   popr   )rB   rr   s     r,   drop_chat_datazDictPersistence.drop_chat_datap  ;       ?"FGT***#r.   c                 `   K   | j         dS | j                             |d           d| _        dS )zWill delete the specified key from the :attr:`user_data`.

        .. versionadded:: 20.0

        Args:
            user_id (:obj:`int`): The user id to delete from the persistence.
        N)r   r{   r   )rB   rn   s     r,   drop_user_datazDictPersistence.drop_user_data}  r}   r.   rI   c                 
   K   dS )zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
        Nr&   )rB   rn   rI   s      r,   refresh_user_dataz!DictPersistence.refresh_user_data  
        r.   rO   c                 
   K   dS )zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
        Nr&   )rB   rr   rO   s      r,   refresh_chat_dataz!DictPersistence.refresh_chat_data  r   r.   rR   c                 
   K   dS )zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
        Nr&   )rB   rR   s     r,   refresh_bot_dataz DictPersistence.refresh_bot_data  r   r.   c                 
   K   dS )zvDoes nothing.

        .. versionadded:: 20.0
        .. seealso:: :meth:`telegram.ext.BasePersistence.flush`
        Nr&   rH   s    r,   flushzDictPersistence.flush  r   r.   rX   c                     i }|                                  D ]A\  }}i ||<   |                                 D ]"\  }}|||         t          j        |          <   #Bt          j        |          S )af  Helper method to encode a conversations dict (that uses tuples as keys) to a
        JSON-serializable way. Use :meth:`self._decode_conversations_from_json` to decode.

        Args:
            conversations (:obj:`dict`): The conversations dict to transform to JSON.

        Returns:
            :obj:`str`: The JSON-serialized conversations dict
        )itemsr=   rL   )rX   tmphandlerstatesri   states         r,   rZ   z-DictPersistence._encode_conversations_to_json  sx     $&,2244 	6 	6OGVCL$llnn 6 6
U05GTZ__--6z#r.   json_stringc           	          t          j        |           }i }|                                D ]N\  }}i ||<   |                                D ]/\  }}|||         t          t          j        |                    <   0O|S )aQ  Helper method to decode a conversations dict (that uses tuples as keys) from a
        JSON-string created with :meth:`self._encode_conversations_to_json`.

        Args:
            json_string (:obj:`str`): The conversations dict as JSON string.

        Returns:
            :obj:`dict`: The conversations dict after decoding
        )r=   r>   r   tuple)r   r   rX   r   r   ri   r   s          r,   rA   z/DictPersistence._decode_conversations_from_json  s     j%%57"yy{{ 	G 	GOGV%'M'"$llnn G G
UAFg&uTZ__'='=>>Gr.   c                    i }t          j        |           }|                                D ]a\  }}t          |          }i ||<   |                                D ]3\  }}	 t          |          }n# t          $ r |}Y nw xY w|||         |<   4b|S )a  Helper method to decode chat or user data (that uses ints as keys) from a
        JSON-string.

        Args:
            data (:obj:`str`): The user/chat_data dict as JSON string.

        Returns:
            :obj:`dict`: The user/chat_data defaultdict after decoding
        )r=   r>   r   intr:   )	rD   r   decoded_datauserrI   int_user_idri   value_ids	            r,   r9   z0DictPersistence._decode_user_chat_data_from_json  s     02z$''+1133 	. 	.OD)d))K!C'oo// . .
Uc((CC!   CCC(-K %%. 
s   A..A=<A=)Nr   r   r   r   r   r   )rF   N)1__name__
__module____qualname____doc__	__slots__r   r
   r4   r'   r8   propertyr   r   r   rI   r   rO   r   rR   r    r   rU   r"   r   rX   r!   objectr\   r_   ra   rc   rh   r   rm   rp   rt   rw   ry   r|   r   r   r   r   r   staticmethodrZ   rA   r9   __classcell__)rE   s   @r,   r   r      s       . .`I 26  "$"$!#M M-.M M 	M
 M  M  M M M M M M M^ 8Dd38n)<$=>    X * * * * X* 8Dd38n)<$=>    X * * * * X* (4S>2    X )s ) ) ) X) #x0 # # # X# .C . . . X. #xS2B-B(CD # # # X# .C . . . X.(T#tFFN/C*C%D ( ( ( ((T#tFFN/C*C%D ( ( ( ('D$8 ' ' ' ','): , , , ,	7C 	74D 	7 	7 	7 	7((-(:B6:J(	( ( ( ("$c $c3h $D $ $ $ $$c $c3h $D $ $ $ $	#$sCx. 	#T 	# 	# 	# 	#(w (4 ( ( ( ($C $D $ $ $ $$C $D $ $ $ $s tCH~ RV    s tCH~ RV    tCH~ $        T#?O:O5P UX    \" S T#GWBW=X    \$ s tCffnAU<U7V    \    r.   r   )r   r=   rg   r   typingr   r   r   r   r   telegram.extr	   r
   telegram.ext._utils.typesr   r   r   telegram._utils.typesr   r   r&   r.   r,   <module>r      s  & 6 5        ; ; ; ; ; ; ; ; ; ; ; ; ; ; : : : : : : : : P P P P P P P P P P /......B B B B Bod38nd38nd3PS8n&TU B B B B Br.   