2018-09-20 22:50:40 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2021-01-03 06:10:24 +01:00
|
|
|
# Copyright (C) 2015-2021
|
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
|
|
|
|
from collections import defaultdict
|
2021-06-06 10:37:53 +02:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Dict,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
|
|
|
overload,
|
|
|
|
cast,
|
|
|
|
DefaultDict,
|
|
|
|
)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
from telegram.ext import BasePersistence
|
2021-06-06 10:37:53 +02:00
|
|
|
from telegram.utils.types import ConversationDict # pylint: disable=W0611
|
|
|
|
from .utils.types import UD, CD, BD
|
|
|
|
from .contexttypes import ContextTypes
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
class PicklePersistence(BasePersistence[UD, CD, BD]):
|
2018-09-20 22:50:40 +02:00
|
|
|
"""Using python's builtin pickle for making you bot persistent.
|
|
|
|
|
2020-07-13 21:52:26 +02:00
|
|
|
Warning:
|
|
|
|
:class:`PicklePersistence` will try to replace :class:`telegram.Bot` instances by
|
|
|
|
:attr:`REPLACED_BOT` and insert the bot set with
|
|
|
|
:meth:`telegram.ext.BasePersistence.set_bot` upon loading of the data. This is to ensure
|
|
|
|
that changes to the bot apply to the saved objects, too. If you change the bots token, this
|
|
|
|
may lead to e.g. ``Chat not found`` errors. For the limitations on replacing bots see
|
|
|
|
:meth:`telegram.ext.BasePersistence.replace_bot` and
|
|
|
|
:meth:`telegram.ext.BasePersistence.insert_bot`.
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Args:
|
2018-09-20 22:50:40 +02:00
|
|
|
filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`
|
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
|
|
|
is :obj:`False` this will be used as a prefix.
|
2020-12-30 15:59:50 +01:00
|
|
|
store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this
|
|
|
|
persistence class. Default is :obj:`True`.
|
|
|
|
store_chat_data (:obj:`bool`, optional): Whether user_data should be saved by this
|
|
|
|
persistence class. Default is :obj:`True`.
|
|
|
|
store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this
|
|
|
|
persistence class. Default is :obj:`True` .
|
|
|
|
single_file (:obj:`bool`, optional): When :obj:`False` will store 3 separate files of
|
2018-09-20 22:50:40 +02:00
|
|
|
`filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
|
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
|
|
|
:obj:`True`.
|
|
|
|
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
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2018-09-20 22:50:40 +02:00
|
|
|
filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`
|
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
|
|
|
is :obj:`False` this will be used as a prefix.
|
2020-12-30 15:59:50 +01:00
|
|
|
store_user_data (:obj:`bool`): Optional. Whether user_data should be saved by this
|
|
|
|
persistence class.
|
|
|
|
store_chat_data (:obj:`bool`): Optional. Whether user_data should be saved by this
|
|
|
|
persistence class.
|
|
|
|
store_bot_data (:obj:`bool`): Optional. Whether bot_data should be saved by this
|
|
|
|
persistence class.
|
|
|
|
single_file (:obj:`bool`): Optional. When :obj:`False` will store 3 separate files of
|
2018-09-20 22:50:40 +02:00
|
|
|
`filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
|
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
|
|
|
:obj:`True`.
|
|
|
|
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`): 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__ = (
|
|
|
|
'filename',
|
|
|
|
'single_file',
|
|
|
|
'on_flush',
|
|
|
|
'user_data',
|
|
|
|
'chat_data',
|
|
|
|
'bot_data',
|
|
|
|
'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__(
|
|
|
|
self: 'PicklePersistence[Dict, Dict, Dict]',
|
|
|
|
filename: str,
|
|
|
|
store_user_data: bool = True,
|
|
|
|
store_chat_data: bool = True,
|
|
|
|
store_bot_data: bool = True,
|
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
|
|
|
):
|
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def __init__(
|
|
|
|
self: 'PicklePersistence[UD, CD, BD]',
|
|
|
|
filename: str,
|
|
|
|
store_user_data: bool = True,
|
|
|
|
store_chat_data: bool = True,
|
|
|
|
store_bot_data: bool = True,
|
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
|
|
|
context_types: ContextTypes[Any, UD, CD, BD] = None,
|
|
|
|
):
|
|
|
|
...
|
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
filename: str,
|
|
|
|
store_user_data: bool = True,
|
|
|
|
store_chat_data: bool = True,
|
|
|
|
store_bot_data: bool = True,
|
|
|
|
single_file: bool = True,
|
|
|
|
on_flush: bool = False,
|
2021-06-06 10:37:53 +02:00
|
|
|
context_types: ContextTypes[Any, UD, CD, BD] = None,
|
2020-10-09 17:22:07 +02:00
|
|
|
):
|
|
|
|
super().__init__(
|
|
|
|
store_user_data=store_user_data,
|
|
|
|
store_chat_data=store_chat_data,
|
|
|
|
store_bot_data=store_bot_data,
|
|
|
|
)
|
2018-09-20 22:50:40 +02:00
|
|
|
self.filename = filename
|
2019-08-29 19:09:38 +02:00
|
|
|
self.single_file = single_file
|
2018-09-20 22:50:40 +02:00
|
|
|
self.on_flush = on_flush
|
2021-06-06 10:37:53 +02:00
|
|
|
self.user_data: Optional[DefaultDict[int, UD]] = None
|
|
|
|
self.chat_data: Optional[DefaultDict[int, CD]] = None
|
|
|
|
self.bot_data: Optional[BD] = None
|
2021-01-30 11:38:54 +01:00
|
|
|
self.conversations: Optional[Dict[str, Dict[Tuple, object]]] = None
|
2021-06-06 10:37:53 +02:00
|
|
|
self.context_types = 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:
|
|
|
|
filename = self.filename
|
2020-10-31 16:33:34 +01:00
|
|
|
with open(self.filename, "rb") as file:
|
|
|
|
data = pickle.load(file)
|
2021-06-06 10:37:53 +02:00
|
|
|
self.user_data = defaultdict(self.context_types.user_data, data['user_data'])
|
|
|
|
self.chat_data = defaultdict(self.context_types.chat_data, data['chat_data'])
|
2020-02-08 18:24:35 +01:00
|
|
|
# For backwards compatibility with files not containing bot data
|
2021-06-06 10:37:53 +02:00
|
|
|
self.bot_data = data.get('bot_data', self.context_types.bot_data())
|
2020-02-23 22:03:58 +01:00
|
|
|
self.conversations = data['conversations']
|
2021-01-17 23:24:20 +01:00
|
|
|
except OSError:
|
2021-04-05 13:25:27 +02:00
|
|
|
self.conversations = {}
|
2021-06-06 10:37:53 +02:00
|
|
|
self.user_data = defaultdict(self.context_types.user_data)
|
|
|
|
self.chat_data = defaultdict(self.context_types.chat_data)
|
|
|
|
self.bot_data = self.context_types.bot_data()
|
2020-10-31 16:33:34 +01:00
|
|
|
except pickle.UnpicklingError as exc:
|
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:
|
2020-11-23 22:09:29 +01:00
|
|
|
raise TypeError(f"Something went wrong unpickling {filename}") from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
@staticmethod
|
2021-05-27 09:38:17 +02:00
|
|
|
def _load_file(filename: str) -> Any:
|
2018-09-20 22:50:40 +02:00
|
|
|
try:
|
2020-10-31 16:33:34 +01:00
|
|
|
with open(filename, "rb") as file:
|
|
|
|
return pickle.load(file)
|
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:
|
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:
|
2020-11-23 22:09:29 +01:00
|
|
|
raise TypeError(f"Something went wrong unpickling {filename}") from exc
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-05-27 09:38:17 +02:00
|
|
|
def _dump_singlefile(self) -> None:
|
2020-10-31 16:33:34 +01:00
|
|
|
with open(self.filename, "wb") as file:
|
2020-10-09 17:22:07 +02:00
|
|
|
data = {
|
|
|
|
'conversations': self.conversations,
|
|
|
|
'user_data': self.user_data,
|
|
|
|
'chat_data': self.chat_data,
|
|
|
|
'bot_data': self.bot_data,
|
|
|
|
}
|
2020-10-31 16:33:34 +01:00
|
|
|
pickle.dump(data, file)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
@staticmethod
|
2021-05-27 09:38:17 +02:00
|
|
|
def _dump_file(filename: str, data: object) -> None:
|
2020-10-31 16:33:34 +01:00
|
|
|
with open(filename, "wb") as file:
|
|
|
|
pickle.dump(data, file)
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
def get_user_data(self) -> DefaultDict[int, UD]:
|
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
|
|
|
"""Returns the user_data from the pickle file if it exists or an empty :obj:`defaultdict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2021-06-06 10:37:53 +02:00
|
|
|
DefaultDict[:obj:`int`, :class:`telegram.ext.utils.types.UD`]: The restored user data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
if self.user_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_user_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
data = self._load_file(filename)
|
2018-09-20 22:50:40 +02:00
|
|
|
if not data:
|
2021-06-06 10:37:53 +02:00
|
|
|
data = defaultdict(self.context_types.user_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
else:
|
2021-06-06 10:37:53 +02:00
|
|
|
data = defaultdict(self.context_types.user_data, 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()
|
2021-06-06 10:37:53 +02:00
|
|
|
return self.user_data # type: ignore[return-value]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
def get_chat_data(self) -> DefaultDict[int, CD]:
|
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
|
|
|
"""Returns the chat_data from the pickle file if it exists or an empty :obj:`defaultdict`.
|
2018-09-20 22:50:40 +02:00
|
|
|
|
|
|
|
Returns:
|
2021-06-06 10:37:53 +02:00
|
|
|
DefaultDict[:obj:`int`, :class:`telegram.ext.utils.types.CD`]: The restored chat data.
|
2018-09-20 22:50:40 +02:00
|
|
|
"""
|
|
|
|
if self.chat_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_chat_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
data = self._load_file(filename)
|
2018-09-20 22:50:40 +02:00
|
|
|
if not data:
|
2021-06-06 10:37:53 +02:00
|
|
|
data = defaultdict(self.context_types.chat_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
else:
|
2021-06-06 10:37:53 +02:00
|
|
|
data = defaultdict(self.context_types.chat_data, 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()
|
2021-06-06 10:37:53 +02:00
|
|
|
return self.chat_data # type: ignore[return-value]
|
2018-09-20 22:50:40 +02:00
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
def get_bot_data(self) -> BD:
|
|
|
|
"""Returns the bot_data from the pickle file if it exists or an empty object of type
|
|
|
|
:class:`telegram.ext.utils.types.BD`.
|
2020-02-02 22:20:31 +01:00
|
|
|
|
|
|
|
Returns:
|
2021-06-06 10:37:53 +02:00
|
|
|
:class:`telegram.ext.utils.types.BD`: The restored bot data.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
if self.bot_data:
|
|
|
|
pass
|
|
|
|
elif not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_bot_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
data = self._load_file(filename)
|
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()
|
2021-06-06 10:37:53 +02:00
|
|
|
return self.bot_data # type: ignore[return-value]
|
2020-02-02 22:20:31 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def get_conversations(self, name: str) -> ConversationDict:
|
|
|
|
"""Returns the conversations from the pickle file if it exsists 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:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_conversations"
|
2021-05-27 09:38:17 +02:00
|
|
|
data = self._load_file(filename)
|
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
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
def update_conversation(
|
|
|
|
self, name: str, key: Tuple[int, ...], new_state: Optional[object]
|
|
|
|
) -> 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.
|
|
|
|
new_state (:obj:`tuple` | :obj:`any`): The new state for the given key.
|
|
|
|
"""
|
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:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_conversations"
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(filename, 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
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
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.
|
2021-06-06 10:37:53 +02:00
|
|
|
data (:class:`telegram.ext.utils.types.UD`): The
|
|
|
|
:attr:`telegram.ext.dispatcher.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:
|
2021-06-06 10:37:53 +02:00
|
|
|
self.user_data = defaultdict(self.context_types.user_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
if self.user_data.get(user_id) == data:
|
|
|
|
return
|
|
|
|
self.user_data[user_id] = data
|
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_user_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(filename, 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
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
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.
|
2021-06-06 10:37:53 +02:00
|
|
|
data (:class:`telegram.ext.utils.types.CD`): The
|
|
|
|
:attr:`telegram.ext.dispatcher.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:
|
2021-06-06 10:37:53 +02:00
|
|
|
self.chat_data = defaultdict(self.context_types.chat_data)
|
2018-09-20 22:50:40 +02:00
|
|
|
if self.chat_data.get(chat_id) == data:
|
|
|
|
return
|
|
|
|
self.chat_data[chat_id] = data
|
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_chat_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(filename, 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
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
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-06-06 10:37:53 +02:00
|
|
|
data (:class:`telegram.ext.utils.types.BD`): The
|
|
|
|
:attr:`telegram.ext.dispatcher.bot_data`.
|
2020-02-02 22:20:31 +01:00
|
|
|
"""
|
|
|
|
if self.bot_data == data:
|
|
|
|
return
|
2021-06-06 10:37:53 +02:00
|
|
|
self.bot_data = data
|
2020-02-02 22:20:31 +01:00
|
|
|
if not self.on_flush:
|
|
|
|
if not self.single_file:
|
2020-11-23 22:09:29 +01:00
|
|
|
filename = f"{self.filename}_bot_data"
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(filename, 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
|
|
|
|
2021-06-06 10:37:53 +02:00
|
|
|
def refresh_user_data(self, user_id: int, user_data: UD) -> None:
|
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data`
|
|
|
|
"""
|
|
|
|
|
|
|
|
def refresh_chat_data(self, chat_id: int, chat_data: CD) -> None:
|
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data`
|
|
|
|
"""
|
|
|
|
|
|
|
|
def refresh_bot_data(self, bot_data: BD) -> None:
|
|
|
|
"""Does nothing.
|
|
|
|
|
|
|
|
.. versionadded:: 13.6
|
|
|
|
.. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data`
|
|
|
|
"""
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def flush(self) -> None:
|
2020-10-09 17:22:07 +02:00
|
|
|
"""Will save all data in memory to pickle file(s)."""
|
2019-02-13 23:30:29 +01:00
|
|
|
if self.single_file:
|
2020-07-10 11:10:53 +02:00
|
|
|
if self.user_data or self.chat_data or self.bot_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-05-27 09:38:17 +02:00
|
|
|
self._dump_file(f"{self.filename}_user_data", self.user_data)
|
2019-02-13 23:30:29 +01:00
|
|
|
if self.chat_data:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(f"{self.filename}_chat_data", self.chat_data)
|
2020-02-02 22:20:31 +01:00
|
|
|
if self.bot_data:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(f"{self.filename}_bot_data", self.bot_data)
|
2019-02-13 23:30:29 +01:00
|
|
|
if self.conversations:
|
2021-05-27 09:38:17 +02:00
|
|
|
self._dump_file(f"{self.filename}_conversations", self.conversations)
|