2024-12-01 12:31:11 +08:00
|
|
|
|
from collections.abc import ItemsView, Iterator, KeysView, MutableMapping, ValuesView
|
|
|
|
|
from typing import TYPE_CHECKING, Optional, TypeVar, Union, overload
|
2022-11-21 18:44:55 +08:00
|
|
|
|
|
|
|
|
|
from .provider import DEFAULT_PROVIDER_CLASS, MatcherProvider
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from .matcher import Matcher
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
class MatcherManager(MutableMapping[int, list[type["Matcher"]]]):
|
2022-11-21 18:44:55 +08:00
|
|
|
|
"""事件响应器管理器
|
|
|
|
|
|
|
|
|
|
实现了常用字典操作,用于管理事件响应器。
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.provider: MatcherProvider = DEFAULT_PROVIDER_CLASS({})
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"MatcherManager(provider={self.provider!r})"
|
|
|
|
|
|
|
|
|
|
def __contains__(self, o: object) -> bool:
|
|
|
|
|
return o in self.provider
|
|
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[int]:
|
|
|
|
|
return iter(self.provider)
|
|
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
|
return len(self.provider)
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def __getitem__(self, key: int) -> list[type["Matcher"]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider[key]
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def __setitem__(self, key: int, value: list[type["Matcher"]]) -> None:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
self.provider[key] = value
|
|
|
|
|
|
|
|
|
|
def __delitem__(self, key: int) -> None:
|
|
|
|
|
del self.provider[key]
|
|
|
|
|
|
2023-08-18 19:07:35 +08:00
|
|
|
|
def __eq__(self, other: object) -> bool:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return isinstance(other, MatcherManager) and self.provider == other.provider
|
|
|
|
|
|
|
|
|
|
def keys(self) -> KeysView[int]:
|
|
|
|
|
return self.provider.keys()
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def values(self) -> ValuesView[list[type["Matcher"]]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.values()
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def items(self) -> ItemsView[int, list[type["Matcher"]]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.items()
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get(self, key: int) -> Optional[list[type["Matcher"]]]: ...
|
2022-11-21 18:44:55 +08:00
|
|
|
|
|
|
|
|
|
@overload
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get(self, key: int, default: T) -> Union[list[type["Matcher"]], T]: ...
|
2022-11-21 18:44:55 +08:00
|
|
|
|
|
|
|
|
|
def get(
|
|
|
|
|
self, key: int, default: Optional[T] = None
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> Optional[Union[list[type["Matcher"]], T]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.get(key, default)
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def pop( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
|
|
|
self, key: int
|
|
|
|
|
) -> list[type["Matcher"]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.pop(key)
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def popitem(self) -> tuple[int, list[type["Matcher"]]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.popitem()
|
|
|
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
|
self.provider.clear()
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def update( # pyright: ignore[reportIncompatibleMethodOverride]
|
2024-12-01 12:31:11 +08:00
|
|
|
|
self, m: MutableMapping[int, list[type["Matcher"]]], /
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> None:
|
2024-12-01 12:31:11 +08:00
|
|
|
|
self.provider.update(m)
|
2022-11-21 18:44:55 +08:00
|
|
|
|
|
|
|
|
|
def setdefault(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, key: int, default: list[type["Matcher"]]
|
|
|
|
|
) -> list[type["Matcher"]]:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
return self.provider.setdefault(key, default)
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def set_provider(self, provider_class: type[MatcherProvider]) -> None:
|
2022-11-21 18:44:55 +08:00
|
|
|
|
"""设置事件响应器存储器
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
provider_class: 事件响应器存储器类
|
|
|
|
|
"""
|
|
|
|
|
self.provider = provider_class(self.provider)
|