mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Some refactor
This commit is contained in:
parent
cabb10bc06
commit
ad7238fda8
7 changed files with 106 additions and 93 deletions
|
|
@ -97,22 +97,22 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
|
|||
filters_factory.bind(Text, event_handlers=[
|
||||
self.message_handlers, self.edited_message_handlers,
|
||||
self.channel_post_handlers, self.edited_channel_post_handlers,
|
||||
self.callback_query_handlers, self.poll_handlers, self.inline_query_handlers
|
||||
self.callback_query_handlers, self.poll_handlers, self.inline_query_handlers,
|
||||
])
|
||||
filters_factory.bind(HashTag, event_handlers=[
|
||||
self.message_handlers, self.edited_message_handlers,
|
||||
self.channel_post_handlers, self.edited_channel_post_handlers
|
||||
self.channel_post_handlers, self.edited_channel_post_handlers,
|
||||
])
|
||||
filters_factory.bind(Regexp, event_handlers=[
|
||||
self.message_handlers, self.edited_message_handlers,
|
||||
self.channel_post_handlers, self.edited_channel_post_handlers,
|
||||
self.callback_query_handlers, self.poll_handlers, self.inline_query_handlers
|
||||
self.callback_query_handlers, self.poll_handlers, self.inline_query_handlers,
|
||||
])
|
||||
filters_factory.bind(RegexpCommandsFilter, event_handlers=[
|
||||
self.message_handlers, self.edited_message_handlers
|
||||
self.message_handlers, self.edited_message_handlers,
|
||||
])
|
||||
filters_factory.bind(ExceptionsFilter, event_handlers=[
|
||||
self.errors_handlers
|
||||
self.errors_handlers,
|
||||
])
|
||||
filters_factory.bind(IdFilter, event_handlers=[
|
||||
self.message_handlers, self.edited_message_handlers,
|
||||
|
|
|
|||
|
|
@ -205,6 +205,13 @@ class Text(Filter):
|
|||
Simple text filter
|
||||
"""
|
||||
|
||||
_default_params = (
|
||||
('text', 'equals'),
|
||||
('text_contains', 'contains'),
|
||||
('text_startswith', 'startswith'),
|
||||
('text_endswith', 'endswith'),
|
||||
)
|
||||
|
||||
def __init__(self,
|
||||
equals: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
|
||||
contains: Optional[Union[str, LazyProxy, Iterable[Union[str, LazyProxy]]]] = None,
|
||||
|
|
@ -244,14 +251,9 @@ class Text(Filter):
|
|||
|
||||
@classmethod
|
||||
def validate(cls, full_config: Dict[str, Any]):
|
||||
if 'text' in full_config:
|
||||
return {'equals': full_config.pop('text')}
|
||||
elif 'text_contains' in full_config:
|
||||
return {'contains': full_config.pop('text_contains')}
|
||||
elif 'text_startswith' in full_config:
|
||||
return {'startswith': full_config.pop('text_startswith')}
|
||||
elif 'text_endswith' in full_config:
|
||||
return {'endswith': full_config.pop('text_endswith')}
|
||||
for param, key in cls._default_params:
|
||||
if param in full_config:
|
||||
return {key: full_config.pop(param)}
|
||||
|
||||
async def check(self, obj: Union[Message, CallbackQuery, InlineQuery, Poll]):
|
||||
if isinstance(obj, Message):
|
||||
|
|
|
|||
|
|
@ -25,17 +25,17 @@ class State:
|
|||
|
||||
@property
|
||||
def state(self):
|
||||
if self._state is None:
|
||||
return None
|
||||
elif self._state == '*':
|
||||
if self._state is None or self._state == '*':
|
||||
return self._state
|
||||
elif self._group_name is None and self._group:
|
||||
|
||||
if self._group_name is None and self._group:
|
||||
group = self._group.__full_group_name__
|
||||
elif self._group_name:
|
||||
group = self._group_name
|
||||
else:
|
||||
group = '@'
|
||||
return f"{group}:{self._state}"
|
||||
|
||||
return f'{group}:{self._state}'
|
||||
|
||||
def set_parent(self, group):
|
||||
if not issubclass(group, StatesGroup):
|
||||
|
|
@ -73,7 +73,6 @@ class StatesGroupMeta(type):
|
|||
elif inspect.isclass(prop) and issubclass(prop, StatesGroup):
|
||||
childs.append(prop)
|
||||
prop._parent = cls
|
||||
# continue
|
||||
|
||||
cls._parent = None
|
||||
cls._childs = tuple(childs)
|
||||
|
|
@ -83,13 +82,13 @@ class StatesGroupMeta(type):
|
|||
return cls
|
||||
|
||||
@property
|
||||
def __group_name__(cls):
|
||||
def __group_name__(cls) -> str:
|
||||
return cls._group_name
|
||||
|
||||
@property
|
||||
def __full_group_name__(cls):
|
||||
def __full_group_name__(cls) -> str:
|
||||
if cls._parent:
|
||||
return cls._parent.__full_group_name__ + '.' + cls._group_name
|
||||
return '.'.join((cls._parent.__full_group_name__, cls._group_name))
|
||||
return cls._group_name
|
||||
|
||||
@property
|
||||
|
|
@ -97,7 +96,7 @@ class StatesGroupMeta(type):
|
|||
return cls._states
|
||||
|
||||
@property
|
||||
def childs(cls):
|
||||
def childs(cls) -> tuple:
|
||||
return cls._childs
|
||||
|
||||
@property
|
||||
|
|
@ -130,9 +129,9 @@ class StatesGroupMeta(type):
|
|||
def __contains__(cls, item):
|
||||
if isinstance(item, str):
|
||||
return item in cls.all_states_names
|
||||
elif isinstance(item, State):
|
||||
if isinstance(item, State):
|
||||
return item in cls.all_states
|
||||
elif isinstance(item, StatesGroup):
|
||||
if isinstance(item, StatesGroup):
|
||||
return item in cls.all_childs
|
||||
return False
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ T = TypeVar('T')
|
|||
|
||||
class ContextInstanceMixin:
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
cls.__context_instance = contextvars.ContextVar('instance_' + cls.__name__)
|
||||
cls.__context_instance = contextvars.ContextVar(f'instance_{cls.__name__}')
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
|
|
@ -43,5 +43,5 @@ class ContextInstanceMixin:
|
|||
@classmethod
|
||||
def set_current(cls: Type[T], value: T):
|
||||
if not isinstance(value, cls):
|
||||
raise TypeError(f"Value should be instance of '{cls.__name__}' not '{type(value).__name__}'")
|
||||
raise TypeError(f'Value should be instance of {cls.__name__!r} not {type(value).__name__!r}')
|
||||
cls.__context_instance.set(value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue