diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2022-10-24 22:29:03 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:52 +0100 |
commit | 4b004a356aec12eb59fb4c5a208c21d3817ef9d7 (patch) | |
tree | 41ab637631d19f890afae937e42f03e8a339c1cd /slack | |
parent | ee9d976ff8d8910c091d1554dfa62ab28ad67509 (diff) | |
download | wee-slack-4b004a356aec12eb59fb4c5a208c21d3817ef9d7.tar.gz |
Add callbacks to global scope
Diffstat (limited to 'slack')
-rw-r--r-- | slack/config.py | 5 | ||||
-rw-r--r-- | slack/globals.py | 1 | ||||
-rw-r--r-- | slack/main.py | 5 | ||||
-rw-r--r-- | slack/task.py | 5 | ||||
-rw-r--r-- | slack/util.py | 8 | ||||
-rw-r--r-- | slack/weechat_http.py | 4 |
6 files changed, 23 insertions, 5 deletions
diff --git a/slack/config.py b/slack/config.py index 8584fca..a2ab654 100644 --- a/slack/config.py +++ b/slack/config.py @@ -7,6 +7,7 @@ import globals as G import weechat from api import SlackWorkspace from log import print_error +from util import get_callback_name class WeeChatColor(str): @@ -293,14 +294,14 @@ class SlackConfig: # See https://github.com/weechat/weechat/pull/1843 print("version", G.weechat_version) callback_write = ( - config_section_workspace_write_for_old_weechat_cb.__name__ + get_callback_name(config_section_workspace_write_for_old_weechat_cb) if G.weechat_version < 0x3080000 else "" ) self._section_workspace = WeeChatSection( self.weechat_config, "workspace", - callback_read=config_section_workspace_read_cb.__name__, + callback_read=get_callback_name(config_section_workspace_read_cb), callback_write=callback_write, ) self._workspace_default = SlackConfigSectionWorkspace( diff --git a/slack/globals.py b/slack/globals.py index 30ffbd1..8cdd9a3 100644 --- a/slack/globals.py +++ b/slack/globals.py @@ -12,6 +12,7 @@ SCRIPT_DESC = "Extends weechat for typing notification/search/etc on slack.com" REPO_URL = "https://github.com/wee-slack/wee-slack" weechat_version: int +weechat_callbacks: Dict[str, Any] active_tasks: Dict[str, Task[Any]] = {} active_responses: Dict[str, Tuple[Any, ...]] = {} workspaces: Dict[str, SlackWorkspace] = {} diff --git a/slack/main.py b/slack/main.py index 32dc3d5..5ab431c 100644 --- a/slack/main.py +++ b/slack/main.py @@ -9,6 +9,9 @@ sys.path.append(os.path.dirname(os.path.realpath(__file__))) import globals as G # pylint: disable=wrong-import-position from config import SlackConfig, SlackWorkspace # pylint: disable=wrong-import-position from task import create_task # pylint: disable=wrong-import-position +from util import get_callback_name # pylint: disable=wrong-import-position + +G.weechat_callbacks = globals() def shutdown_cb(): @@ -39,7 +42,7 @@ if __name__ == "__main__": G.SCRIPT_VERSION, G.SCRIPT_LICENSE, G.SCRIPT_DESC, - shutdown_cb.__name__, + get_callback_name(shutdown_cb), "", ): G.weechat_version = int(weechat.info_get("version_number", "") or 0) diff --git a/slack/task.py b/slack/task.py index d9ba1cb..c6c72f6 100644 --- a/slack/task.py +++ b/slack/task.py @@ -5,6 +5,7 @@ from uuid import uuid4 import globals as G import weechat +from util import get_callback_name T = TypeVar("T") @@ -75,5 +76,7 @@ def create_task( async def sleep(milliseconds: int): future = FutureTimer() - weechat.hook_timer(milliseconds, 0, 1, weechat_task_cb.__name__, future.id) + weechat.hook_timer( + milliseconds, 0, 1, get_callback_name(weechat_task_cb), future.id + ) return await future diff --git a/slack/util.py b/slack/util.py new file mode 100644 index 0000000..10d4ce9 --- /dev/null +++ b/slack/util.py @@ -0,0 +1,8 @@ +from typing import Any, Callable + +import globals as G + + +def get_callback_name(callback: Callable[..., Any]) -> str: + G.weechat_callbacks[callback.__name__] = callback + return callback.__name__ diff --git a/slack/weechat_http.py b/slack/weechat_http.py index df7943d..61e6c38 100644 --- a/slack/weechat_http.py +++ b/slack/weechat_http.py @@ -5,9 +5,11 @@ import resource from io import StringIO from typing import Dict +import globals as G import weechat from log import LogLevel, log from task import FutureProcess, sleep, weechat_task_cb +from util import get_callback_name class HttpError(Exception): @@ -34,7 +36,7 @@ async def hook_process_hashtable(command: str, options: Dict[str, str], timeout: while available_file_descriptors() < 10: await sleep(10) weechat.hook_process_hashtable( - command, options, timeout, weechat_task_cb.__name__, future.id + command, options, timeout, get_callback_name(weechat_task_cb), future.id ) stdout = StringIO() |