Skip to content

Commit

Permalink
startup 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RootShinobi committed Jan 18, 2024
1 parent 7502782 commit bacbcb4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 85 deletions.
19 changes: 8 additions & 11 deletions aiogram_i18n/lazy/base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from abc import abstractmethod
from typing import TYPE_CHECKING, Any, Dict, Union
from typing import Any

if TYPE_CHECKING:
from aiogram_i18n import I18nMiddleware
from aiogram.filters.base import Filter

from aiogram_i18n.managers.base import CallableMixin

class BaseLazyFilter:
async def startup(self, middleware: "I18nMiddleware") -> None:
pass

class BaseLazyFilter(Filter):
async def call(self, context_key: str, **kwargs: Any):
return await CallableMixin(callback=self.startup).call(kwargs.pop(context_key), **kwargs)

@abstractmethod
async def __call__(self, *args: Any, **kwargs: Any) -> Union[bool, Dict[str, Any]]:
async def startup(self, *args: Any, **kwargs: Any) -> None:
pass

def __await__(self): # type: ignore # pragma: no cover
# Is needed only for inspection and this method is never be called
return self.__call__
12 changes: 5 additions & 7 deletions aiogram_i18n/lazy/filter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from typing import TYPE_CHECKING, Any, Dict, Tuple, Union
from typing import Any, Dict, Tuple, Union

from aiogram.types import Message

from aiogram_i18n import I18nContext
from aiogram_i18n.lazy.base import BaseLazyFilter

if TYPE_CHECKING:
from aiogram_i18n import I18nMiddleware


class LazyFilter(BaseLazyFilter):
keys: Tuple[str, ...]
Expand All @@ -16,10 +14,10 @@ def __init__(self, *keys: str):
self.keys = keys
self.all_keys = ()

async def startup(self, middleware: "I18nMiddleware") -> None:
async def startup(self, i18n: I18nContext) -> None:
self.all_keys = tuple(
middleware.core.get(key, locale)
for locale in middleware.core.available_locales
i18n.core.get(key, locale)
for locale in i18n.core.available_locales
for key in self.keys
)

Expand Down
2 changes: 1 addition & 1 deletion aiogram_i18n/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
try:
from aiogram.dispatcher.event.handler import CallableObject as CallableMixin # type: ignore
except ImportError:
from aiogram.dispatcher.event.handler import CallableMixin
from aiogram.dispatcher.event.handler import CallableMixin # type: ignore


class BaseManager(ABC):
Expand Down
25 changes: 18 additions & 7 deletions aiogram_i18n/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from aiogram_i18n.context import I18nContext
from aiogram_i18n.cores.base import BaseCore
from aiogram_i18n.lazy.base import BaseLazyFilter
from aiogram_i18n.managers.base import BaseManager
from aiogram_i18n.managers.base import BaseManager, CallableMixin
from aiogram_i18n.managers.memory import MemoryManager
from aiogram_i18n.types import StartupFunction
from aiogram_i18n.utils.context_instance import ContextInstanceMixin


Expand Down Expand Up @@ -49,6 +50,11 @@ def __init__(
I18nMiddleware.set_current(self)
if locale_key:
warn("parameter locale_key deprecated since version 2.0")
self._startup: list[CallableMixin] = []

def on_startup(self, func: StartupFunction):
self._startup.append(CallableMixin(callback=func))
return func

def setup(self, dispatcher: Dispatcher) -> None:
dispatcher.update.outer_middleware.register(self)
Expand All @@ -61,14 +67,19 @@ def setup(self, dispatcher: Dispatcher) -> None:
dispatcher[self.middleware_key] = self

async def startup(self, dispatcher: Dispatcher, **kwargs) -> None:
kwargs.update(dispatcher=dispatcher)
with self.use_context(data=kwargs):
for startup_func in self._startup:
await startup_func.call(**kwargs)
for sub_router in dispatcher.chain_tail:
for observ in sub_router.observers.values():
for handler in observ.handlers:
if handler.filters:
for filter_ in handler.filters:
if isinstance(filter_.callback, BaseLazyFilter):
await filter_.callback.startup(middleware=self)
for observer in sub_router.observers.values():
for handler in observer.handlers:
if not handler.filters:
continue
for filter_ in handler.filters:
if not isinstance(filter_.callback, BaseLazyFilter):
continue
await filter_.callback.call(self.context_key, **kwargs)

async def __call__(
self,
Expand Down
Loading

0 comments on commit bacbcb4

Please sign in to comment.