Skip to content

Commit

Permalink
Moved Phasmo stuff into a folder with actions in discrete files
Browse files Browse the repository at this point in the history
  • Loading branch information
howroyd committed Feb 13, 2024
1 parent c412cfd commit 1f20096
Show file tree
Hide file tree
Showing 34 changed files with 1,463 additions and 1,384 deletions.
35 changes: 21 additions & 14 deletions src/simonsays_drgreengiant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import functools
import hashlib
import shutil
from collections.abc import Callable
from collections.abc import Callable, Iterable, MutableMapping
from typing import NoReturn, Self

import tomlkit

from . import environment, errorcodes, gameactions, phasmoactions, twitchactions
from . import environment, errorcodes, gameactions, twitchactions
from .phasmoactions import phasmoactions

OFFLINE = environment.getenvboolean("OFFLINE", False)
NO_BLOCKLIST = environment.getenvboolean("NO_BLOCKLIST", OFFLINE)
Expand All @@ -31,10 +32,12 @@
del _blocklist


def check_blocklist(channel: str | set[str], *, abort: bool = True, silent: bool = False) -> set[str] | NoReturn:
def check_blocklist(channel: str | Iterable[str], *, abort: bool = True, silent: bool = False) -> set[str] | NoReturn:
"""Return any channels/users in the blocklist"""
channels = channel if isinstance(channel, set) else set(channel)
channels = set(channel) if isinstance(channel, str) else channel
assert isinstance(channels, Iterable)
blockedchannels = set(channel for channel in channels if hashlib.sha256(channel.strip().lower().encode("utf-8")).hexdigest() in BLOCKLIST)
assert isinstance(blockedchannels, Iterable)

if blockedchannels:
if not silent:
Expand Down Expand Up @@ -63,17 +66,21 @@ class Config:
config: ConfigDict
version: str
enabled: bool = True
channel: set[str] = dataclasses.field(default_factory=lambda: DEFAULT_CHANNELS)
superusers: set[str] = dataclasses.field(default_factory=lambda: DEFAULT_SUPERUSERS)
channel: Iterable[str] = dataclasses.field(default_factory=lambda: DEFAULT_CHANNELS)
superusers: Iterable[str] = dataclasses.field(default_factory=lambda: DEFAULT_SUPERUSERS)
superuser_prefix: str = DEFAULT_SUPERUSER_COMMAND_PREFIX
bots: set[str] = dataclasses.field(default_factory=lambda: DEFAULT_BOTS)
bots: Iterable[str] = dataclasses.field(default_factory=lambda: DEFAULT_BOTS)
filename: str = DEFAULT_FILENAME
actions: twitchactions.ActionDict = dataclasses.field(init=False, default_factory=dict)

def __post_init__(self):
self.channel = self.channel if isinstance(self.channel, set) else set(self.channel)
self.superusers = self.superusers if isinstance(self.superusers, set) else set(self.superusers)
self.bots = self.bots if isinstance(self.bots, set) else set(self.bots)
self.channel = set(self.channel) if isinstance(self.channel, str) else self.channel
assert isinstance(self.channel, Iterable)
self.superusers = set(self.superusers) if isinstance(self.superusers, str) else self.superusers
assert isinstance(self.superusers, Iterable)
self.bots = set(self.bots) if isinstance(self.bots, set) else self.bots
assert isinstance(self.bots, Iterable)

check_blocklist(self.channel)

actions = self._make_twitch_actions()
Expand All @@ -93,22 +100,22 @@ def find_tag_by_command(self, command: str) -> str | None:
@staticmethod
def root_keys() -> set[str]:
"""Return the root keys"""
return [
return {
"version",
"enabled",
"channel",
"superusers",
"superuser_prefix",
"bots",
"filename",
]
}

def to_dict(self) -> dict:
"""Convert the config to a dict"""
return {key: dataclasses.asdict(item) for key, item in self.config.items()}

@staticmethod
def replace_enum(config: dict) -> dict:
def replace_enum(config: MutableMapping) -> dict:
"""Replace enums with their values"""

def _replace_enum(obj):
Expand All @@ -122,7 +129,7 @@ def _replace_enum(obj):
return {key: {k: _replace_enum(v) for k, v in item.items()} for key, item in config.items()}

@staticmethod
def remove_none(config: dict) -> dict:
def remove_none(config: MutableMapping) -> dict:
"""Remove keys containing None values"""

def _remove_none(obj):
Expand Down
4 changes: 2 additions & 2 deletions src/simonsays_drgreengiant/gameactions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!./.venv/bin/python3
import dataclasses
from collections.abc import Callable
from collections.abc import Callable, Mapping
from typing import Any, Protocol, Self

from . import errorcodes, hidactions
Expand Down Expand Up @@ -52,7 +52,7 @@ def __getitem__(self, name: str) -> ActionConfig | None:
return self.config.get(name, None)

@classmethod
def from_toml(cls, existing: dict[str, dict[str, Any]], *, default: Self | None = None) -> Self:
def from_toml(cls, existing: Mapping[str, Mapping[str, Any]], *, default: Self | None = None) -> Self:
"""Get a config from an existing config"""
ret = default or {}
for key in ret.config.keys():
Expand Down
Loading

0 comments on commit 1f20096

Please sign in to comment.