
    'iOJ              
           d Z ddlZddlmZ ddl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y)
z/This module contains the DictPersistence class.    N)deepcopy)TYPE_CHECKINGAnycast)BasePersistencePersistenceInput)CDCDataConversationDictConversationKey)JSONDictc                       e Zd ZdZdZ	 	 	 	 	 	 	 d5dedz  dedededed	ed
ef fdZe	de
ee
eef   f   dz  fd       Ze	defd       Ze	de
ee
eef   f   dz  fd       Ze	defd       Ze	de
eef   dz  fd       Ze	defd       Ze	dedz  fd       Ze	defd       Ze	de
eef   dz  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dz  fdZdedefdZdedededz  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d.Z)d6d/Z*e+d0e
eef   defd1       Z,e+d2ede
eef   fd3       Z-e+d!ede
ee
eef   f   fd4       Z. xZ/S )7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 | _        |r	 | j                  |      | _        || _        |r	 | j                  |      | _        || _        |rG	 t!        j"                  |      | _        || _	        t%        | j                  t&              st        d      |r	 t!        j"                  |      }		 |	d | _        n;t)        d|	d   D 
cg c]  \  }
}}|
t+        |      |f c}}}
|	d	   f      | _        || _
        | j
                  Gt/        d | j
                  d   D              rt%        | j
                  d	   t&              st        d
      |r	 | j1                  |      | _        || _        y y # t        t        f$ r}t        d      |d }~ww xY w# t        t        f$ r}t        d      |d }~ww xY w# t        t        f$ r}t        d      |d }~ww xY w# t        t        f$ r}t        d      |d }~ww xY wc c}}}
w # t        t,        f$ r}t        d
      |d }~ww xY w# t        t        f$ r}t        d      |d }~ww xY w)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 JSONr	   r      z0callback_data_json is not in the required formatc              3   l   K   | ],  }t        |d    t              xr t        |d   t               . yw)   r   N)
isinstancedictstr).0entrys     g/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/telegram/ext/_dictpersistence.py	<genexpr>z+DictPersistence.__init__.<locals>.<genexpr>   s6       uQx.L:eAh3LL   24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loadsr$   r%   r   float
IndexErrorall_decode_conversations_from_json)selfr   r   r   r   r   r   r   excdataonetwothree	__class__s                r)   r-   zDictPersistence.__init__^   s    	JP""+/+/*./3 /3 a"&"G"G"W'5$ a"&"G"G"W'5$ `!%M!:&3# dnnd3 GHHzz"45
]<*.D'*.!JNq'RRsC3c
E2RTXYZT[\+D' ,>( "". !%!4!4Q!7  "$"5"5a"8$? RSS&*&J&JK]&^#+=( [ / a VW]``a / a VW]``a / ` UV\__` / N S 
+ ] RSY\\] / Ns   F< <G !H %H% ;I I-I I2 <GGGG?.G::G?H"HH"%I4I  II I/I**I/2JJJreturnc                     | j                   S )z%:obj:`dict`: The user_data as a dict.)r   r8   s    r)   	user_datazDictPersistence.user_data            c                 p    | j                   r| j                   S t        j                  | j                        S )z6:obj:`str`: The user_data serialized as a JSON-string.)r   r2   dumpsrB   rA   s    r)   r   zDictPersistence.user_data_json   ,     '''zz$..))rD   c                     | j                   S )z%:obj:`dict`: The chat_data as a dict.)r   rA   s    r)   	chat_datazDictPersistence.chat_data   rC   rD   c                 p    | j                   r| j                   S t        j                  | j                        S )z6:obj:`str`: The chat_data serialized as a JSON-string.)r   r2   rF   rI   rA   s    r)   r   zDictPersistence.chat_data_json   rG   rD   c                     | j                   S )z$:obj:`dict`: The bot_data as a dict.)r   rA   s    r)   bot_datazDictPersistence.bot_data   s     ~~rD   c                 p    | j                   r| j                   S t        j                  | j                        S )z5:obj:`str`: The bot_data serialized as a JSON-string.)r   r2   rF   rL   rA   s    r)   r   zDictPersistence.bot_data_json   s,     &&&zz$--((rD   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   rA   s    r)   callback_datazDictPersistence.callback_data   s     """rD   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   r2   rF   rO   rA   s    r)   r   z"DictPersistence.callback_data_json   s.     ##+++zz$,,--rD   c                     | j                   S )z):obj:`dict`: The conversations as a dict.)r   rA   s    r)   conversationszDictPersistence.conversations   s     """rD   c                     | j                   r| j                   S | j                  r| j                  | j                        S t        j                  | j                        S )z::obj:`str`: The conversations serialized as a JSON-string.)r   rR   _encode_conversations_to_jsonr2   rF   rA   s    r)   r   z"DictPersistence.conversations_json   sN     ##+++55d6H6HIIzz$,,--rD   c                 Z   K   | j                   i | _        t        | j                         S w)zReturns the user_data created from the ``user_data_json`` or an empty :obj:`dict`.

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

        Returns:
            :obj:`dict`: The restored chat data.
        )rI   r   r   rA   s    r)   get_chat_datazDictPersistence.get_chat_data   rW   rX   c                 Z   K   | j                   i | _        t        | j                         S w)zReturns the bot_data created from the ``bot_data_json`` or an empty :obj:`dict`.

        Returns:
            :obj:`dict`: The restored bot data.
        )rL   r   r   rA   s    r)   get_bot_datazDictPersistence.get_bot_data  s(      == DN&&rX   c                 \   K   | j                   d| _        yt        | j                         S w)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)rO   r   r   rA   s    r)   get_callback_dataz!DictPersistence.get_callback_data  s0      %"&D**++s   *,namec                    K   | j                   i | _        | j                   j                  |i       j                         S w)zReturns the conversations created from the ``conversations_json`` or an empty
        :obj:`dict`.

        Returns:
            :obj:`dict`: The restored conversations data.
        )rR   r   getcopy)r8   r_   s     r)   get_conversationsz!DictPersistence.get_conversations  s>      %"$D!!%%dB/4466s   >A key	new_statec                    K   | j                   si | _         | j                   j                  |i       j                  |      |k(  ry|| j                   |   |<   d| _        yw)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   
setdefaultra   r   )r8   r_   rd   re   s       r)   update_conversationz#DictPersistence.update_conversation*  s_      """$D))$377<	I)2D!#&#' s   AAuser_idr:   c                    K   | j                   i | _         | j                   j                  |      |k(  ry|| j                   |<   d| _        yw)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   ra   r   )r8   ri   r:   s      r)   update_user_dataz DictPersistence.update_user_data;  I      ??" DO??w'4/#' #   A
Achat_idc                    K   | j                   i | _         | j                   j                  |      |k(  ry|| j                   |<   d| _        yw)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   ra   r   )r8   rn   r:   s      r)   update_chat_dataz DictPersistence.update_chat_dataI  rl   rm   c                 H   K   | j                   |k(  ry|| _         d| _        yw)zWill update the bot_data (if changed).

        Args:
            data (:obj:`dict`): The :attr:`telegram.ext.Application.bot_data`.
        N)r   r   r8   r:   s     r)   update_bot_datazDictPersistence.update_bot_dataW  s&      >>T!"    "c                 H   K   | j                   |k(  ry|| _         d| _        yw)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   rr   s     r)   update_callback_dataz$DictPersistence.update_callback_datab  s)      $&"#' rt   c                 l   K   | j                   y| j                   j                  |d       d| _        yw)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   )r8   rn   s     r)   drop_chat_datazDictPersistence.drop_chat_dataq  1      ??"GT*#r+   c                 l   K   | j                   y| j                   j                  |d       d| _        yw)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   rx   r   )r8   ri   s     r)   drop_user_datazDictPersistence.drop_user_data~  rz   r+   rB   c                    K   yw)zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
        N )r8   ri   rB   s      r)   refresh_user_dataz!DictPersistence.refresh_user_data          rI   c                    K   yw)zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
        Nr~   )r8   rn   rI   s      r)   refresh_chat_dataz!DictPersistence.refresh_chat_data  r   r   rL   c                    K   yw)zDoes nothing.

        .. versionadded:: 13.6
        .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
        Nr~   )r8   rL   s     r)   refresh_bot_dataz DictPersistence.refresh_bot_data  r   r   c                    K   yw)zvDoes nothing.

        .. versionadded:: 20.0
        .. seealso:: :meth:`telegram.ext.BasePersistence.flush`
        Nr~   rA   s    r)   flushzDictPersistence.flush  r   r   rR   c                     i }| j                         D ]=  \  }}i ||<   |j                         D ]   \  }}|||   t        j                  |      <   " ? t        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
        )itemsr2   rF   )rR   tmphandlerstatesrd   states         r)   rT   z-DictPersistence._encode_conversations_to_json  sm     $&,224 	6OGVCL$lln 6
U05GTZZ_-6	6 zz#rD   json_stringc           	          t        j                  |       }i }|j                         D ]F  \  }}i ||<   |j                         D ])  \  }}|||   t        t        j                  |            <   + H |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
        )r2   r3   r   tuple)r   r   rR   r   r   rd   r   s          r)   r7   z/DictPersistence._decode_conversations_from_json  s{     jj%57"yy{ 	GOGV%'M'"$lln G
UAFg&uTZZ_'=>G	G rD   c                     i }t        j                  |       }|j                         D ]A  \  }}t        |      }i ||<   |j                         D ]  \  }}	 t        |      }|||   |<    C |S # t        $ r |}Y w xY w)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
        )r2   r3   r   intr/   )	r:   r   decoded_datauserrB   int_user_idrd   value_ids	            r)   r.   z0DictPersistence._decode_user_chat_data_from_json  s     02zz$'+113 	.OD)d)K!C'oo/ .
Uc(C ).K %.	. 
 " Cs   A..A<;A<)N r   r   r   r   <   )r?   N)0__name__
__module____qualname____doc__	__slots__r   r&   r4   r-   propertyr%   r   r   rB   r   rI   r   rL   r   r	   rO   r   r
   rR   r   objectrV   rZ   r\   r^   rc   r   rh   rk   rp   rs   rv   ry   r|   r   r   r   r   staticmethodrT   r7   r.   __classcell__)r>   s   @r)   r   r       s	   .`I /3  "$"$!#M$t+M M 	M
 M  M  M M^ 4T#s(^ 34t;   * * * 4T#s(^ 34t;   * * * $sCx.4/   )s ) ) #w~ # # .C . . #tC)9$9:TA # # .C . .(T#tFFN/C*C%D ((T#tFFN/C*C%D ('D$8 ',4 ,	7C 	74D 	7((-(:@4-(	("$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  rD   r   )r   r2   rb   r   typingr   r   r   telegram.extr   r   telegram.ext._utils.typesr	   r
   r   telegram._utils.typesr   r%   r   r~   rD   r)   <module>r      sX   & 6   + + : P P.Bod38nd38nd3PS8n&TU BrD   