mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 01:54:53 +00:00
Comments in dispatcher.filters
This commit is contained in:
parent
bbacfb6a50
commit
3903fbd626
1 changed files with 57 additions and 0 deletions
|
|
@ -10,6 +10,14 @@ USER_STATE = 'USER_STATE'
|
|||
|
||||
|
||||
async def check_filter(filter_, args, kwargs):
|
||||
"""
|
||||
Helper for executing filter
|
||||
|
||||
:param filter_:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
if not callable(filter_):
|
||||
raise TypeError('Filter must be callable and/or awaitable!')
|
||||
|
||||
|
|
@ -20,6 +28,14 @@ async def check_filter(filter_, args, kwargs):
|
|||
|
||||
|
||||
async def check_filters(filters, args, kwargs):
|
||||
"""
|
||||
Check list of filters
|
||||
|
||||
:param filters:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
if filters is not None:
|
||||
for filter_ in filters:
|
||||
f = await check_filter(filter_, args, kwargs)
|
||||
|
|
@ -29,6 +45,9 @@ async def check_filters(filters, args, kwargs):
|
|||
|
||||
|
||||
class Filter:
|
||||
"""
|
||||
Base class for filters
|
||||
"""
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.check(*args, **kwargs)
|
||||
|
||||
|
|
@ -37,6 +56,9 @@ class Filter:
|
|||
|
||||
|
||||
class AsyncFilter(Filter):
|
||||
"""
|
||||
Base class for asynchronous filters
|
||||
"""
|
||||
def __aiter__(self):
|
||||
return None
|
||||
|
||||
|
|
@ -48,6 +70,9 @@ class AsyncFilter(Filter):
|
|||
|
||||
|
||||
class AnyFilter(AsyncFilter):
|
||||
"""
|
||||
One filter from many
|
||||
"""
|
||||
def __init__(self, *filters: callable):
|
||||
self.filters = filters
|
||||
|
||||
|
|
@ -57,6 +82,9 @@ class AnyFilter(AsyncFilter):
|
|||
|
||||
|
||||
class NotFilter(AsyncFilter):
|
||||
"""
|
||||
Reverse filter
|
||||
"""
|
||||
def __init__(self, filter_: callable):
|
||||
self.filter = filter_
|
||||
|
||||
|
|
@ -65,6 +93,9 @@ class NotFilter(AsyncFilter):
|
|||
|
||||
|
||||
class CommandsFilter(AsyncFilter):
|
||||
"""
|
||||
Check commands in message
|
||||
"""
|
||||
def __init__(self, commands):
|
||||
self.commands = commands
|
||||
|
||||
|
|
@ -85,6 +116,9 @@ class CommandsFilter(AsyncFilter):
|
|||
|
||||
|
||||
class RegexpFilter(Filter):
|
||||
"""
|
||||
Regexp filter for messages
|
||||
"""
|
||||
def __init__(self, regexp):
|
||||
self.regexp = re.compile(regexp, flags=re.IGNORECASE | re.MULTILINE)
|
||||
|
||||
|
|
@ -94,6 +128,9 @@ class RegexpFilter(Filter):
|
|||
|
||||
|
||||
class ContentTypeFilter(Filter):
|
||||
"""
|
||||
Check message content type
|
||||
"""
|
||||
def __init__(self, content_types):
|
||||
self.content_types = content_types
|
||||
|
||||
|
|
@ -103,6 +140,9 @@ class ContentTypeFilter(Filter):
|
|||
|
||||
|
||||
class CancelFilter(Filter):
|
||||
"""
|
||||
Find cancel in message text
|
||||
"""
|
||||
def __init__(self, cancel_set=None):
|
||||
if cancel_set is None:
|
||||
cancel_set = ['/cancel', 'cancel', 'cancel.']
|
||||
|
|
@ -114,6 +154,9 @@ class CancelFilter(Filter):
|
|||
|
||||
|
||||
class StateFilter(AsyncFilter):
|
||||
"""
|
||||
Check user state
|
||||
"""
|
||||
def __init__(self, dispatcher, state):
|
||||
self.dispatcher = dispatcher
|
||||
self.state = state
|
||||
|
|
@ -137,6 +180,9 @@ class StateFilter(AsyncFilter):
|
|||
|
||||
|
||||
class StatesListFilter(StateFilter):
|
||||
"""
|
||||
List of states
|
||||
"""
|
||||
async def check(self, obj):
|
||||
chat, user = self.get_target(obj)
|
||||
|
||||
|
|
@ -146,6 +192,9 @@ class StatesListFilter(StateFilter):
|
|||
|
||||
|
||||
class ExceptionsFilter(Filter):
|
||||
"""
|
||||
Filter for exceptions
|
||||
"""
|
||||
def __init__(self, exception):
|
||||
self.exception = exception
|
||||
|
||||
|
|
@ -159,6 +208,14 @@ class ExceptionsFilter(Filter):
|
|||
|
||||
|
||||
def generate_default_filters(dispatcher, *args, **kwargs):
|
||||
"""
|
||||
Prepare filters
|
||||
|
||||
:param dispatcher:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
filters_set = []
|
||||
|
||||
for name, filter_ in kwargs.items():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue