Skip to content

Commit

Permalink
Create helper object for accessing plugin config
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Mar 22, 2024
1 parent 7f4e183 commit 771eeef
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions scripts/weechatrn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import weechat
import json
from collections import UserDict


class Options(UserDict[str, str]):
"""Helper object for accessing configuration settings."""

def __init__(self):
UserDict[str, str].__init__(self)

def push_tokens(self) -> list[str]:
"""Return comma separated push tokens as an array."""
if script_options["push_tokens"]:
return self.data["push_tokens"].split(",")
return []

def notify_current_buffer(self):
"""Return notify_current_buffer option as boolean."""
return weechat.config_string_to_boolean(self.data["notify_current_buffer"])


script_options_default = {
"push_tokens": (
Expand All @@ -11,19 +30,15 @@
"Option to send notifications for the current buffer",
),
}
script_options: dict[str, str] = {}
script_options = Options()


def weechatrn_cb(data: str, buffer: str, args: str) -> int:
"""
Command to allow managing push tokens without the need to allow /set access
to relay clients.
"""
tokens = (
script_options["push_tokens"].split(",")
if script_options["push_tokens"]
else []
)
tokens = script_options.push_tokens()
if args not in tokens:
tokens.append(args)
weechat.config_set_plugin("push_tokens", ",".join(tokens))
Expand Down Expand Up @@ -58,7 +73,7 @@ def priv_msg_cb(
return weechat.WEECHAT_RC_OK

if (
not weechat.config_string_to_boolean(script_options["notify_current_buffer"])
not script_options.notify_current_buffer()
and weechat.current_buffer() == buffer
):
return weechat.WEECHAT_RC_OK
Expand All @@ -83,11 +98,7 @@ def send_push(title: str, body: str) -> None:
"title": "Notification title",
"body": "Notification body" }]
"""
push_tokens = (
script_options["push_tokens"].split(",")
if script_options["push_tokens"]
else []
)
push_tokens = script_options.push_tokens()
if push_tokens == []:
return

Expand Down Expand Up @@ -125,7 +136,7 @@ def remove_unregistered_devices(response: str) -> None:
except json.JSONDecodeError:
pass
else:
tokens = script_options["push_tokens"].split(",")
tokens = script_options.push_tokens()

for index, status in enumerate(statuses["data"]):
if (
Expand Down

0 comments on commit 771eeef

Please sign in to comment.