From fd49a272ae8c23dbf393e9f975c7945f1ad85e9a Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Mon, 25 Jun 2018 22:49:15 +0800 Subject: [PATCH] Add CommandGroup class --- none/command.py | 18 ++++++++++++++++++ none/plugins/weather.py | 9 +++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/none/command.py b/none/command.py index c433d4df..0227d9cc 100644 --- a/none/command.py +++ b/none/command.py @@ -106,6 +106,24 @@ def on_command(name: Union[str, Tuple[str]], *, return deco +class CommandGroup: + __slots__ = ('basename', 'permission') + + def __init__(self, name: Union[str, Tuple[str]], permission: int = None): + self.basename = name if isinstance(name, tuple) else (name,) + self.permission = permission + + def command(self, name: Union[str, Tuple[str]], *, + aliases: Iterable = None, permission: int = None) -> Callable: + name = self.basename + (name if isinstance(name, tuple) else (name,)) + kwargs = {} + if aliases is not None: + kwargs['aliases'] = aliases + if permission is not None: + kwargs['permission'] = permission + return on_command(name, **kwargs) + + def _find_command(name: Union[str, Tuple[str]]) -> Optional[Command]: cmd_name = name if isinstance(name, tuple) else (name,) diff --git a/none/plugins/weather.py b/none/plugins/weather.py index bbb71f83..2ca82b62 100644 --- a/none/plugins/weather.py +++ b/none/plugins/weather.py @@ -1,9 +1,11 @@ import none -from none.command import Session +from none.command import Session, CommandGroup from none.expressions import weather as expr +w = CommandGroup('weather') -@none.on_command(('weather', 'weather'), aliases=('天气', '天气预报')) + +@w.command('weather', aliases=('天气', '天气预报')) async def weather(session: Session): city = session.require_arg('city', prompt_expr=expr.WHICH_CITY) await session.send_expr(expr.REPORT, city=city) @@ -15,7 +17,6 @@ async def _(session: Session): session.args[session.current_key] = session.current_arg.strip() -@none.on_command(('weather', 'suggestion'), - aliases=('生活指数', '生活建议', '生活提示')) +@w.command('suggestion', aliases=('生活指数', '生活建议', '生活提示')) async def suggestion(session: Session): await session.send('suggestion')