Merge branch 'master' into V12

This commit is contained in:
Jasmin Bom 2019-02-14 11:52:31 +01:00
commit 9f1eccf569
6 changed files with 113 additions and 22 deletions

View file

@ -19,6 +19,7 @@
"""This module contains the ConversationHandler."""
import logging
import warnings
from telegram import Update
from telegram.ext import (Handler, CallbackQueryHandler, InlineQueryHandler,
@ -173,8 +174,8 @@ class ConversationHandler(Handler):
raise ValueError("'per_user', 'per_chat' and 'per_message' can't all be 'False'")
if self.per_message and not self.per_chat:
logging.warning("If 'per_message=True' is used, 'per_chat=True' should also be used, "
"since message IDs are not globally unique.")
warnings.warn("If 'per_message=True' is used, 'per_chat=True' should also be used, "
"since message IDs are not globally unique.")
all_handlers = list()
all_handlers.extend(entry_points)
@ -186,20 +187,23 @@ class ConversationHandler(Handler):
if self.per_message:
for handler in all_handlers:
if not isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
warnings.warn("If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers "
"have a message context.")
break
else:
for handler in all_handlers:
if isinstance(handler, CallbackQueryHandler):
logging.warning("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
warnings.warn("If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message.")
break
if self.per_chat:
for handler in all_handlers:
if isinstance(handler, (InlineQueryHandler, ChosenInlineResultHandler)):
logging.warning("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
warnings.warn("If 'per_chat=True', 'InlineQueryHandler' can not be used, "
"since inline queries have no chat context.")
break
def _get_key(self, update):
chat = update.effective_chat

View file

@ -429,6 +429,15 @@ class Dispatcher(object):
del self.handlers[group]
self.groups.remove(group)
def update_persistence(self):
"""Update :attr:`user_data` and :attr:`chat_data` in :attr:`persistence`.
"""
if self.persistence:
for chat_id in self.chat_data:
self.persistence.update_chat_data(chat_id, self.chat_data[chat_id])
for user_id in self.user_data:
self.persistence.update_user_data(user_id, self.user_data[user_id])
def add_error_handler(self, callback):
"""Registers an error handler in the Dispatcher.

View file

@ -37,9 +37,9 @@ class PicklePersistence(BasePersistence):
single_file (:obj:`bool`): Optional. When ``False`` will store 3 sperate files of
`filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
``True``.
on_flush (:obj:`bool`): Optional. When ``True`` will only save to file when :meth:`flush`
is called and keep data in memory until that happens. When False will store data on any
transaction. Default is ``False``.
on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`
is called and keep data in memory until that happens. When ``False`` will store data
on any transaction *and* on call fo :meth:`flush`. Default is ``False``.
Args:
filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`
@ -52,8 +52,8 @@ class PicklePersistence(BasePersistence):
`filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
``True``.
on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`
is called and keep data in memory until that happens. When False will store data on any
transaction. Default is ``False``.
is called and keep data in memory until that happens. When ``False`` will store data
on any transaction *and* on call fo :meth:`flush`. Default is ``False``.
"""
def __init__(self, filename, store_user_data=True, store_chat_data=True, singe_file=True,
@ -222,15 +222,15 @@ class PicklePersistence(BasePersistence):
self.dump_singlefile()
def flush(self):
"""If :attr:`on_flush` is set to ``True``. Will save all data in memory to pickle file(s). If
it's ``False`` will just pass.
""" Will save all data in memory to pickle file(s).
"""
if not self.on_flush:
pass
else:
if self.single_file:
if self.single_file:
if self.user_data or self.chat_data or self.conversations:
self.dump_singlefile()
else:
else:
if self.user_data:
self.dump_file("{}_user_data".format(self.filename), self.user_data)
if self.chat_data:
self.dump_file("{}_chat_data".format(self.filename), self.chat_data)
if self.conversations:
self.dump_file("{}_conversations".format(self.filename), self.conversations)

View file

@ -500,6 +500,8 @@ class Updater(object):
self.logger.info('Received signal {} ({}), stopping...'.format(
signum, get_signal_name(signum)))
if self.persistence:
# Update user_data and chat_data before flushing
self.dispatcher.update_persistence()
self.persistence.flush()
self.stop()
if self.user_sig_handler:

View file

@ -24,7 +24,7 @@ import pytest
from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message,
PreCheckoutQuery, ShippingQuery, Update, User, MessageEntity)
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler,
MessageHandler, Filters)
MessageHandler, Filters, InlineQueryHandler)
@pytest.fixture(scope='class')
@ -479,6 +479,7 @@ class TestConversationHandler(object):
handler = ConversationHandler(entry_points=self.entry_points, states=states,
fallbacks=self.fallbacks, conversation_timeout=0.5)
dp.add_handler(handler)
# CommandHandler timeout
message = Message(0, user1, None, self.group, text='/start',
entities=[MessageEntity(type=MessageEntity.BOT_COMMAND, offset=0,
@ -516,3 +517,58 @@ class TestConversationHandler(object):
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) is None
assert not self.is_timeout
def test_per_message_warning_is_only_shown_once(self, recwarn):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CommandHandler('pourCoffee', self.drink)],
self.BREWING: [CommandHandler('startCoding', self.code)]
},
fallbacks=self.fallbacks,
per_message=True
)
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_message=True', all entry points and state handlers"
" must be 'CallbackQueryHandler', since no other handlers"
" have a message context."
)
def test_per_message_false_warning_is_only_shown_once(self, recwarn):
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [CallbackQueryHandler(self.drink)],
self.BREWING: [CallbackQueryHandler(self.code)],
},
fallbacks=self.fallbacks,
per_message=False
)
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_message=False', 'CallbackQueryHandler' will not be "
"tracked for every message."
)
def test_warnings_per_chat_is_only_shown_once(self, recwarn):
def hello(bot, update):
return self.BREWING
def bye(bot, update):
return ConversationHandler.END
ConversationHandler(
entry_points=self.entry_points,
states={
self.THIRSTY: [InlineQueryHandler(hello)],
self.BREWING: [InlineQueryHandler(bye)]
},
fallbacks=self.fallbacks,
per_chat=True
)
assert len(recwarn) == 1
assert str(recwarn[0].message) == (
"If 'per_chat=True', 'InlineQueryHandler' can not be used,"
" since inline queries have no chat context."
)

View file

@ -16,6 +16,8 @@
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
import signal
from telegram.utils.helpers import enocde_conversations_to_json
try:
@ -539,6 +541,24 @@ class TestPickelPersistence(object):
dp.add_handler(h2)
dp.process_update(update)
def test_flush_on_stop(self, bot, update, pickle_persistence, good_pickle_files):
u = Updater(bot=bot, persistence=pickle_persistence)
dp = u.dispatcher
u.running = True
dp.user_data[4242424242]['my_test'] = 'Working!'
dp.chat_data[-4242424242]['my_test2'] = 'Working2!'
u.signal_handler(signal.SIGINT, None)
del (dp)
del (u)
del (pickle_persistence)
pickle_persistence_2 = PicklePersistence(filename='pickletest',
store_user_data=True,
store_chat_data=True,
singe_file=False,
on_flush=False)
assert pickle_persistence_2.get_user_data()[4242424242]['my_test'] == 'Working!'
assert pickle_persistence_2.get_chat_data()[-4242424242]['my_test2'] == 'Working2!'
def test_with_conversationHandler(self, dp, update, good_pickle_files, pickle_persistence):
dp.persistence = pickle_persistence
dp.use_context = True