Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: 改进 CommandGroupMatcherGroup 的结构 #1240

Merged
merged 7 commits into from
Sep 10, 2022
142 changes: 63 additions & 79 deletions nonebot/plugin/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,33 @@ def on_type(
return on(rule=is_type(*event_types) & rule, **kwargs, _depth=_depth + 1)


class CommandGroup:
class _Group:
def __init__(self, **kwargs):
"""创建一个事件响应器组合,参数为默认值,与 `on` 一致"""
self.matchers: List[Type[Matcher]] = []
"""组内事件响应器列表"""
self.base_kwargs: Dict[str, Any] = kwargs
"""其他传递给 `on` 的参数默认值"""

def _get_final_kwargs(
self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None
) -> Dict[str, Any]:
"""获取最终传递给 `on` 的参数

参数:
update: 更新的关键字参数
exclude: 需要排除的参数
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(update)
if exclude:
for key in exclude:
final_kwargs.pop(key, None)
final_kwargs["_depth"] = 1
return final_kwargs


class CommandGroup(_Group):
"""命令组,用于声明一组有相同名称前缀的命令。

参数:
Expand All @@ -478,15 +504,13 @@ class CommandGroup:
"""

def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs):
self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
"""命令前缀"""
if "aliases" in kwargs:
del kwargs["aliases"]
self.base_kwargs: Dict[str, Any] = kwargs
"""其他传递给 `on_command` 的参数默认值"""
super().__init__(**kwargs)
self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
self.base_kwargs.pop("aliases", None)

def __repr__(self) -> str:
return f"CommandGroup(cmd={self.basecmd})"
return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})"

def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]:
"""注册一个新的命令。新参数将会覆盖命令组默认值
Expand All @@ -505,10 +529,9 @@ def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]:
"""
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
cmd = self.basecmd + sub_cmd

final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
return on_command(cmd, **final_kwargs, _depth=1)
matcher = on_command(cmd, **self._get_final_kwargs(kwargs))
self.matchers.append(matcher)
return matcher

def shell_command(
self, cmd: Union[str, Tuple[str, ...]], **kwargs
Expand All @@ -530,22 +553,14 @@ def shell_command(
"""
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
cmd = self.basecmd + sub_cmd

final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
return on_shell_command(cmd, **final_kwargs, _depth=1)
matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs))
self.matchers.append(matcher)
return matcher


class MatcherGroup:
class MatcherGroup(_Group):
"""事件响应器组合,统一管理。为 `Matcher` 创建提供默认属性。"""

def __init__(self, **kwargs):
"""创建一个事件响应器组合,参数为默认值,与 `on` 一致"""
self.matchers: List[Type[Matcher]] = []
"""组内事件响应器列表"""
self.base_kwargs: Dict[str, Any] = kwargs
"""其他传递给 `on` 的参数默认值"""

def __repr__(self) -> str:
return f"MatcherGroup(matchers={len(self.matchers)})"

Expand All @@ -563,9 +578,7 @@ def on(self, **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
matcher = on(**final_kwargs, _depth=1)
matcher = on(**self._get_final_kwargs(kwargs))
self.matchers.append(matcher)
return matcher

Expand All @@ -581,11 +594,8 @@ def on_metaevent(self, **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
final_kwargs.pop("permission", None)
matcher = on_metaevent(**final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
matcher = on_metaevent(**final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -602,10 +612,8 @@ def on_message(self, **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_message(**final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_message(**final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -621,11 +629,8 @@ def on_notice(self, **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
final_kwargs.pop("permission", None)
matcher = on_notice(**final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
matcher = on_notice(**final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -641,11 +646,8 @@ def on_request(self, **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
final_kwargs.pop("permission", None)
matcher = on_request(**final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
matcher = on_request(**final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -666,10 +668,8 @@ def on_startswith(
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_startswith(msg, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_startswith(msg, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -688,10 +688,8 @@ def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matche
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_endswith(msg, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_endswith(msg, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -710,10 +708,8 @@ def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Match
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_fullmatch(msg, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_fullmatch(msg, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -731,10 +727,8 @@ def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]:
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_keyword(keywords, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_keyword(keywords, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -760,10 +754,8 @@ def on_command(
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_command(cmd, aliases=aliases, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_command(cmd, aliases=aliases, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand Down Expand Up @@ -793,12 +785,8 @@ def on_shell_command(
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_shell_command(
cmd, aliases=aliases, parser=parser, **final_kwargs, _depth=1
)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_shell_command(cmd, aliases=aliases, parser=parser, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -821,10 +809,8 @@ def on_regex(
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_regex(pattern, flags=flags, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_regex(pattern, flags=flags, **final_kwargs)
self.matchers.append(matcher)
return matcher

Expand All @@ -844,9 +830,7 @@ def on_type(
block: 是否阻止事件向更低优先级传递
state: 默认 state
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = on_type(types, **final_kwargs, _depth=1)
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
matcher = on_type(types, **final_kwargs)
self.matchers.append(matcher)
return matcher