2018-09-20 22:50:40 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2018-09-20 22:50:40 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains the PicklePersistence class."""
|
|
|
|
import pickle
|
2022-03-12 12:27:18 +01:00
|
|
|
from copy import deepcopy
|
2021-10-05 19:50:11 +02:00
|
|
|
from pathlib import Path
|
2023-02-02 18:55:07 +01:00
|
|
|
from typing import Any, Callable, Dict, Optional, Set, Tuple, Type, TypeVar, Union, cast, overload
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
from telegram import Bot, TelegramObject
|
2021-10-11 20:24:52 +02:00
|
|
|
from telegram._utils.types import FilePathInput
|
2022-03-12 12:27:18 +01:00
|
|
|
from telegram._utils.warnings import warn
|
2021-08-13 16:18:42 +02:00
|
|
|
from telegram.ext import BasePersistence, PersistenceInput
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram.ext._contexttypes import ContextTypes
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.ext._utils.types import BD, CD, UD, CDCData, ConversationDict, ConversationKey
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
_REPLACED_KNOWN_BOT = "a known bot replaced by PTB's PicklePersistence"
|
|
|
|
_REPLACED_UNKNOWN_BOT = "an unknown bot replaced by PTB's PicklePersistence"
|
|
|
|
|
2022-09-17 15:08:54 +02:00
|
|
|
TelegramObj = TypeVar("TelegramObj", bound=TelegramObject)
|
2022-03-12 12:27:18 +01:00
|
|
|
|
|
|
|
|
2022-09-17 15:08:54 +02:00
|
|
|
def _all_subclasses(cls: Type[TelegramObj]) -> Set[Type[TelegramObj]]:
|
2022-03-12 12:27:18 +01:00
|
|
|
"""Gets all subclasses of the specified object, recursively. from
|
|
|
|
https://stackoverflow.com/a/3862957/9706202
|
|
|
|
"""
|
|
|
|
subclasses = cls.__subclasses__()
|
|
|
|
return set(subclasses).union([s for c in subclasses for s in _all_subclasses(c)])
|
|
|
|
|
|
|
|
|
2022-09-17 15:08:54 +02:00
|
|
|
def _reconstruct_to(cls: Type[TelegramObj], kwargs: dict) -> TelegramObj:
|
2022-03-12 12:27:18 +01:00
|
|
|
"""
|
|
|
|
This method is used for unpickling. The data, which is in the form a dictionary, is
|
|
|
|
converted back into a class. Works mostly the same as :meth:`TelegramObject.__setstate__`.
|
|
|
|
This function should be kept in place for backwards compatibility even if the pickling logic
|
|
|
|
is changed, since `_custom_reduction` places references to this function into the pickled data.
|
|
|
|
"""
|
|
|
|
obj = cls.__new__(cls)
|
|
|
|
obj.__setstate__(kwargs)
|
2022-10-07 11:51:53 +02:00
|
|
|
return obj
|
2022-03-12 12:27:18 +01:00
|
|
|
|
|
|
|
|
2022-09-17 15:08:54 +02:00
|
|
|
def _custom_reduction(cls: TelegramObj) -> Tuple[Callable, Tuple[Type[TelegramObj], dict]]:
|
2022-03-12 12:27:18 +01:00
|
|
|
"""
|
|
|
|
This method is used for pickling. The bot attribute is preserved so _BotPickler().persistent_id
|
|
|
|
works as intended.
|
|
|
|
"""
|
|
|
|
data = cls._get_attrs(include_private=True) # pylint: disable=protected-access
|
2022-12-15 15:00:36 +01:00
|
|
|
# MappingProxyType is not pickable, so we convert it to a dict
|
|
|
|
# no need to convert back to MPT in _reconstruct_to, since it's done in __setstate__
|
|
|
|
data["api_kwargs"] = dict(data["api_kwargs"]) # type: ignore[arg-type]
|
2022-03-12 12:27:18 +01:00
|
|
|
return _reconstruct_to, (cls.__class__, data)
|
|
|
|
|
|
|
|
|
|
|
|
class _BotPickler(pickle.Pickler):
|
|
|
|
__slots__ = ("_bot",)
|
|
|
|
|
|
|
|
def __init__(self, bot: Bot, *args: Any, **kwargs: Any):
|
|
|
|
self._bot = bot
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2022-09-17 15:08:54 +02:00
|
|
|
def reducer_override( # skipcq: PYL-R0201
|
|
|
|
self, obj: TelegramObj
|
|
|
|
) -> Tuple[Callable, Tuple[Type[TelegramObj], dict]]:
|
2023-06-29 18:17:47 +02:00
|
|
|
"""
|
|
|
|
This method is used for pickling. The bot attribute is preserved so
|
|
|
|
_BotPickler().persistent_id works as intended.
|
|
|
|
"""
|
2022-03-12 12:27:18 +01:00
|
|
|
if not isinstance(obj, TelegramObject):
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
return _custom_reduction(obj)
|
|
|
|
|
|
|
|
def persistent_id(self, obj: object) -> Optional[str]:
|
|
|
|
"""Used to 'mark' the Bot, so it can be replaced later. See
|
|
|
|
https://docs.python.org/3/library/pickle.html#pickle.Pickler.persistent_id for more info
|
|
|
|
"""
|
|
|
|
if obj is self._bot:
|
|
|
|
return _REPLACED_KNOWN_BOT
|
|
|
|
if isinstance(obj, Bot):
|
2023-04-27 22:36:04 +02:00
|
|
|
warn(
|
|
|
|
"Unknown bot instance found. Will be replaced by `None` during unpickling",
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
2022-03-12 12:27:18 +01:00
|
|
|
return _REPLACED_UNKNOWN_BOT
|
|
|
|
return None # pickles as usual
|
|
|
|
|
|
|
|
|
|
|
|
class _BotUnpickler(pickle.Unpickler):
|
|
|
|
__slots__ = ("_bot",)
|
|
|
|
|
|
|
|
def __init__(self, bot: Bot, *args: Any, **kwargs: Any):
|
|
|
|
self._bot = bot
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def persistent_load(self, pid: str) -> Optional[Bot]:
|
|
|
|
"""Replaces the bot with the current bot if known, else it is replaced by :obj:`None`."""
|
|
|
|
if pid == _REPLACED_KNOWN_BOT:
|
|
|
|
return self._bot
|
|
|
|
if pid == _REPLACED_UNKNOWN_BOT:
|
|
|
|
return None
|
|
|
|
raise pickle.UnpicklingError("Found unknown persistent id when unpickling!")
|
|
|
|
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
class PicklePersistence(BasePersistence[UD, CD, BD]):
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Using python's builtin :mod:`pickle` for making your bot persistent.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
Attention:
|
|
|
|
The interface provided by this class is intended to be accessed exclusively by
|
2022-04-24 12:38:09 +02:00
|
|
|
:class:`~telegram.ext.Application`. Calling any of the methods below manually might
|
|
|
|
interfere with the integration of persistence into :class:`~telegram.ext.Application`.
|
2022-02-09 17:30:16 +01:00
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
Note:
|
|
|
|
This implementation of :class:`BasePersistence` uses the functionality of the pickle module
|
|
|
|
to support serialization of bot instances. Specifically any reference to
|
|
|
|
:attr:`~BasePersistence.bot` will be replaced by a placeholder before pickling and
|
|
|
|
:attr:`~BasePersistence.bot` will be inserted back when loading the data.
|
2020-07-13 21:52:26 +02:00
|
|
|
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
Examples:
|
|
|
|
:any:`Persistent Conversation Bot <examples.persistentconversationbot>`
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Making Your Bot Persistent <Making-your-bot-persistent>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-10-19 18:28:19 +02:00
|
|
|
|
2021-10-05 19:50:11 +02:00
|
|
|
* The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`.
|
|
|
|
* The parameter and attribute ``filename`` were replaced by :attr:`filepath`.
|
|
|
|
* :attr:`filepath` now also accepts :obj:`pathlib.Path` as argument.
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Args:
|
2021-10-05 19:50:11 +02:00
|
|
|
filepath (:obj:`str` | :obj:`pathlib.Path`): The filepath for storing the pickle files.
|
|
|
|
When :attr:`single_file` is :obj:`False` this will be used as a prefix.
|
2023-02-05 18:09:55 +01:00
|
|
|
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.
|
2021-06-06 11:48:48 +02:00
|
|
|
single_file (:obj:`bool`, optional): When :obj:`False` will store 5 separate files of
|
2021-07-01 17:34:23 +02:00
|
|
|
`filename_user_data`, `filename_bot_data`, `filename_chat_data`,
|
2021-06-06 11:48:48 +02:00
|
|
|
`filename_callback_data` and `filename_conversations`. Default is :obj:`True`.
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
on_flush (:obj:`bool`, optional): When :obj:`True` will only save to file when
|
|
|
|
:meth:`flush` is called and keep data in memory until that happens. When
|
|
|
|
:obj:`False` will store data on any transaction *and* on call to :meth:`flush`.
|
|
|
|
Default is :obj:`False`.
|
2021-06-06 10:37:53 +02:00
|
|
|
context_types (:class:`telegram.ext.ContextTypes`, optional): Pass an instance
|
|
|
|
of :class:`telegram.ext.ContextTypes` to customize the types used in the
|
|
|
|
``context`` interface. If not passed, the defaults documented in
|
|
|
|
:class:`telegram.ext.ContextTypes` will be used.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
2022-04-24 12:38:09 +02:00
|
|
|
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.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2021-10-05 19:50:11 +02:00
|
|
|
filepath (:obj:`str` | :obj:`pathlib.Path`): The filepath for storing the pickle files.
|
|
|
|
When :attr:`single_file` is :obj:`False` this will be used as a prefix.
|
2023-02-05 18:09:55 +01:00
|
|
|
store_data (:class:`~telegram.ext.PersistenceInput`): Specifies which kinds of data will
|
|
|
|
be saved by this persistence instance.
|
2021-06-06 11:48:48 +02:00
|
|
|
single_file (:obj:`bool`): Optional. When :obj:`False` will store 5 separate files of
|
2021-07-01 17:34:23 +02:00
|
|
|
`filename_user_data`, `filename_bot_data`, `filename_chat_data`,
|
2021-06-06 11:48:48 +02:00
|
|
|
`filename_callback_data` and `filename_conversations`. Default is :obj:`True`.
|
2022-11-22 10:39:20 +01:00
|
|
|
on_flush (:obj:`bool`): Optional. When :obj:`True` will only save to file when
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
:meth:`flush` is called and keep data in memory until that happens. When
|
|
|
|
:obj:`False` will store data on any transaction *and* on call to :meth:`flush`.
|
|
|
|
Default is :obj:`False`.
|
2021-06-06 10:37:53 +02:00
|
|
|
context_types (:class:`telegram.ext.ContextTypes`): Container for the types used
|
|
|
|
in the ``context`` interface.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
2021-10-05 19:50:11 +02:00
|
|
|
"filepath",
|
2021-05-29 16:18:16 +02:00
|
|
|
"single_file",
|
|
|
|
"on_flush",
|
|
|
|
"user_data",
|
|
|
|
"chat_data",
|
|
|
|
"bot_data",
|
2021-06-06 11:48:48 +02:00
|
|
|
"callback_data",
|
2021-05-29 16:18:16 +02:00
|
|
|
"conversations",
|
2021-06-06 10:37:53 +02:00
|
|
|
"context_types",
|
2021-05-29 16:18:16 +02:00
|
|
|
)
|
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
@overload
|
|
|
|
def __init__(
|
2023-02-02 18:55:07 +01:00
|
|
|
self: "PicklePersistence[Dict[Any, Any], Dict[Any, Any], Dict[Any, Any]]",
|
2021-10-11 20:24:52 +02:00
|
|
|
filepath: FilePathInput,
|
2023-05-18 07:57:59 +02:00
|
|
|
store_data: Optional[PersistenceInput] = None,
|
2021-06-06 10:37:53 +02:00
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval: float = 60,
|
2021-06-06 10:37:53 +02:00
|
|
|
):
|
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def __init__(
|
|
|
|
self: "PicklePersistence[UD, CD, BD]",
|
2021-10-11 20:24:52 +02:00
|
|
|
filepath: FilePathInput,
|
2023-05-18 07:57:59 +02:00
|
|
|
store_data: Optional[PersistenceInput] = None,
|
2021-06-06 10:37:53 +02:00
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval: float = 60,
|
2023-05-18 07:57:59 +02:00
|
|
|
context_types: Optional[ContextTypes[Any, UD, CD, BD]] = None,
|
2021-06-06 10:37:53 +02:00
|
|
|
):
|
|
|
|
...
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-10-11 20:24:52 +02:00
|
|
|
filepath: FilePathInput,
|
2023-05-18 07:57:59 +02:00
|
|
|
store_data: Optional[PersistenceInput] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
2022-04-24 12:38:09 +02:00
|
|
|
update_interval: float = 60,
|
2023-05-18 07:57:59 +02:00
|
|
|
context_types: Optional[ContextTypes[Any, UD, CD, BD]] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
):
|
2022-04-24 12:38:09 +02:00
|
|
|
super().__init__(store_data=store_data, update_interval=update_interval)
|
2023-02-02 18:55:07 +01:00
|
|
|
self.filepath: Path = Path(filepath)
|
|
|
|
self.single_file: Optional[bool] = single_file
|
|
|
|
self.on_flush: Optional[bool] = on_flush
|
2022-02-01 17:50:17 +01:00
|
|
|
self.user_data: Optional[Dict[int, UD]] = None
|
|
|
|
self.chat_data: Optional[Dict[int, CD]] = None
|
2021-06-06 10:37:53 +02:00
|
|
|
self.bot_data: Optional[BD] = None
|
2021-06-06 11:48:48 +02:00
|
|
|
self.callback_data: Optional[CDCData] = None
|
2023-02-02 18:55:07 +01:00
|
|
|
self.conversations: Optional[Dict[str, Dict[Tuple[Union[int, str], ...], object]]] = None
|
|
|
|
self.context_types: ContextTypes[Any, UD, CD, BD] = cast(
|
|
|
|
ContextTypes[Any, UD, CD, BD], context_types or ContextTypes()
|
|
|
|
)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-05-27 09:38:17 +02:00
|
|
|
def _load_singlefile(self) -> None:
|
2018-09-20 22:50:40 +02:00
|
|
|
try:
|
2021-10-05 19:50:11 +02:00
|
|
|
with self.filepath.open("rb") as file:
|
2022-03-12 12:27:18 +01:00
|
|
|
data = _BotUnpickler(self.bot, file).load()
|
|
|
|
|
2022-02-01 17:50:17 +01:00
|
|
|
self.user_data = data["user_data"]
|
|
|
|
self.chat_data = data["chat_data"]
|
2021-10-05 19:50:11 +02:00
|
|
|
# For backwards compatibility with files not containing bot data
|
|
|
|
self.bot_data = data.get("bot_data", self.context_types.bot_data())
|
|
|
|
self.callback_data = data.get("callback_data", {})
|
|
|
|
self.conversations = data["conversations"]
|
2021-01-17 23:24:20 +01:00
|
|
|
except OSError:
|
2021-04-05 13:25:27 +02:00
|
|
|
self.conversations = {}
|
2022-02-01 17:50:17 +01:00
|
|
|
self.user_data = {}
|
|
|
|
self.chat_data = {}
|
2021-06-06 10:37:53 +02:00
|
|
|
self.bot_data = self.context_types.bot_data()
|
2021-06-06 11:48:48 +02:00
|
|
|
self.callback_data = None
|
2020-10-31 16:33:34 +01:00
|
|
|
except pickle.UnpicklingError as exc:
|
2021-10-05 19:50:11 +02:00
|
|
|
filename = self.filepath.name
|
2020-11-23 22:09:29 +01:00
|
|
|
raise TypeError(f"File {filename} does not contain valid pickle data") from exc
|
2020-10-31 16:33:34 +01:00
|
|
|
except Exception as exc:
|
2021-10-05 19:50:11 +02:00
|
|
|
raise TypeError(f"Something went wrong unpickling {self.filepath.name}") from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
def _load_file(self, filepath: Path) -> Any:
|
2018-09-20 22:50:40 +02:00
|
|
|
try:
|
2021-10-05 19:50:11 +02:00
|
|
|
with filepath.open("rb") as file:
|
2022-03-12 12:27:18 +01:00
|
|
|
return _BotUnpickler(self.bot, file).load()
|
|
|
|
|
2021-01-17 23:24:20 +01:00
|
|
|
except OSError:
|
2018-09-20 22:50:40 +02:00
|
|
|
return None
|
2020-10-31 16:33:34 +01:00
|
|
|
except pickle.UnpicklingError as exc:
|
2021-10-05 19:50:11 +02:00
|
|
|
raise TypeError(f"File {filepath.name} does not contain valid pickle data") from exc
|
2020-10-31 16:33:34 +01:00
|
|
|
except Exception as exc:
|
2021-10-05 19:50:11 +02:00
|
|
|
raise TypeError(f"Something went wrong unpickling {filepath.name}") from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-05-27 09:38:17 +02:00
|
|
|
def _dump_singlefile(self) -> None:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = {
|
|
|
|
"conversations": self.conversations,
|
|
|
|
"user_data": self.user_data,
|
|
|
|
"chat_data": self.chat_data,
|
|
|
|
"bot_data": self.bot_data,
|
|
|
|
"callback_data": self.callback_data,
|
|
|
|
}
|
|
|
|
with self.filepath.open("wb") as file:
|
2022-03-12 12:27:18 +01:00
|
|
|
_BotPickler(self.bot, file, protocol=pickle.HIGHEST_PROTOCOL).dump(data)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-03-12 12:27:18 +01:00
|
|
|
def _dump_file(self, filepath: Path, data: object) -> None:
|
2021-10-05 19:50:11 +02:00
|
|
|
with filepath.open("wb") as file:
|
2022-03-12 12:27:18 +01:00
|
|
|
_BotPickler(self.bot, file, protocol=pickle.HIGHEST_PROTOCOL).dump(data)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_user_data(self) -> Dict[int, UD]:
|
2022-02-01 17:50:17 +01:00
|
|
|
"""Returns the user_data from the pickle file if it exists or an empty :obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2022-02-01 17:50:17 +01:00
|
|
|
Dict[:obj:`int`, :obj:`dict`]: The restored user data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
if self.user_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = self._load_file(Path(f"{self.filepath}_user_data"))
|
2018-09-20 22:50:40 +02:00
|
|
|
if not data:
|
2022-02-01 17:50:17 +01:00
|
|
|
data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
self.user_data = data
|
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._load_singlefile()
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.user_data) # type: ignore[arg-type]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_chat_data(self) -> Dict[int, CD]:
|
2022-02-01 17:50:17 +01:00
|
|
|
"""Returns the chat_data from the pickle file if it exists or an empty :obj:`dict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2022-02-01 17:50:17 +01:00
|
|
|
Dict[:obj:`int`, :obj:`dict`]: The restored chat data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
if self.chat_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = self._load_file(Path(f"{self.filepath}_chat_data"))
|
2018-09-20 22:50:40 +02:00
|
|
|
if not data:
|
2022-02-01 17:50:17 +01:00
|
|
|
data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
self.chat_data = data
|
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._load_singlefile()
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.chat_data) # type: ignore[arg-type]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_bot_data(self) -> BD:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Returns the bot_data from the pickle file if it exists or an empty object of type
|
2021-10-10 15:10:21 +02:00
|
|
|
:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`.
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
Returns:
|
2021-10-10 15:10:21 +02:00
|
|
|
:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`: The restored bot data.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
if self.bot_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = self._load_file(Path(f"{self.filepath}_bot_data"))
|
2020-02-02 22:20:31 +01:00
|
|
|
if not data:
|
2021-06-06 10:37:53 +02:00
|
|
|
data = self.context_types.bot_data()
|
2020-02-02 22:20:31 +01:00
|
|
|
self.bot_data = data
|
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._load_singlefile()
|
2022-03-12 12:27:18 +01:00
|
|
|
return deepcopy(self.bot_data) # type: ignore[return-value]
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_callback_data(self) -> Optional[CDCData]:
|
2021-06-06 11:48:48 +02:00
|
|
|
"""Returns the callback data from the pickle file if it exists or :obj:`None`.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
|
|
|
Returns:
|
2022-06-09 17:08:54 +02:00
|
|
|
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.
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
if self.callback_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = self._load_file(Path(f"{self.filepath}_callback_data"))
|
2021-06-06 11:48:48 +02:00
|
|
|
if not data:
|
|
|
|
data = None
|
|
|
|
self.callback_data = data
|
|
|
|
else:
|
|
|
|
self._load_singlefile()
|
|
|
|
if self.callback_data is None:
|
|
|
|
return None
|
2022-04-24 12:38:09 +02:00
|
|
|
return deepcopy(self.callback_data)
|
2021-06-06 11:48:48 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def get_conversations(self, name: str) -> ConversationDict:
|
2021-06-06 12:16:23 +02:00
|
|
|
"""Returns the conversations from the pickle file if it exists or an empty dict.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name (:obj:`str`): The handlers name.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`dict`: The restored conversations for the handler.
|
|
|
|
"""
|
|
|
|
if self.conversations:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
data = self._load_file(Path(f"{self.filepath}_conversations"))
|
2018-09-20 22:50:40 +02:00
|
|
|
if not data:
|
|
|
|
data = {name: {}}
|
|
|
|
self.conversations = data
|
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._load_singlefile()
|
2020-10-06 19:28:40 +02:00
|
|
|
return self.conversations.get(name, {}).copy() # type: ignore[union-attr]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_conversation(
|
|
|
|
self, name: str, key: ConversationKey, new_state: Optional[object]
|
2020-10-06 19:28:40 +02:00
|
|
|
) -> None:
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Will update the conversations for the given handler and depending on :attr:`on_flush`
|
|
|
|
save the pickle file.
|
|
|
|
|
|
|
|
Args:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
name (:obj:`str`): The handler's name.
|
2018-09-20 22:50:40 +02:00
|
|
|
key (:obj:`tuple`): The key the state is changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
new_state (:class:`object`): The new state for the given key.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-10-06 19:28:40 +02:00
|
|
|
if not self.conversations:
|
2021-04-05 13:25:27 +02:00
|
|
|
self.conversations = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self.conversations.setdefault(name, {}).get(key) == new_state:
|
|
|
|
return
|
|
|
|
self.conversations[name][key] = new_state
|
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_conversations"), self.conversations)
|
2018-09-20 22:50:40 +02:00
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_singlefile()
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_user_data(self, user_id: int, data: UD) -> None:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
"""Will update the user_data and depending on :attr:`on_flush` save the pickle file.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user the data might have been changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
data (:obj:`dict`): The :attr:`telegram.ext.Application.user_data` ``[user_id]``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-04-10 13:23:13 +02:00
|
|
|
if self.user_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self.user_data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self.user_data.get(user_id) == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self.user_data[user_id] = data
|
2018-09-20 22:50:40 +02:00
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_user_data"), self.user_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_singlefile()
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_chat_data(self, chat_id: int, data: CD) -> None:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
"""Will update the chat_data and depending on :attr:`on_flush` save the pickle file.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id (:obj:`int`): The chat the data might have been changed for.
|
2022-04-24 12:38:09 +02:00
|
|
|
data (:obj:`dict`): The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
2020-04-10 13:23:13 +02:00
|
|
|
if self.chat_data is None:
|
2022-02-01 17:50:17 +01:00
|
|
|
self.chat_data = {}
|
2018-09-20 22:50:40 +02:00
|
|
|
if self.chat_data.get(chat_id) == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self.chat_data[chat_id] = data
|
2018-09-20 22:50:40 +02:00
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_chat_data"), self.chat_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_singlefile()
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_bot_data(self, data: BD) -> None:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
"""Will update the bot_data and depending on :attr:`on_flush` save the pickle file.
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
Args:
|
2021-10-10 15:10:21 +02:00
|
|
|
data (:obj:`dict` | :attr:`telegram.ext.ContextTypes.bot_data`): The
|
2022-04-24 12:38:09 +02:00
|
|
|
:attr:`telegram.ext.Application.bot_data`.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
if self.bot_data == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self.bot_data = data
|
2020-02-02 22:20:31 +01:00
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_bot_data"), self.bot_data)
|
2020-02-02 22:20:31 +01:00
|
|
|
else:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_singlefile()
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def update_callback_data(self, data: CDCData) -> None:
|
2021-06-06 11:48:48 +02:00
|
|
|
"""Will update the callback_data (if changed) and depending on :attr:`on_flush` save the
|
|
|
|
pickle file.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
|
|
|
|
Args:
|
2021-10-10 15:10:21 +02:00
|
|
|
data (Tuple[List[Tuple[:obj:`str`, :obj:`float`, \
|
2022-02-09 17:30:16 +01:00
|
|
|
Dict[:obj:`str`, :class:`object`]]], Dict[:obj:`str`, :obj:`str`]]):
|
2021-10-10 15:10:21 +02:00
|
|
|
The relevant data to restore :class:`telegram.ext.CallbackDataCache`.
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
if self.callback_data == data:
|
|
|
|
return
|
2022-04-24 12:38:09 +02:00
|
|
|
self.callback_data = data
|
2021-06-06 11:48:48 +02:00
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_callback_data"), self.callback_data)
|
2021-06-06 11:48:48 +02:00
|
|
|
else:
|
|
|
|
self._dump_singlefile()
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_chat_data(self, chat_id: int) -> None:
|
2022-02-09 17:30:16 +01:00
|
|
|
"""Will delete the specified key from the ``chat_data`` and depending on
|
2022-01-19 17:27:02 +01:00
|
|
|
:attr:`on_flush` save the pickle file.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-01-19 17:27:02 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat_id (:obj:`int`): The chat id to delete from the persistence.
|
|
|
|
"""
|
|
|
|
if self.chat_data is None:
|
|
|
|
return
|
|
|
|
self.chat_data.pop(chat_id, None) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
|
|
|
self._dump_file(Path(f"{self.filepath}_chat_data"), self.chat_data)
|
|
|
|
else:
|
|
|
|
self._dump_singlefile()
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def drop_user_data(self, user_id: int) -> None:
|
2022-02-09 17:30:16 +01:00
|
|
|
"""Will delete the specified key from the ``user_data`` and depending on
|
2022-01-19 17:27:02 +01:00
|
|
|
:attr:`on_flush` save the pickle file.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-01-19 17:27:02 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user id to delete from the persistence.
|
|
|
|
"""
|
|
|
|
if self.user_data is None:
|
|
|
|
return
|
|
|
|
self.user_data.pop(user_id, None) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
|
|
|
self._dump_file(Path(f"{self.filepath}_user_data"), self.user_data)
|
|
|
|
else:
|
|
|
|
self._dump_singlefile()
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_user_data(self, user_id: int, user_data: UD) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
|
|
|
|
"""
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
|
|
|
|
"""
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def refresh_bot_data(self, bot_data: BD) -> None:
|
2021-06-06 10:37:53 +02:00
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
|
|
|
|
"""
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def flush(self) -> None:
|
2019-02-13 23:30:29 +01:00
|
|
|
"""Will save all data in memory to pickle file(s)."""
|
|
|
|
if self.single_file:
|
2021-06-06 11:48:48 +02:00
|
|
|
if (
|
|
|
|
self.user_data
|
|
|
|
or self.chat_data
|
|
|
|
or self.bot_data
|
|
|
|
or self.callback_data
|
|
|
|
or self.conversations
|
|
|
|
):
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_singlefile()
|
2019-02-13 23:30:29 +01:00
|
|
|
else:
|
|
|
|
if self.user_data:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_user_data"), self.user_data)
|
2019-02-13 23:30:29 +01:00
|
|
|
if self.chat_data:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_chat_data"), self.chat_data)
|
2020-02-02 22:20:31 +01:00
|
|
|
if self.bot_data:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_bot_data"), self.bot_data)
|
2021-06-06 11:48:48 +02:00
|
|
|
if self.callback_data:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_callback_data"), self.callback_data)
|
2019-02-13 23:30:29 +01:00
|
|
|
if self.conversations:
|
2021-10-05 19:50:11 +02:00
|
|
|
self._dump_file(Path(f"{self.filepath}_conversations"), self.conversations)
|