Skip to content

Commit

Permalink
Remove deprecated code-stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
JrooTJunior committed Aug 4, 2017
1 parent abaa6a6 commit 2ac0115
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 1,053 deletions.
28 changes: 3 additions & 25 deletions aiogram/dispatcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import typing

from .filters import CommandsFilter, RegexpFilter, ContentTypeFilter, generate_default_filters
from .handler import Handler, NextStepHandler
from .handler import Handler
from .storage import DisabledStorage, BaseStorage, FSMContext
from .. import types
from ..bot import Bot
from ..types.message import ContentType
from ..utils.deprecated import deprecated

log = logging.getLogger(__name__)

Expand All @@ -22,7 +20,7 @@ class Dispatcher:
Provide next step handler and etc.
"""

def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage]=None):
def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage] = None):
if loop is None:
loop = bot.loop
if storage is None:
Expand All @@ -45,9 +43,7 @@ def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage]=None):
self.shipping_query_handlers = Handler(self)
self.pre_checkout_query_handlers = Handler(self)

self.next_step_message_handlers = NextStepHandler(self)
self.updates_handler.register(self.process_update)
# self.message_handlers.register(self._notify_next_message)

self._pooling = False

Expand Down Expand Up @@ -90,8 +86,7 @@ async def process_update(self, update):
"""
self.last_update_id = update.update_id
if update.message:
if not await self.next_step_message_handlers.notify(update.message):
await self.message_handlers.notify(update.message)
await self.message_handlers.notify(update.message)
if update.edited_message:
await self.edited_message_handlers.notify(update.edited_message)
if update.channel_post:
Expand Down Expand Up @@ -687,23 +682,6 @@ def decorator(callback):

return decorator

@deprecated("Use FSM instead of next step message handler.")
async def next_message(self, message: types.Message, otherwise=None, once=False, include_cancel=True,
regexp=None, content_types=None, func=None, custom_filters=None, **kwargs):
if content_types is None:
content_types = []
if custom_filters is None:
custom_filters = []

filters_set = generate_default_filters(self,
*custom_filters,
regexp=regexp,
content_types=content_types,
func=func,
**kwargs)
self.next_step_message_handlers.register(message, otherwise, once, include_cancel, filters_set)
return await self.next_step_message_handlers.wait(message)

def current_state(self, *,
chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None) -> FSMContext:
Expand Down
68 changes: 1 addition & 67 deletions aiogram/dispatcher/handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from asyncio import Event

from .filters import check_filters, CancelFilter
from .. import types
from .filters import check_filters


class SkipHandler(BaseException):
Expand Down Expand Up @@ -47,66 +44,3 @@ async def notify(self, *args, **kwargs):
continue
except CancelHandler:
break


class NextStepHandler:
def __init__(self, dispatcher):
self.dispatcher = dispatcher
self.handlers = {}

def register(self, message, otherwise=None, once=False, include_cancel=False, filters=None):
identifier = gen_identifier(message.chat.id, message.chat.id)

if identifier not in self.handlers:
self.handlers[identifier] = {'event': Event(), 'filters': filters,
'otherwise': otherwise, 'once': once,
'include_cancel': include_cancel,
'message': None}
return True

# In normal it's impossible.
raise RuntimeError('Dialog already wait message.')
# return False

async def notify(self, message):
identifier = gen_identifier(message.chat.id, message.chat.id)
if identifier not in self.handlers:
return False
handler = self.handlers[identifier]

include_cancel = handler['include_cancel']
if include_cancel:
filter_ = CancelFilter(include_cancel if isinstance(include_cancel, (list, set, tuple)) else None)
if filter_.check(message):
handler['event'].set()
return True

if handler['filters'] and not await check_filters(handler['filters'], [message], {}):
otherwise = handler['otherwise']
if otherwise:
await otherwise(message)
if handler['once']:
handler['event'].set()
return True

handler['message'] = message
handler['event'].set()
return True

async def wait(self, message) -> types.Message:
identifier = gen_identifier(message.chat.id, message.chat.id)

handler = self.handlers[identifier]
event = handler.get('event')

await event.wait()
message = self.handlers[identifier]['message']
self.reset(identifier)
return message

def reset(self, identifier):
del self.handlers[identifier]


def gen_identifier(chat_id, from_user_id):
return "{0}:{1}".format(chat_id, from_user_id)

0 comments on commit 2ac0115

Please sign in to comment.