Skip to content

Commit

Permalink
[melobot] Add PluginPlanner decorator "use" to simplify components co…
Browse files Browse the repository at this point in the history
…llecting
  • Loading branch information
aicorein committed Dec 10, 2024
1 parent 4508081 commit ecec685
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/melobot/plugin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Iterable, final
from typing import Iterable, final, overload

from .._hook import HookBus
from ..ctx import BotCtx
Expand All @@ -16,6 +16,7 @@
Callable,
P,
SingletonBetterABCMeta,
T,
abstractattr,
deprecate_warn,
)
Expand Down Expand Up @@ -112,6 +113,27 @@ def wrapped(func: AsyncCallable[P, None]) -> AsyncCallable[P, None]:

return wrapped

@overload
def use(self, obj: Flow) -> Flow: ...
@overload
def use(self, obj: SyncShare[T]) -> SyncShare[T]: ... # type: ignore[overload-overlap]
@overload
def use(self, obj: AsyncShare[T]) -> AsyncShare[T]: ... # type: ignore[overload-overlap]
@overload
def use(self, obj: Callable[P, T]) -> Callable[P, T]: ...

@final
def use(self, obj: T) -> T:
if isinstance(obj, Flow):
self.flows.append(obj)
elif isinstance(obj, (SyncShare, AsyncShare)):
self.shares.append(obj)
elif callable(obj):
self.funcs.append(obj)
else:
raise PluginLoadError(f"插件无法使用并使用 {type(obj)} 类型的对象")
return obj

@final
def _build(self, name: str) -> Plugin:
if not self._built:
Expand Down

0 comments on commit ecec685

Please sign in to comment.