diff options
-rwxr-xr-x | combine.sh | 2 | ||||
-rw-r--r-- | slack.py | 24 | ||||
-rw-r--r-- | slack/api.py | 14 | ||||
-rw-r--r-- | slack/config.py | 20 | ||||
-rw-r--r-- | slack/globals.py | 19 | ||||
-rw-r--r-- | slack/log.py | 4 | ||||
-rw-r--r-- | slack/main.py | 17 | ||||
-rw-r--r-- | slack/shared.py | 30 | ||||
-rw-r--r-- | slack/task.py | 24 | ||||
-rw-r--r-- | slack/util.py | 4 |
10 files changed, 83 insertions, 75 deletions
@@ -1,6 +1,6 @@ #!/bin/bash -contents="$(cat slack/globals.py slack/log.py slack/util.py slack/task.py slack/http.py slack/api.py slack/config.py slack/main.py slack.py | grep -Ev '^from (\.|slack)' | sed 's/G\.//')" +contents="$(cat slack/*.py slack.py | grep -Ev '^from (\.|slack)')" echo "$contents" | grep '^from __future__' | sort -u > combined.py echo "$contents" | grep -v '^from __future__' | grep -E '^(import|from)' | sort -u >> combined.py @@ -4,32 +4,32 @@ import sys import weechat sys.path.append(os.path.dirname(os.path.realpath(__file__))) -from slack import globals as G # pylint: disable=wrong-import-position from slack.config import SlackConfig from slack.main import init # pylint: disable=wrong-import-position +from slack.shared import shared # pylint: disable=wrong-import-position from slack.task import create_task # pylint: disable=wrong-import-position from slack.util import get_callback_name # pylint: disable=wrong-import-position -G.weechat_callbacks = globals() +shared.weechat_callbacks = globals() def shutdown_cb(): - weechat.config_write(G.config.weechat_config.pointer) + weechat.config_write(shared.config.weechat_config.pointer) return weechat.WEECHAT_RC_OK if __name__ == "__main__": if weechat.register( - G.SCRIPT_NAME, - G.SCRIPT_AUTHOR, - G.SCRIPT_VERSION, - G.SCRIPT_LICENSE, - G.SCRIPT_DESC, + shared.SCRIPT_NAME, + shared.SCRIPT_AUTHOR, + shared.SCRIPT_VERSION, + shared.SCRIPT_LICENSE, + shared.SCRIPT_DESC, get_callback_name(shutdown_cb), "", ): - G.weechat_version = int(weechat.info_get("version_number", "") or 0) - G.workspaces = {} - G.config = SlackConfig() - G.config.config_read() + shared.weechat_version = int(weechat.info_get("version_number", "") or 0) + shared.workspaces = {} + shared.config = SlackConfig() + shared.config.config_read() create_task(init(), final=True) diff --git a/slack/api.py b/slack/api.py index 8b7a377..4cdc53d 100644 --- a/slack/api.py +++ b/slack/api.py @@ -1,19 +1,15 @@ from __future__ import annotations import json -from typing import TYPE_CHECKING, Any, Dict, Union +from typing import TYPE_CHECKING, Dict, Union from urllib.parse import urlencode -from . import globals as G +from slack.shared import shared + from .http import http_request if TYPE_CHECKING: from slack_api import SlackConversation, SlackConversationIm, SlackConversationNotIm -else: - # To support running without slack types - SlackConversation = Any - SlackConversationNotIm = Any - SlackConversationIm = Any class SlackApi: @@ -22,7 +18,7 @@ class SlackApi: def get_request_options(self): return { - "useragent": f"wee_slack {G.SCRIPT_VERSION}", + "useragent": f"wee_slack {shared.SCRIPT_VERSION}", "httpheader": f"Authorization: Bearer {self.workspace.config.api_token.value}", "cookie": self.workspace.config.api_cookies.value, } @@ -56,7 +52,7 @@ class SlackApi: class SlackWorkspace: def __init__(self, name: str): self.name = name - self.config = G.config.create_workspace_config(self.name) + self.config = shared.config.create_workspace_config(self.name) self.api = SlackApi(self) diff --git a/slack/config.py b/slack/config.py index 65dd929..56233a0 100644 --- a/slack/config.py +++ b/slack/config.py @@ -5,9 +5,9 @@ from typing import Generic, TypeVar, Union, cast import weechat -from . import globals as G from .api import SlackWorkspace from .log import print_error +from .shared import shared from .util import get_callback_name @@ -129,7 +129,7 @@ class WeeChatOption(Generic[WeeChatOptionType]): value = None - if G.weechat_version < 0x3050000: + if shared.weechat_version < 0x3050000: default_value = str(self.default_value) value = default_value @@ -237,17 +237,17 @@ def config_section_workspace_read_cb( if not workspace_name or not name: return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR - if workspace_name not in G.workspaces: - G.workspaces[workspace_name] = SlackWorkspace(workspace_name) + if workspace_name not in shared.workspaces: + shared.workspaces[workspace_name] = SlackWorkspace(workspace_name) - option = getattr(G.workspaces[workspace_name].config, name, None) + option = getattr(shared.workspaces[workspace_name].config, name, None) if option is None: return weechat.WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND if not isinstance(option, WeeChatOption): return weechat.WEECHAT_CONFIG_OPTION_SET_ERROR if value is None or ( - G.weechat_version < 0x3080000 + shared.weechat_version < 0x3080000 and value == "" and option.weechat_type != "string" ): @@ -265,7 +265,7 @@ def config_section_workspace_write_for_old_weechat_cb( if not weechat.config_write_line(config_file, section_name, ""): return weechat.WEECHAT_CONFIG_WRITE_ERROR - for workspace in G.workspaces.values(): + for workspace in shared.workspaces.values(): for option in vars(workspace.config).values(): if isinstance(option, WeeChatOption): if ( @@ -293,10 +293,10 @@ class SlackConfig: # WeeChat < 3.8 sends null as an empty string to callback_read, so in # order to distinguish them, don't write the null values to the config # See https://github.com/weechat/weechat/pull/1843 - print("version", G.weechat_version) + print("version", shared.weechat_version) callback_write = ( get_callback_name(config_section_workspace_write_for_old_weechat_cb) - if G.weechat_version < 0x3080000 + if shared.weechat_version < 0x3080000 else "" ) self._section_workspace = WeeChatSection( @@ -313,7 +313,7 @@ class SlackConfig: weechat.config_read(self.weechat_config.pointer) def create_workspace_config(self, workspace_name: str): - if workspace_name in G.workspaces: + if workspace_name in shared.workspaces: raise Exception( f"Failed to create workspace config, already exists: {workspace_name}" ) diff --git a/slack/globals.py b/slack/globals.py deleted file mode 100644 index 73001dd..0000000 --- a/slack/globals.py +++ /dev/null @@ -1,19 +0,0 @@ -from typing import Any, Dict, Tuple - -from .api import SlackWorkspace -from .config import SlackConfig -from .task import Task - -SCRIPT_NAME = "slack" -SCRIPT_AUTHOR = "Trygve Aaberge <trygveaa@gmail.com>" -SCRIPT_VERSION = "3.0.0" -SCRIPT_LICENSE = "MIT" -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] = {} -config: SlackConfig diff --git a/slack/log.py b/slack/log.py index 21b6434..10dc803 100644 --- a/slack/log.py +++ b/slack/log.py @@ -2,7 +2,7 @@ from enum import IntEnum import weechat -from . import globals as G +from .shared import shared class LogLevel(IntEnum): @@ -16,7 +16,7 @@ class LogLevel(IntEnum): # TODO: Figure out what to do with print_error vs log def print_error(message: str): - weechat.prnt("", f"{weechat.prefix('error')}{G.SCRIPT_NAME}: {message}") + weechat.prnt("", f"{weechat.prefix('error')}{shared.SCRIPT_NAME}: {message}") def log(level: LogLevel, message: str): diff --git a/slack/main.py b/slack/main.py index 667e108..9d31eac 100644 --- a/slack/main.py +++ b/slack/main.py @@ -1,20 +1,21 @@ import weechat -from slack import globals as G from slack.config import SlackWorkspace +from .shared import shared + async def init(): - print(G.workspaces) - if "wee-slack-test" not in G.workspaces: - G.workspaces["wee-slack-test"] = SlackWorkspace("wee-slack-test") - G.workspaces[ + 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") - G.workspaces[ + shared.workspaces[ "wee-slack-test" ].config.api_cookies.value = weechat.config_get_plugin("api_cookie") - workspace = G.workspaces["wee-slack-test"] + workspace = shared.workspaces["wee-slack-test"] print(workspace) print(workspace.config.slack_timeout.value) - print(G.config.color.reaction_suffix.value) + print(shared.config.color.reaction_suffix.value) diff --git a/slack/shared.py b/slack/shared.py new file mode 100644 index 0000000..0c2f71b --- /dev/null +++ b/slack/shared.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Dict, Tuple + +if TYPE_CHECKING: + from .api import SlackWorkspace + from .config import SlackConfig + from .task import Task + + +class Shared: + def __init__(self): + self.SCRIPT_NAME = "slack" + self.SCRIPT_AUTHOR = "Trygve Aaberge <trygveaa@gmail.com>" + self.SCRIPT_VERSION = "3.0.0" + self.SCRIPT_LICENSE = "MIT" + self.SCRIPT_DESC = ( + "Extends weechat for typing notification/search/etc on slack.com" + ) + self.REPO_URL = "https://github.com/wee-slack/wee-slack" + + self.weechat_version: int + self.weechat_callbacks: Dict[str, Any] + self.active_tasks: Dict[str, Task[Any]] = {} + self.active_responses: Dict[str, Tuple[Any, ...]] = {} + self.workspaces: Dict[str, SlackWorkspace] = {} + self.config: SlackConfig + + +shared = Shared() diff --git a/slack/task.py b/slack/task.py index db914fb..80687a6 100644 --- a/slack/task.py +++ b/slack/task.py @@ -5,7 +5,7 @@ from uuid import uuid4 import weechat -from . import globals as G +from .shared import shared from .util import get_callback_name T = TypeVar("T") @@ -35,7 +35,7 @@ class Task(Future[T]): def weechat_task_cb(data: str, *args: Any) -> int: - task = G.active_tasks.pop(data) + task = shared.active_tasks.pop(data) task_runner(task, args) return weechat.WEECHAT_RC_OK @@ -44,26 +44,26 @@ def task_runner(task: Task[Any], response: Any): while True: try: future = task.coroutine.send(response) - if future.id in G.active_responses: - response = G.active_responses.pop(future.id) + if future.id in shared.active_responses: + response = shared.active_responses.pop(future.id) else: - if future.id in G.active_tasks: + if future.id in shared.active_tasks: raise Exception( - f"future.id in active_tasks, {future.id}, {G.active_tasks}" + f"future.id in active_tasks, {future.id}, {shared.active_tasks}" ) - G.active_tasks[future.id] = task + shared.active_tasks[future.id] = task break except StopIteration as e: - if task.id in G.active_tasks: - task = G.active_tasks.pop(task.id) + if task.id in shared.active_tasks: + task = shared.active_tasks.pop(task.id) response = e.value else: - if task.id in G.active_responses: + if task.id in shared.active_responses: raise Exception( # pylint: disable=raise-missing-from - f"task.id in active_responses, {task.id}, {G.active_responses}" + f"task.id in active_responses, {task.id}, {shared.active_responses}" ) if not task.final: - G.active_responses[task.id] = e.value + shared.active_responses[task.id] = e.value break diff --git a/slack/util.py b/slack/util.py index acbe5cf..4f9e5dd 100644 --- a/slack/util.py +++ b/slack/util.py @@ -1,8 +1,8 @@ from typing import Any, Callable -from . import globals as G +from .shared import shared def get_callback_name(callback: Callable[..., Any]) -> str: - G.weechat_callbacks[callback.__name__] = callback + shared.weechat_callbacks[callback.__name__] = callback return callback.__name__ |