From b876ce7eae911f946df5892438f826e1e4afb5c5 Mon Sep 17 00:00:00 2001 From: zzbslayer Date: Mon, 28 Sep 2020 15:06:24 +0800 Subject: [PATCH] feat: mark platform-specific sv function by decorator --- .../pcrclanbattle/clanbattle/__init__.py | 2 + kokkoro/platform_patch.py | 42 +++++++++++++++++++ kokkoro/service.py | 9 +++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/kokkoro/modules/pcrclanbattle/clanbattle/__init__.py b/kokkoro/modules/pcrclanbattle/clanbattle/__init__.py index ee62e5a..244c0c6 100644 --- a/kokkoro/modules/pcrclanbattle/clanbattle/__init__.py +++ b/kokkoro/modules/pcrclanbattle/clanbattle/__init__.py @@ -1,5 +1,6 @@ # 公主连接Re:Dive会战管理插件 # clan == クラン == 戰隊(直译为氏族)(CLANNAD的CLAN(笑)) +from functools import wraps from .argparse import ArgParser from .exception import * @@ -23,6 +24,7 @@ def cb_cmd(prefixes, parser:ArgParser) -> Callable: raise ValueError('`name` of cb_cmd must be `str` or `Iterable[str]`') def deco(func): + @wraps(func) async def wrapper(bot: KokkoroBot, ev: EventInterface): try: args = parser.parse(ev.get_param().args, ev) diff --git a/kokkoro/platform_patch.py b/kokkoro/platform_patch.py index a52f106..6fceb36 100644 --- a/kokkoro/platform_patch.py +++ b/kokkoro/platform_patch.py @@ -1,3 +1,4 @@ +import kokkoro from kokkoro import config def process_mention_me(raw_msg:str): @@ -25,6 +26,47 @@ def preprocess_message(ev) -> bool: ev.set_content(processed_msg) return to_me +PLATFORM_FIELD = '__kkr_platform__' + +def discord(func): + func.__setattr__(PLATFORM_FIELD, ['discord']) + return func + +def tomon(func): + func.__setattr__(PLATFORM_FIELD, ['tomon']) + return func + +def telegram(func): + func.__setattr__(PLATFORM_FIELD, ['telegram']) + return func + +def platform(pl): + if type(pl) == str: + pl = [pl] + def deco(func): + func.__setattr__(PLATFORM_FIELD, pl) + return func + return deco + +def is_supported(func): + try: + supported_platforms = func.__getattribute__(PLATFORM_FIELD) + except AttributeError as e: + #supported_platforms = "*" + return True + platform = config.BOT_TYPE + if platform in supported_platforms: + return True + return False + +def check_platform(on_deco): + def wrapper(sv_func): + if is_supported(sv_func): + return on_deco(sv_func) + else: + kokkoro.logger.warning(f'Function {sv_func.__name__} isn\'t supported in current platform') + return sv_func + return wrapper \ No newline at end of file diff --git a/kokkoro/service.py b/kokkoro/service.py index 556ac8b..12dad71 100644 --- a/kokkoro/service.py +++ b/kokkoro/service.py @@ -6,12 +6,13 @@ from collections import defaultdict import kokkoro -from kokkoro.typing import * from kokkoro import logger from kokkoro import priv, log, typing, trigger +from kokkoro.typing import * from kokkoro.common_interface import * from kokkoro.bot import get_scheduler, get_bot from kokkoro.util import join_iterable +from kokkoro.platform_patch import check_platform # service management _loaded_services: Dict[str, "Service"] = {} # {name: service} @@ -190,6 +191,7 @@ def get_enable_groups(self) -> dict: def on_prefix(self, prefix, only_to_me=False) -> Callable: if isinstance(prefix, str): prefix = (prefix, ) + @check_platform def deco(func) -> Callable: sf = ServiceFunc(self, func, only_to_me) for p in prefix: @@ -200,6 +202,7 @@ def deco(func) -> Callable: def on_fullmatch(self, word, only_to_me=False) -> Callable: if isinstance(word, str): word = (word, ) + @check_platform def deco(func) -> Callable: @wraps(func) async def wrapper(bot: KokkoroBot, ev: EventInterface): @@ -226,6 +229,7 @@ async def wrapper(bot: KokkoroBot, ev: EventInterface): def on_suffix(self, suffix, only_to_me=False) -> Callable: if isinstance(suffix, str): suffix = (suffix, ) + @check_platform def deco(func) -> Callable: sf = ServiceFunc(self, func, only_to_me) for s in suffix: @@ -236,6 +240,7 @@ def deco(func) -> Callable: def on_keyword(self, keywords, only_to_me=False) -> Callable: if isinstance(keywords, str): keywords = (keywords, ) + @check_platform def deco(func) -> Callable: sf = ServiceFunc(self, func, only_to_me) for kw in keywords: @@ -246,6 +251,7 @@ def deco(func) -> Callable: def on_rex(self, rex: Union[str, re.Pattern], only_to_me=False) -> Callable: if isinstance(rex, str): rex = re.compile(rex) + @check_platform def deco(func) -> Callable: sf = ServiceFunc(self, func, only_to_me) trigger.rex.add(rex, sf) @@ -261,6 +267,7 @@ def scheduled_job(self, *args, **kwargs) -> Callable: else: kwargs.setdefault('misfire_grace_time', 60) + @check_platform def deco(func: Callable) -> Callable: kokkoro.logger.debug(f'{func.__name__} registered to scheduler') @wraps(func)