Skip to content

Commit

Permalink
feat: mark platform-specific sv function by decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
zzbslayer committed Sep 28, 2020
1 parent 5ff3fbf commit ace6715
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kokkoro/modules/pcrclanbattle/clanbattle/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 公主连接Re:Dive会战管理插件
# clan == クラン == 戰隊(直译为氏族)(CLANNAD的CLAN(笑))
from functools import wraps

from .argparse import ArgParser
from .exception import *
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions kokkoro/platform_patch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import kokkoro
from kokkoro import config

def process_mention_me(raw_msg:str):
Expand Down Expand Up @@ -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'`{sv_func.__name__}` isn\'t supported in current platform')
return sv_func
return wrapper



9 changes: 8 additions & 1 deletion kokkoro/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit ace6715

Please sign in to comment.