1234567891011121314151617181920212223242526272829 |
- from typing import Generic, TypeVar, Any
- from contextlib import asynccontextmanager
- from ..types import Message, Context
- Dep = TypeVar("DepType")
- class Injector(Generic[Dep]):
- async def inject(self, message: Message, context: Context) -> Dep:
- raise NotImplementedError
- class InjectorWithCleanup(Injector[Dep]):
- async def cleanup(self, dep: Dep):
- raise NotImplementedError
- @asynccontextmanager
- async def inject_all(injectors: list[Injector[Any]], message: Message, context: Context):
- try:
- deps = []
- for inj in injectors:
- deps.append(await inj.inject(message, context))
- yield deps
- finally:
- for dep, inj in zip(deps, injectors):
- if isinstance(inj, InjectorWithCleanup):
- await inj.cleanup(dep)
|