nonebot2/nonebot/processor/models.py

38 lines
1.1 KiB
Python
Raw Normal View History

2021-11-15 21:44:24 +08:00
from typing import Any, List, Callable, Optional
2021-11-12 20:55:59 +08:00
2021-11-15 21:44:24 +08:00
from pydantic.fields import ModelField
2021-11-12 20:55:59 +08:00
2021-11-15 21:44:24 +08:00
from nonebot.utils import get_name
2021-11-13 19:38:01 +08:00
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,
2021-11-15 21:44:24 +08:00
params: Optional[List[ModelField]] = None,
2021-11-12 20:55:59 +08:00
dependencies: Optional[List["Dependent"]] = None,
use_cache: bool = True) -> None:
self.func = func
self.name = name
2021-11-15 21:44:24 +08:00
self.params = params or []
2021-11-12 20:55:59 +08:00
self.dependencies = dependencies or []
self.use_cache = use_cache
self.cache_key = self.func