
    Yi}J              
           S r SSKrSSKJr  SSKJrJrJrJr  SSK	J
r
Jr  SSKJrJrJr  \(       a  SSKJr   " S S	\
\\\4   \\\4   \\\4   4   5      rg)
z/This module contains the DictPersistence class.    N)deepcopy)TYPE_CHECKINGAnyOptionalcast)BasePersistencePersistenceInput)CDCDataConversationDictConversationKey)JSONDictc                     ^  \ rS rSrSrSr       S6S\\   S\S\S\S	\S
\S\	4U 4S jjjr
\S\\\\\\4   4      4S j5       r\S\4S j5       r\S\\\\\\4   4      4S j5       r\S\4S j5       r\S\\\\4      4S j5       r\S\4S j5       r\S\\   4S j5       r\S\4S j5       r\S\\\\4      4S j5       r\S\4S j5       rS\\\\\4   4   4S jrS\\\\\4   4   4S jrS\\\4   4S jrS\\   4S jrS\S\4S jr S\S\!S\\   SS4S  jr"S!\S"\\\4   SS4S# jr#S$\S"\\\4   SS4S% jr$S"\\\4   SS4S& jr%S"\SS4S' jr&S$\SS4S( jr'S!\SS4S) jr(S!\S*\\\4   SS4S+ jr)S$\S,\\\4   SS4S- jr*S.\\\4   SS4S/ jr+S7S0 jr,\-S1\\\4   S\4S2 j5       r.\-S3\S\\\4   4S4 j5       r/\-S"\S\\\\\4   4   4S5 j5       r0Sr1U =r2$ )8DictPersistence   a	  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U ]  XS9  S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l	        S U l
        S U l        U(       a   U R                  U5      U l        X l        U(       a   U R                  U5      U l        X0l        U(       aL   [         R"                  " U5      U l        X@l	        [%        U R                  [&        5      (       d  [        S5      eU(       a   [         R"                  " U5      n	 U	c  S U l        O@[)        [*        U	S    V
VVs/ s H  u  po[-        U5      U4PM     snnn
U	S   45      U l        X`l
        U R
                  bQ  [1        S
 U R
                  S    5       5      (       a"  [%        U R
                  S   [&        5      (       d  [        S	5      eU(       a   U R3                  U5      U l        XPl        g g ! [        [        4 a  n[        S5      UeS nAff = f! [        [        4 a  n[        S5      UeS nAff = f! [        [        4 a  n[        S5      UeS nAff = f! [        [        4 a  n[        S5      UeS nAff = fs  snnn
f ! [        [.        4 a  n[        S	5      UeS nAff = f! [        [        4 a  n[        S5      UeS nAff = f)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      z0callback_data_json is not in the required formatc              3   |   #    U  H2  n[        US    [        5      =(       a    [        US   [        5      v   M4     g7f)   r   N)
isinstancedictstr).0entrys     H/app/.venv/lib/python3.13/site-packages/telegram/ext/_dictpersistence.py	<genexpr>+DictPersistence.__init__.<locals>.<genexpr>   s5      !7 uQx.L:eAh3LL!7s   :<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   r
   float
IndexErrorall_decode_conversations_from_json)selfr   r   r   r   r   r    r!   excdataonetwothree	__class__s                r+   r/   DictPersistence.__init__]   s    	JP"".2.2-126 26 a"&"G"G"W'5$ a"&"G"G"W'5$ `!%M!:&3# dnnd33 GHHzz"45
]<*.D'*.JNq'R's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   G$ H %!H, 8I I; -I4I; J $H4H  HH)H$$H),I<III1 I,,I14I; ;JJJK /J;;K returnc                     U R                   $ )z%:obj:`dict`: The user_data as a dict.)r   r:   s    r+   	user_dataDictPersistence.user_data            c                 |    U R                   (       a  U R                   $ [        R                  " U R                  5      $ )z6:obj:`str`: The user_data serialized as a JSON-string.)r   r4   dumpsrE   rD   s    r+   r   DictPersistence.user_data_json   ,     '''zz$..))rH   c                     U R                   $ )z%:obj:`dict`: The chat_data as a dict.)r   rD   s    r+   	chat_dataDictPersistence.chat_data   rG   rH   c                 |    U R                   (       a  U R                   $ [        R                  " U R                  5      $ )z6:obj:`str`: The chat_data serialized as a JSON-string.)r   r4   rJ   rN   rD   s    r+   r   DictPersistence.chat_data_json   rL   rH   c                     U R                   $ )z$:obj:`dict`: The bot_data as a dict.)r   rD   s    r+   bot_dataDictPersistence.bot_data   s     ~~rH   c                 |    U R                   (       a  U R                   $ [        R                  " U R                  5      $ )z5:obj:`str`: The bot_data serialized as a JSON-string.)r   r4   rJ   rS   rD   s    r+   r   DictPersistence.bot_data_json   s,     &&&zz$--((rH   c                     U R                   $ )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   rD   s    r+   callback_dataDictPersistence.callback_data   s     """rH   c                 |    U R                   (       a  U R                   $ [        R                  " U R                  5      $ )z_:obj:`str`: The metadata on the stored callback data as a JSON-string.

.. versionadded:: 13.6
)r   r4   rJ   rX   rD   s    r+   r    "DictPersistence.callback_data_json   s.     ##+++zz$,,--rH   c                     U R                   $ )z):obj:`dict`: The conversations as a dict.)r   rD   s    r+   conversationsDictPersistence.conversations   s     """rH   c                     U R                   (       a  U R                   $ U R                  (       a  U R                  U R                  5      $ [        R                  " U R                  5      $ )z::obj:`str`: The conversations serialized as a JSON-string.)r   r]   _encode_conversations_to_jsonr4   rJ   rD   s    r+   r   "DictPersistence.conversations_json   sN     ##+++55d6H6HIIzz$,,--rH   c                 \   #    U R                   c  0 U l        [        U R                   5      $ 7f)zReturns the user_data created from the ``user_data_json`` or an empty :obj:`dict`.

Returns:
    :obj:`dict`: The restored user data.
)rE   r   r   rD   s    r+   get_user_dataDictPersistence.get_user_data   '      >>! DO''   *,c                 \   #    U R                   c  0 U l        [        U R                   5      $ 7f)zReturns the chat_data created from the ``chat_data_json`` or an empty :obj:`dict`.

Returns:
    :obj:`dict`: The restored chat data.
)rN   r   r   rD   s    r+   get_chat_dataDictPersistence.get_chat_data   re   rf   c                 \   #    U R                   c  0 U l        [        U R                   5      $ 7f)zReturns the bot_data created from the ``bot_data_json`` or an empty :obj:`dict`.

Returns:
    :obj:`dict`: The restored bot data.
)rS   r   r   rD   s    r+   get_bot_dataDictPersistence.get_bot_data  s'      == DN&&rf   c                 ^   #    U R                   c  SU l        g[        U R                   5      $ 7f)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)rX   r   r   rD   s    r+   get_callback_data!DictPersistence.get_callback_data  s/      %"&D**++s   +-namec                    #    U R                   c  0 U l        U R                   R                  U0 5      R                  5       $ 7f)zReturns the conversations created from the ``conversations_json`` or an empty
:obj:`dict`.

Returns:
    :obj:`dict`: The restored conversations data.
)r]   r   getcopy)r:   rp   s     r+   get_conversations!DictPersistence.get_conversations  s=      %"$D!!%%dB/4466s   ?Akey	new_statec                    #    U R                   (       d  0 U l         U R                   R                  U0 5      R                  U5      U:X  a  gX0R                   U   U'   SU l        g7f)zWill 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   
setdefaultrr   r   )r:   rp   rv   rw   s       r+   update_conversation#DictPersistence.update_conversation)  s\      """$D))$377<	I)2D!#&#' s   A"A$user_idr<   c                    #    U R                   c  0 U l         U R                   R                  U5      U:X  a  gX R                   U'   SU l        g7f)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   rr   r   )r:   r|   r<   s      r+   update_user_data DictPersistence.update_user_data:  F      ??" DO??w'4/#' #   AAchat_idc                    #    U R                   c  0 U l         U R                   R                  U5      U:X  a  gX R                   U'   SU l        g7f)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   rr   r   )r:   r   r<   s      r+   update_chat_data DictPersistence.update_chat_dataH  r   r   c                 H   #    U R                   U:X  a  gXl         SU l        g7f)zuWill update the bot_data (if changed).

Args:
    data (:obj:`dict`): The :attr:`telegram.ext.Application.bot_data`.
N)r   r   r:   r<   s     r+   update_bot_dataDictPersistence.update_bot_dataV  s#      >>T!"    "c                 H   #    U R                   U:X  a  gXl         SU l        g7f)a#  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   r   s     r+   update_callback_data$DictPersistence.update_callback_dataa  s&      $&"#' r   c                 n   #    U R                   c  gU R                   R                  US5        SU l        g7f)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   )r:   r   s     r+   drop_chat_dataDictPersistence.drop_chat_datap  0      ??"GT*#   35c                 n   #    U R                   c  gU R                   R                  US5        SU l        g7f)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   )r:   r|   s     r+   drop_user_dataDictPersistence.drop_user_data}  r   r   rE   c                    #    g7f)zjDoes nothing.

.. versionadded:: 13.6
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
N )r:   r|   rE   s      r+   refresh_user_data!DictPersistence.refresh_user_data          rN   c                    #    g7f)zjDoes nothing.

.. versionadded:: 13.6
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
Nr   )r:   r   rN   s      r+   refresh_chat_data!DictPersistence.refresh_chat_data  r   r   rS   c                    #    g7f)ziDoes nothing.

.. versionadded:: 13.6
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
Nr   )r:   rS   s     r+   refresh_bot_data DictPersistence.refresh_bot_data  r   r   c                    #    g7f)z^Does nothing.

.. versionadded:: 20.0
.. seealso:: :meth:`telegram.ext.BasePersistence.flush`
Nr   rD   s    r+   flushDictPersistence.flush  r   r   r]   c                     0 nU R                  5        H=  u  p#0 X'   UR                  5        H   u  pEXQU   [        R                  " U5      '   M"     M?     [        R                  " U5      $ )a6  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
)itemsr4   rJ   )r]   tmphandlerstatesrv   states         r+   r`   -DictPersistence._encode_conversations_to_json  s[     $&,224OGCL$lln
05GTZZ_- -  5 zz#rH   json_stringc           	          [         R                  " U 5      n0 nUR                  5        HF  u  p40 X#'   UR                  5        H)  u  pVXbU   [        [         R                  " U5      5      '   M+     MH     U$ )a!  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
)r4   r5   r   tuple)r   r   r]   r   r   rv   r   s          r+   r9   /DictPersistence._decode_conversations_from_json  se     jj%57"yy{OG%'M"$lln
AFg&uTZZ_'=> -  + rH   c                    0 n[         R                  " U 5      nUR                  5        H@  u  p4[        U5      n0 X'   UR                  5        H  u  pg [        U5      nXqU   U'   M     MB     U$ ! [         a    Un Nf = f)zHelper 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
)r4   r5   r   intr1   )	r<   r   decoded_datauserrE   int_user_idrv   value_ids	            r+   r0   0DictPersistence._decode_user_chat_data_from_json  s     02zz$'+113ODd)K!C'oo/
c(C ).K % 0  4 
 " Cs   A//A>=A>)N r   r   r   r   <   )rB   N)3__name__
__module____qualname____firstlineno____doc__	__slots__r   r	   r(   r6   r/   propertyr'   r   r   rE   r   rN   r   rS   r   r
   rX   r    r   r]   r   objectrc   rh   rk   rn   rt   r   rz   r~   r   r   r   r   r   r   r   r   r   staticmethodr`   r9   r0   __static_attributes____classcell__)r@   s   @r+   r   r      s   .`I 26  "$"$!#M-.M M 	M
 M  M  M M M^ 8Dd38n)<$=>   * * * 8Dd38n)<$=>   * * * (4S>2   )s ) ) #x0 # # .C . . #xS2B-B(CD # # .C . .(T#tFFN/C*C%D ((T#tFFN/C*C%D ('D$8 ','): ,	7C 	74D 	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  rH   r   )r   r4   rs   r   typingr   r   r   r   telegram.extr   r	   telegram.ext._utils.typesr
   r   r   telegram._utils.typesr   r'   r   r   rH   r+   <module>r      sX   & 6   5 5 : P P.Bod38nd38nd3PS8n&TU BrH   