Добавлены примеры и обновлена модель Update
This commit is contained in:
@@ -6,48 +6,15 @@ from magic_filter.operations.comparator import ComparatorOperation as mf_compara
|
||||
F = MagicFilter()
|
||||
|
||||
|
||||
def filter_attrs(obj, *magic_args):
|
||||
def filter_attrs(obj: object, *filters: MagicFilter) -> bool:
|
||||
"""
|
||||
Применяет один или несколько фильтров MagicFilter к объекту.
|
||||
|
||||
:param obj: Любой объект с атрибутами (например, event/message)
|
||||
:param filters: Один или несколько MagicFilter выражений
|
||||
:return: True, если все фильтры возвращают True, иначе False
|
||||
"""
|
||||
try:
|
||||
for arg in magic_args:
|
||||
|
||||
attr_last = None
|
||||
method_found = False
|
||||
|
||||
operations = arg._operations
|
||||
if isinstance(operations[-1], mf_call):
|
||||
operations = operations[:len(operations)-2]
|
||||
method_found = True
|
||||
elif isinstance(operations[-1], mf_func):
|
||||
operations = operations[:len(operations)-1]
|
||||
method_found = True
|
||||
elif isinstance(operations[-1], mf_comparator):
|
||||
operations = operations[:len(operations)-1]
|
||||
|
||||
for element in operations:
|
||||
if attr_last is None:
|
||||
attr_last = getattr(obj, element.name)
|
||||
else:
|
||||
attr_last = getattr(attr_last, element.name)
|
||||
|
||||
if attr_last is None:
|
||||
break
|
||||
|
||||
if isinstance(arg._operations[-1], mf_comparator):
|
||||
return attr_last == arg._operations[-1].right
|
||||
|
||||
if not method_found:
|
||||
return bool(attr_last)
|
||||
|
||||
if attr_last is None:
|
||||
return False
|
||||
|
||||
if isinstance(arg._operations[-1], mf_func):
|
||||
func_operation: mf_func = arg._operations[-1]
|
||||
return func_operation.resolve(attr_last, attr_last)
|
||||
else:
|
||||
method = getattr(attr_last, arg._operations[-2].name)
|
||||
args = arg._operations[-1].args
|
||||
|
||||
return method(*args)
|
||||
except Exception as e:
|
||||
...
|
||||
return all(f.resolve(obj) for f in filters)
|
||||
except Exception:
|
||||
return False
|
@@ -2,9 +2,14 @@ from typing import Callable
|
||||
|
||||
from magic_filter import F, MagicFilter
|
||||
|
||||
from ..filters.middleware import BaseMiddleware
|
||||
|
||||
from ..types.command import Command
|
||||
|
||||
from ..context.state_machine import State
|
||||
|
||||
from ..enums.update import UpdateType
|
||||
|
||||
from ..loggers import logger_dp
|
||||
|
||||
|
||||
@@ -36,10 +41,11 @@ class Handler:
|
||||
:param kwargs: Дополнительные параметры (не используются)
|
||||
"""
|
||||
|
||||
self.func_event = func_event
|
||||
self.update_type = update_type
|
||||
self.func_event: Callable = func_event
|
||||
self.update_type: UpdateType = update_type
|
||||
self.filters = []
|
||||
self.state = None
|
||||
self.state: State = None
|
||||
self.middleware: BaseMiddleware = None
|
||||
|
||||
for arg in args:
|
||||
if isinstance(arg, MagicFilter):
|
||||
@@ -48,6 +54,8 @@ class Handler:
|
||||
self.state = arg
|
||||
elif isinstance(arg, Command):
|
||||
self.filters.insert(0, F.message.body.text.startswith(arg.command))
|
||||
elif isinstance(arg, BaseMiddleware):
|
||||
self.middleware = arg
|
||||
else:
|
||||
logger_dp.info(f'Обнаружен неизвестный фильтр `{arg}` при '
|
||||
f'регистрации функции `{func_event.__name__}`')
|
6
maxapi/filters/middleware.py
Normal file
6
maxapi/filters/middleware.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from ..types.updates import UpdateUnion
|
||||
|
||||
|
||||
class BaseMiddleware:
|
||||
def __init__(self):
|
||||
...
|
Reference in New Issue
Block a user