nonebot2/nonebot/processor/models.py

49 lines
1.7 KiB
Python
Raw Normal View History

2021-11-14 01:34:25 +08:00
from typing import TYPE_CHECKING, Any, List, Type, Tuple, Callable, Optional
2021-11-12 20:55:59 +08:00
from nonebot.utils import get_name
2021-11-13 19:38:01 +08:00
if TYPE_CHECKING:
from nonebot.adapters import Bot, Event
2021-11-12 20:55:59 +08:00
class Depends:
def __init__(self,
dependency: Optional[Callable[..., Any]] = None,
*,
use_cache: bool = True) -> None:
self.dependency = dependency
self.use_cache = use_cache
def __repr__(self) -> str:
dep = get_name(self.dependency)
cache = "" if self.use_cache else ", use_cache=False"
return f"{self.__class__.__name__}({dep}{cache})"
class Dependent:
def __init__(self,
*,
func: Optional[Callable[..., Any]] = None,
name: Optional[str] = None,
bot_param_name: Optional[str] = None,
2021-11-14 01:34:25 +08:00
bot_param_type: Optional[Tuple[Type["Bot"], ...]] = None,
2021-11-12 20:55:59 +08:00
event_param_name: Optional[str] = None,
2021-11-14 01:34:25 +08:00
event_param_type: Optional[Tuple[Type["Event"], ...]] = None,
2021-11-12 20:55:59 +08:00
state_param_name: Optional[str] = None,
matcher_param_name: Optional[str] = None,
dependencies: Optional[List["Dependent"]] = None,
use_cache: bool = True) -> None:
self.func = func
self.name = name
self.bot_param_name = bot_param_name
2021-11-13 19:38:01 +08:00
self.bot_param_type = bot_param_type
2021-11-12 20:55:59 +08:00
self.event_param_name = event_param_name
2021-11-13 19:38:01 +08:00
self.event_param_type = event_param_type
2021-11-12 20:55:59 +08:00
self.state_param_name = state_param_name
self.matcher_param_name = matcher_param_name
self.dependencies = dependencies or []
self.use_cache = use_cache
self.cache_key = self.func