dispatcher/*handler methods to snake_case + deprecation warnings

This commit is contained in:
Rahiel Kasim 2016-04-28 14:29:27 +02:00
parent 9d367e9f2c
commit 592352c849
11 changed files with 115 additions and 49 deletions

View file

@ -21,6 +21,7 @@
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class CallbackQueryHandler(Handler):
@ -29,7 +30,7 @@ class CallbackQueryHandler(Handler):
Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
@ -39,10 +40,15 @@ class CallbackQueryHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(CallbackQueryHandler, self).__init__(callback, pass_update_queue)
def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, Update) and update.callback_query
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.CallbackQueryHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -21,6 +21,7 @@
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class ChosenInlineResultHandler(Handler):
@ -30,7 +31,7 @@ class ChosenInlineResultHandler(Handler):
Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
@ -41,10 +42,15 @@ class ChosenInlineResultHandler(Handler):
super(ChosenInlineResultHandler, self).__init__(callback,
pass_update_queue)
def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, Update) and update.chosen_inline_result
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.ChosenInlineResultHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -21,6 +21,7 @@
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class CommandHandler(Handler):
@ -32,7 +33,7 @@ class CommandHandler(Handler):
Args:
command (str): The name of the command this handler should listen for.
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_args (optional[bool]): If the handler should be passed the
arguments passed to the command as a keyword argument called `
@ -49,7 +50,7 @@ class CommandHandler(Handler):
self.command = command
self.pass_args = pass_args
def checkUpdate(self, update):
def check_update(self, update):
return (isinstance(update, Update) and
update.message and
update.message.text and
@ -57,10 +58,15 @@ class CommandHandler(Handler):
update.message.text[1:].split(' ')[0].split('@')[0] ==
self.command)
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
if self.pass_args:
optional_args['args'] = update.message.text.split(' ')[1:]
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.CommandHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -28,6 +28,7 @@ from queue import Empty
from telegram import (TelegramError, NullHandler)
from telegram.ext.handler import Handler
from telegram.utils.deprecate import deprecate
logging.getLogger(__name__).addHandler(NullHandler())
@ -201,7 +202,7 @@ class Dispatcher(object):
'processing the update')
break
def addHandler(self, handler, group=DEFAULT_GROUP):
def add_handler(self, handler, group=DEFAULT_GROUP):
"""
Register a handler.
@ -239,7 +240,7 @@ class Dispatcher(object):
self.handlers[group].append(handler)
def removeHandler(self, handler, group=DEFAULT_GROUP):
def remove_handler(self, handler, group=DEFAULT_GROUP):
"""
Remove a handler from the specified group
@ -253,7 +254,7 @@ class Dispatcher(object):
del self.handlers[group]
self.groups.remove(group)
def addErrorHandler(self, callback):
def add_error_handler(self, callback):
"""
Registers an error handler in the Dispatcher.
@ -264,7 +265,7 @@ class Dispatcher(object):
self.error_handlers.append(callback)
def removeErrorHandler(self, callback):
def remove_error_handler(self, callback):
"""
De-registers an error handler.
@ -287,8 +288,10 @@ class Dispatcher(object):
for callback in self.error_handlers:
callback(self.bot, update, error)
# snake_case (PEP8) aliases
add_handler = addHandler
remove_handler = removeHandler
add_error_handler = addErrorHandler
remove_error_handler = removeErrorHandler
# old non-PEP8 Dispatcher methods
m = "telegram.dispatcher."
addHandler = deprecate(add_handler, m + "AddHandler", m + "add_handler")
removeHandler = deprecate(remove_handler, m + "removeHandler", m + "remove_handler")
addErrorHandler = deprecate(add_error_handler, m + "addErrorHandler", m + "add_error_handler")
removeErrorHandler = deprecate(remove_error_handler,
m + "removeErrorHandler", m + "remove_error_handler")

View file

@ -20,6 +20,8 @@
""" This module contains the base class for handlers as used by the
Dispatcher """
from telegram.deprecate import deprecate
class Handler(object):
"""
@ -28,7 +30,7 @@ class Handler(object):
Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the callback should be passed
the update queue as a keyword argument called ``update_queue``. It
@ -39,7 +41,7 @@ class Handler(object):
self.callback = callback
self.pass_update_queue = pass_update_queue
def checkUpdate(self, update):
def check_update(self, update):
"""
This method is called to determine if an update should be handled by
this handler instance. It should always be overridden.
@ -52,7 +54,7 @@ class Handler(object):
"""
raise NotImplementedError
def handleUpdate(self, update, dispatcher):
def handle_update(self, update, dispatcher):
"""
This method is called if it was determined that an update should indeed
be handled by this instance. It should also be overridden, but in most
@ -65,7 +67,7 @@ class Handler(object):
"""
raise NotImplementedError
def collectOptionalArgs(self, dispatcher):
def collect_optional_args(self, dispatcher):
"""
Prepares the optional arguments that are the same for all types of
handlers
@ -78,3 +80,10 @@ class Handler(object):
optional_args['update_queue'] = dispatcher.update_queue
return optional_args
# old non-PEP8 Handler methods
m = "telegram.Handler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")
collectOptionalArgs = deprecate(collect_optional_args,
m + "collectOptionalArgs", m + "collect_optional_args")

View file

@ -21,6 +21,7 @@
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class InlineQueryHandler(Handler):
@ -29,7 +30,7 @@ class InlineQueryHandler(Handler):
Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
@ -39,10 +40,15 @@ class InlineQueryHandler(Handler):
def __init__(self, callback, pass_update_queue=False):
super(InlineQueryHandler, self).__init__(callback, pass_update_queue)
def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, Update) and update.inline_query
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.InlineQueryHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -21,6 +21,7 @@
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class Filters(object):
@ -103,7 +104,7 @@ class MessageHandler(Handler):
accepted. If ``bool(filters)`` evaluates to ``False``, messages are
not filtered.
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
@ -114,7 +115,7 @@ class MessageHandler(Handler):
super(MessageHandler, self).__init__(callback, pass_update_queue)
self.filters = filters
def checkUpdate(self, update):
def check_update(self, update):
if isinstance(update, Update) and update.message:
if not self.filters:
res = True
@ -124,7 +125,12 @@ class MessageHandler(Handler):
res = False
return res
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.MessageHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -25,6 +25,7 @@ from future.utils import string_types
from .handler import Handler
from telegram import Update
from telegram.utils.deprecate import deprecate
class RegexHandler(Handler):
@ -37,7 +38,7 @@ class RegexHandler(Handler):
Args:
pattern (str or Pattern): The regex pattern.
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_groups (optional[bool]): If the callback should be passed the
result of ``re.match(pattern, text).groups()`` as a keyword
@ -61,7 +62,7 @@ class RegexHandler(Handler):
self.pass_groups = pass_groups
self.pass_groupdict = pass_groupdict
def checkUpdate(self, update):
def check_update(self, update):
if (isinstance(update, Update) and
update.message and
update.message.text):
@ -70,8 +71,8 @@ class RegexHandler(Handler):
else:
return False
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
match = re.match(self.pattern, update.message.text)
if self.pass_groups:
@ -80,3 +81,8 @@ class RegexHandler(Handler):
optional_args['groupdict'] = match.groupdict()
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.RegexHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -20,6 +20,7 @@
""" This module contains the StringCommandHandler class """
from .handler import Handler
from telegram.utils.deprecate import deprecate
class StringCommandHandler(Handler):
@ -30,7 +31,7 @@ class StringCommandHandler(Handler):
Args:
command (str): The name of the command this handler should listen for.
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_args (optional[bool]): If the handler should be passed the
arguments passed to the command as a keyword argument called `
@ -47,15 +48,20 @@ class StringCommandHandler(Handler):
self.command = command
self.pass_args = pass_args
def checkUpdate(self, update):
def check_update(self, update):
return (isinstance(update, str) and
update.startswith('/') and
update[1:].split(' ')[0] == self.command)
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
if self.pass_args:
optional_args['args'] = update.split(' ')[1:]
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.StringCommandHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -24,6 +24,7 @@ import re
from future.utils import string_types
from .handler import Handler
from telegram.utils.deprecate import deprecate
class StringRegexHandler(Handler):
@ -36,7 +37,7 @@ class StringRegexHandler(Handler):
Args:
pattern (str or Pattern): The regex pattern.
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_groups (optional[bool]): If the callback should be passed the
result of ``re.match(pattern, update).groups()`` as a keyword
@ -60,12 +61,12 @@ class StringRegexHandler(Handler):
self.pass_groups = pass_groups
self.pass_groupdict = pass_groupdict
def checkUpdate(self, update):
def check_update(self, update):
return isinstance(update, string_types) and bool(
re.match(self.pattern, update))
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
match = re.match(self.pattern, update)
if self.pass_groups:
@ -74,3 +75,8 @@ class StringRegexHandler(Handler):
optional_args['groupdict'] = match.groupdict()
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.StringRegexHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")

View file

@ -20,6 +20,7 @@
""" This module contains the TypeHandler class """
from .handler import Handler
from telegram.utils.deprecate import deprecate
class TypeHandler(Handler):
@ -30,7 +31,7 @@ class TypeHandler(Handler):
type (type): The ``type`` of updates this handler should process, as
determined by ``isinstance``
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``checkUpdate``
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
strict (optional[bool]): Use ``type`` instead of ``isinstance``.
Default is ``False``
@ -44,13 +45,18 @@ class TypeHandler(Handler):
self.type = type
self.strict = strict
def checkUpdate(self, update):
def check_update(self, update):
if not self.strict:
return isinstance(update, self.type)
else:
return type(update) is self.type
def handleUpdate(self, update, dispatcher):
optional_args = self.collectOptionalArgs(dispatcher)
def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher)
self.callback(dispatcher.bot, update, **optional_args)
# old non-PEP8 Handler methods
m = "telegram.TypeHandler."
checkUpdate = deprecate(check_update, m + "checkUpdate", m + "check_update")
handleUpdate = deprecate(handle_update, m + "handleUpdate", m + "handle_update")