diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2022-11-19 21:48:27 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 1358241a612980781bc7f80dec67bc8de74a3903 (patch) | |
tree | 3aa8b4be8cb7428d997881b54c3f630aa9445b8d | |
parent | 0c599d0cd601bd491f4270a8ababab0ffe70b327 (diff) | |
download | wee-slack-1358241a612980781bc7f80dec67bc8de74a3903.tar.gz |
Add command to add workspace
-rw-r--r-- | pyproject.toml | 9 | ||||
-rw-r--r-- | slack/commands.py | 121 | ||||
-rw-r--r-- | slack/init.py | 29 | ||||
-rw-r--r-- | tests/test_commands.py | 13 |
4 files changed, 155 insertions, 17 deletions
diff --git a/pyproject.toml b/pyproject.toml index 145896e..ce9df58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,14 +30,15 @@ ignored-modules = ["weechat"] [tool.pylint."messages control"] disable = [ - "dangerous-default-value", # inconvenient with types + "dangerous-default-value", # inconvenient with types + "inconsistent-return-statements", # no need when using typing "invalid-name", "missing-class-docstring", "missing-function-docstring", "missing-module-docstring", - "no-member", # incorrect reports - "protected-access", # covered by pyright - "too-few-public-methods", # too often bad advice + "no-member", # incorrect reports + "protected-access", # covered by pyright + "too-few-public-methods", # too often bad advice "too-many-arguments", "too-many-instance-attributes", ] diff --git a/slack/commands.py b/slack/commands.py new file mode 100644 index 0000000..547ac59 --- /dev/null +++ b/slack/commands.py @@ -0,0 +1,121 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass +from functools import wraps +from typing import Callable, Dict, List + +import weechat + +from slack.api import SlackWorkspace +from slack.log import print_error +from slack.shared import shared +from slack.util import get_callback_name + +commands: Dict[str, Command] = {} + + +# def parse_help_docstring(cmd): +# doc = textwrap.dedent(cmd.__doc__).strip().split("\n", 1) +# cmd_line = doc[0].split(None, 1) +# args = "".join(cmd_line[1:]) +# return cmd_line[0], args, doc[1].strip() + + +@dataclass +class Command: + cmd: str + top_level: bool + description: str + args: str + args_description: str + completion: str + cb: Callable[[str, str], None] + + +def weechat_command(min_args: int = 0, slack_buffer_required: bool = False): + def decorator(f: Callable[[str, List[str]], None]): + cmd = f.__name__.removeprefix("command_").replace("_", " ") + top_level = " " not in cmd + + @wraps(f) + def wrapper(buffer: str, args: str): + split_args = args.split(" ", min_args) + if min_args and not args or len(split_args) < min_args: + print_error( + f'Too few arguments for command "/{cmd}" (help on command: /help {cmd})' + ) + return + return f(buffer, split_args) + + commands[cmd] = Command(cmd, top_level, "", "", "", "", wrapper) + + return wrapper + + return decorator + + +@weechat_command() +def command_slack(buffer: str, args: List[str]): + """ + slack command + """ + print("ran slack") + + +@weechat_command() +def command_slack_workspace(buffer: str, args: List[str]): + print("ran workspace") + + +@weechat_command(min_args=1) +def command_slack_workspace_add(buffer: str, args: List[str]): + name = args[0] + if name in shared.workspaces: + print_error(f'workspace "{name}" already exists, can\'t add it!') + return + shared.workspaces[name] = SlackWorkspace(name) + weechat.prnt( + "", + f"{shared.SCRIPT_NAME}: workspace added: {weechat.color('chat_server')}{name}{weechat.color('reset')}", + ) + + +def command_cb(data: str, buffer: str, args: str) -> int: + args_parts = re.finditer("[^ ]+", args) + cmd = data + cmd_args_startpos = 0 + + for part in args_parts: + next_cmd = f"{cmd} {part.group(0)}" + if next_cmd not in commands: + cmd_args_startpos = part.start(0) + break + cmd = next_cmd + else: + cmd_args_startpos = len(args) + + cmd_args = args[cmd_args_startpos:] + + if cmd in commands: + commands[cmd].cb(buffer, cmd_args) + else: + print_error( + f'Error with command "/{data} {args}" (help on command: /help {data})' + ) + + return weechat.WEECHAT_RC_OK + + +def register_commands(): + for cmd, command in commands.items(): + if command.top_level: + weechat.hook_command( + command.cmd, + command.description, + command.args, + command.args_description, + command.completion, + get_callback_name(command_cb), + cmd, + ) diff --git a/slack/init.py b/slack/init.py index 8e1a433..ff95f57 100644 --- a/slack/init.py +++ b/slack/init.py @@ -1,5 +1,6 @@ import weechat +from slack.commands import register_commands from slack.config import SlackConfig, SlackWorkspace from slack.shared import shared from slack.task import create_task @@ -17,19 +18,20 @@ def shutdown_cb(): async def init(): - print(shared.workspaces) - if "wee-slack-test" not in shared.workspaces: - shared.workspaces["wee-slack-test"] = SlackWorkspace("wee-slack-test") - shared.workspaces[ - "wee-slack-test" - ].config.api_token.value = weechat.config_get_plugin("api_token") - shared.workspaces[ - "wee-slack-test" - ].config.api_cookies.value = weechat.config_get_plugin("api_cookie") - workspace = shared.workspaces["wee-slack-test"] - print(workspace) - print(workspace.config.slack_timeout.value) - print(shared.config.color.reaction_suffix.value) + pass + # print(shared.workspaces) + # if "wee-slack-test" not in shared.workspaces: + # shared.workspaces["wee-slack-test"] = SlackWorkspace("wee-slack-test") + # shared.workspaces[ + # "wee-slack-test" + # ].config.api_token.value = weechat.config_get_plugin("api_token") + # shared.workspaces[ + # "wee-slack-test" + # ].config.api_cookies.value = weechat.config_get_plugin("api_cookie") + # workspace = shared.workspaces["wee-slack-test"] + # print(workspace) + # print(workspace.config.slack_timeout.value) + # print(shared.config.color.reaction_suffix.value) def main(): @@ -46,4 +48,5 @@ def main(): shared.workspaces = {} shared.config = SlackConfig() shared.config.config_read() + register_commands() create_task(init(), final=True) diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..c9dbcaa --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,13 @@ +# Test parsing options +# Test calling the correct function + +from itertools import accumulate + +import slack.commands + + +def test_all_parent_commands_exist(): + for command in slack.commands.commands: + parents = accumulate(command.split(" "), lambda x, y: f"{x} {y}") + for parent in parents: + assert parent in slack.commands.commands |