
    YiG                         S r SSKJrJr  SSKJrJrJrJr  SSK	J
r
  SSKJr  SSKJrJrJrJrJrJr   " S S\5      r " S	 S
\\\\4   \5      rg)z/This module contains the BasePersistence class.    )ABCabstractmethod)Generic
NamedTupleNoReturnOptional)Bot)ExtBot)BDCDUDCDCDataConversationDictConversationKeyc                   R    \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S'   Srg	)
PersistenceInput   a   Convenience wrapper to group boolean input for the :paramref:`~BasePersistence.store_data`
parameter for :class:`BasePersistence`.

Args:
    bot_data (:obj:`bool`, optional): Whether the setting should be applied for ``bot_data``.
        Defaults to :obj:`True`.
    chat_data (:obj:`bool`, optional): Whether the setting should be applied for ``chat_data``.
        Defaults to :obj:`True`.
    user_data (:obj:`bool`, optional): Whether the setting should be applied for ``user_data``.
        Defaults to :obj:`True`.
    callback_data (:obj:`bool`, optional): Whether the setting should be applied for
        ``callback_data``. Defaults to :obj:`True`.

Attributes:
    bot_data (:obj:`bool`): Whether the setting should be applied for ``bot_data``.
    chat_data (:obj:`bool`): Whether the setting should be applied for ``chat_data``.
    user_data (:obj:`bool`): Whether the setting should be applied for ``user_data``.
    callback_data (:obj:`bool`): Whether the setting should be applied for ``callback_data``.

Tbot_data	chat_data	user_datacallback_data N)__name__
__module____qualname____firstlineno____doc__r   bool__annotations__r   r   r   __static_attributes__r       H/app/.venv/lib/python3.13/site-packages/telegram/ext/_basepersistence.pyr   r      s1    * HdItItM4r!   r   c            	       v   \ rS rSrSrSr  S'S\\   S\4S jjr	\
S\4S	 j5       r\R                  S
\S\4S j5       rS\SS4S jr\S\\\4   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\S\4S j5       r\S\S\S\\   SS4S j5       r \S\S\SS4S j5       r!\S\S\SS4S j5       r"\S\SS4S j5       r#\S\SS4S j5       r$\S\SS4S j5       r%\S\SS4S j5       r&\S\S \SS4S! j5       r'\S\S"\SS4S# j5       r(\S$\SS4S% j5       r)\S(S& j5       r*Sr+g))BasePersistence8   a  Interface class for adding persistence to your bot.
Subclass this object for different implementations of a persistent bot.

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`.

All relevant methods must be overwritten. This includes:

* :meth:`get_bot_data`
* :meth:`update_bot_data`
* :meth:`refresh_bot_data`
* :meth:`get_chat_data`
* :meth:`update_chat_data`
* :meth:`refresh_chat_data`
* :meth:`drop_chat_data`
* :meth:`get_user_data`
* :meth:`update_user_data`
* :meth:`refresh_user_data`
* :meth:`drop_user_data`
* :meth:`get_callback_data`
* :meth:`update_callback_data`
* :meth:`get_conversations`
* :meth:`update_conversation`
* :meth:`flush`

If you don't actually need one of those methods, a simple :keyword:`pass` is enough.
For example, if you don't store ``bot_data``, you don't need :meth:`get_bot_data`,
:meth:`update_bot_data` or :meth:`refresh_bot_data`.

Note:
   You should avoid saving :class:`telegram.Bot` instances. This is because if you change e.g.
   the bots token, this won't propagate to the serialized instances and may lead to exceptions.

   To prevent this, the implementation may use :attr:`bot` to replace bot instances with a
   placeholder before serialization and insert :attr:`bot` back when loading the data.
   Since :attr:`bot` will be set when the process starts, this will be the up-to-date bot
   instance.

   If the persistence implementation does not take care of this, you should make sure not to
   store any bot instances in the data that will be persisted. E.g. in case of
   :class:`telegram.TelegramObject`, one may call :meth:`set_bot` to ensure that shortcuts like
   :meth:`telegram.Message.reply_text` are available.

This class is a :class:`~typing.Generic` class and accepts three type variables:

1. The type of the second argument of :meth:`update_user_data`, which must coincide with the
   type of the second argument of :meth:`refresh_user_data` and the values in the dictionary
   returned by :meth:`get_user_data`.
2. The type of the second argument of :meth:`update_chat_data`, which must coincide with the
   type of the second argument of :meth:`refresh_chat_data` and the values in the dictionary
   returned by :meth:`get_chat_data`.
3. The type of the argument of :meth:`update_bot_data`, which must coincide with the
   type of the argument of :meth:`refresh_bot_data` and the return value of
   :meth:`get_bot_data`.

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

.. versionchanged:: 20.0

    * The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`.
    * ``insert/replace_bot`` was dropped. Serialization of bot instances now needs to be
      handled by the specific implementation - see above note.

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.
    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 (:class:`telegram.Bot`): The bot associated with the persistence.
)_update_intervalbot
store_dataNr(   update_intervalc                 N    U=(       d
    [        5       U l        X l        S U l        g )N)r   r(   r&   r'   )selfr(   r)   s      r"   __init__BasePersistence.__init__   s!    
 -7,L:J:L'6r!   returnc                     U R                   $ )z:obj:`float`: Time (in seconds) that the :class:`~telegram.ext.Application`
will wait between two consecutive runs of updating the persistence.

.. versionadded:: 20.0
)r&   r+   s    r"   r)   BasePersistence.update_interval   s     $$$r!   _c                     [        S5      e)NzGYou can not assign a new value to update_interval after initialization.)AttributeError)r+   r2   s     r"   r)   r1      s    U
 	
r!   r'   c                     U R                   R                  (       a   [        U[        5      (       d  [	        S5      eXl        g)a  Set the Bot to be used by this persistence instance.

Args:
    bot (:class:`telegram.Bot`): The bot.

Raises:
    :exc:`TypeError`: If :attr:`PersistenceInput.callback_data` is :obj:`True` and the
        :paramref:`bot` is not an instance of :class:`telegram.ext.ExtBot`.
z@callback_data can only be stored when using telegram.ext.ExtBot.N)r(   r   
isinstancer
   	TypeErrorr'   )r+   r'   s     r"   set_botBasePersistence.set_bot   s.     ??((*S&2I2I^__r!   c                    #    g7f)an  Will be called by :class:`telegram.ext.Application` upon creation with a
persistence object. It should return the ``user_data`` if stored, or an empty
:obj:`dict`. In the latter case, the dictionary should produce values
corresponding to one of the following:

- :obj:`dict`
- The type from :attr:`telegram.ext.ContextTypes.user_data`
  if :class:`telegram.ext.ContextTypes` is used.

.. versionchanged:: 20.0
    This method may now return a :obj:`dict` instead of a :obj:`collections.defaultdict`

Returns:
    dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`]:
        The restored user data.
Nr   r0   s    r"   get_user_dataBasePersistence.get_user_data           c                    #    g7f)an  Will be called by :class:`telegram.ext.Application` upon creation with a
persistence object. It should return the ``chat_data`` if stored, or an empty
:obj:`dict`. In the latter case, the dictionary should produce values
corresponding to one of the following:

- :obj:`dict`
- The type from :attr:`telegram.ext.ContextTypes.chat_data`
  if :class:`telegram.ext.ContextTypes` is used.

.. versionchanged:: 20.0
    This method may now return a :obj:`dict` instead of a :obj:`collections.defaultdict`

Returns:
    dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`]:
        The restored chat data.
Nr   r0   s    r"   get_chat_dataBasePersistence.get_chat_data   r=   r>   c                    #    g7f)a  Will be called by :class:`telegram.ext.Application` upon creation with a
persistence object. It should return the ``bot_data`` if stored, or an empty
:obj:`dict`. In the latter case, the :obj:`dict` should produce values
corresponding to one of the following:

- :obj:`dict`
- The type from :attr:`telegram.ext.ContextTypes.bot_data`
  if :class:`telegram.ext.ContextTypes` are used.

Returns:
    dict[:obj:`int`, :obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`]:
        The restored bot data.
Nr   r0   s    r"   get_bot_dataBasePersistence.get_bot_data   r=   r>   c                    #    g7f)a  Will be called by :class:`telegram.ext.Application` upon creation with a
persistence object. If callback data was stored, it should be returned.

.. versionadded:: 13.6

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.

Returns:
    tuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]],
    dict[:obj:`str`, :obj:`str`]] | :obj:`None`: The restored metadata or :obj:`None`,
    if no data was stored.
Nr   r0   s    r"   get_callback_data!BasePersistence.get_callback_data   r=   r>   namec                    #    g7f)a  Will be called by :class:`telegram.ext.Application` when a
:class:`telegram.ext.ConversationHandler` is added if
:attr:`telegram.ext.ConversationHandler.persistent` is :obj:`True`.
It should return the conversations for the handler with :paramref:`name` or an empty
:obj:`dict`.

Args:
    name (:obj:`str`): The handlers name.

Returns:
    :obj:`dict`: The restored conversations for the handler.
Nr   )r+   rH   s     r"   get_conversations!BasePersistence.get_conversations   r=   r>   key	new_statec                    #    g7f)a;  Will be called when a :class:`telegram.ext.ConversationHandler` changes states.
This allows the storage of the new state in the persistence.

Args:
    name (:obj:`str`): The handler's name.
    key (:obj:`tuple`): The key the state is changed for.
    new_state (:class:`object`): The new state for the given key.
Nr   )r+   rH   rL   rM   s       r"   update_conversation#BasePersistence.update_conversation  r=   r>   user_iddatac                    #    g7f)a;  Will be called by the :class:`telegram.ext.Application` after a handler has
handled an update.

Args:
    user_id (:obj:`int`): The user the data might have been changed for.
    data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`):
        The :attr:`telegram.ext.Application.user_data` ``[user_id]``.
Nr   )r+   rQ   rR   s      r"   update_user_data BasePersistence.update_user_data  r=   r>   chat_idc                    #    g7f)a;  Will be called by the :class:`telegram.ext.Application` after a handler has
handled an update.

Args:
    chat_id (:obj:`int`): The chat the data might have been changed for.
    data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`):
        The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``.
Nr   )r+   rV   rR   s      r"   update_chat_data BasePersistence.update_chat_data'  r=   r>   c                    #    g7f)zWill be called by the :class:`telegram.ext.Application` after a handler has
handled an update.

Args:
    data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`):
        The :attr:`telegram.ext.Application.bot_data`.
Nr   r+   rR   s     r"   update_bot_dataBasePersistence.update_bot_data2  r=   r>   c                    #    g7f)a  Will be called by the :class:`telegram.ext.Application` after a handler has
handled an update.

.. versionadded:: 13.6

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.

Args:
    data (tuple[list[tuple[:obj:`str`, :obj:`float`,                 dict[:obj:`str`, :obj:`Any`]]], dict[:obj:`str`, :obj:`str`]] | :obj:`None`):
        The relevant data to restore :class:`telegram.ext.CallbackDataCache`.
Nr   r[   s     r"   update_callback_data$BasePersistence.update_callback_data<  r=   r>   c                    #    g7f)zWill be called by the :class:`telegram.ext.Application`, when using
:meth:`~telegram.ext.Application.drop_chat_data`.

.. versionadded:: 20.0

Args:
    chat_id (:obj:`int`): The chat id to delete from the persistence.
Nr   )r+   rV   s     r"   drop_chat_dataBasePersistence.drop_chat_dataL  r=   r>   c                    #    g7f)zWill be called by the :class:`telegram.ext.Application`, when using
:meth:`~telegram.ext.Application.drop_user_data`.

.. versionadded:: 20.0

Args:
    user_id (:obj:`int`): The user id to delete from the persistence.
Nr   )r+   rQ   s     r"   drop_user_dataBasePersistence.drop_user_dataW  r=   r>   r   c                    #    g7f)a  Will be called by the :class:`telegram.ext.Application` before passing the
:attr:`~telegram.ext.Application.user_data` to a callback. Can be used to update data
stored in :attr:`~telegram.ext.Application.user_data` from an external source.

Tip:
    This method is expected to edit the object :paramref:`user_data` in-place instead of
    returning a new object.

Warning:
    When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
    may be called while a handler callback is still running. This might lead to race
    conditions.

.. versionadded:: 13.6

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.

Args:
    user_id (:obj:`int`): The user ID this :attr:`~telegram.ext.Application.user_data` is
        associated with.
    user_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.user_data`):
        The ``user_data`` of a single user.
Nr   )r+   rQ   r   s      r"   refresh_user_data!BasePersistence.refresh_user_datab  r=   r>   r   c                    #    g7f)a  Will be called by the :class:`telegram.ext.Application` before passing the
:attr:`~telegram.ext.Application.chat_data` to a callback. Can be used to update data
stored in :attr:`~telegram.ext.Application.chat_data` from an external source.

Tip:
    This method is expected to edit the object :paramref:`chat_data` in-place instead of
    returning a new object.

Warning:
    When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
    may be called while a handler callback is still running. This might lead to race
    conditions.

.. versionadded:: 13.6

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.

Args:
    chat_id (:obj:`int`): The chat ID this :attr:`~telegram.ext.Application.chat_data` is
        associated with.
    chat_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.chat_data`):
        The ``chat_data`` of a single chat.
Nr   )r+   rV   r   s      r"   refresh_chat_data!BasePersistence.refresh_chat_data}  r=   r>   r   c                    #    g7f)a  Will be called by the :class:`telegram.ext.Application` before passing the
:attr:`~telegram.ext.Application.bot_data` to a callback. Can be used to update data stored
in :attr:`~telegram.ext.Application.bot_data` from an external source.

Tip:
    This method is expected to edit the object :paramref:`bot_data` in-place instead of
    returning a new object.

Warning:
    When using :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, this method
    may be called while a handler callback is still running. This might lead to race
    conditions.

.. versionadded:: 13.6

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.

Args:
    bot_data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`):
        The ``bot_data``.
Nr   )r+   r   s     r"   refresh_bot_data BasePersistence.refresh_bot_data  r=   r>   c                    #    g7f)zWill be called by :meth:`telegram.ext.Application.stop`. Gives the
persistence a chance to finish up saving or close a database connection gracefully.

.. versionchanged:: 20.0
   Changed this method into an :external:func:`~abc.abstractmethod`.
Nr   r0   s    r"   flushBasePersistence.flush  r=   r>   )N<   )r.   N),r   r   r   r   r   	__slots__r   r   floatr,   propertyr)   setterobjectr   r	   r8   r   dictintr   r;   r   r@   r   rC   r   rF   strr   rJ   r   rO   rT   rX   r\   r_   rb   re   rh   rk   rn   rq   r    r   r!   r"   r$   r$   8   s   QfI 26!#-.  % % % 
 
H 
 

3 4  T#r']  $ T#r']  $ B   '):   C 4D   

-
:B6:J
	
 
 c     c     "    w 4   C D   C D   s r d  4 s r d  4 r d  0  r!   r$   N)r   abcr   r   typingr   r   r   r   telegram._botr	   telegram.ext._extbotr
   telegram.ext._utils.typesr   r   r   r   r   r   r   r$   r   r!   r"   <module>r      sF   & 6 # : :  ' \ \z 8@gb"bj)3 @r!   