diff --git a/simplematrixbotlib/config.py b/simplematrixbotlib/config.py index dda852e..5902730 100644 --- a/simplematrixbotlib/config.py +++ b/simplematrixbotlib/config.py @@ -1,29 +1,24 @@ from dataclasses import dataclass, field import toml import re -from typing import Set, Union +from typing import Set @dataclass class Config: _join_on_invite: bool = True _allowlist: Set[re.Pattern] = field( - default_factory=set) #TODO: default to bot's homeserver + default_factory=set) # TODO: default to bot's homeserver _blocklist: Set[re.Pattern] = field(default_factory=set) - def _check_set_regex(self, - value: Set[str]) -> Union[Set[re.Pattern], None]: - new_list = set() + @staticmethod + def _check_set_regex(value: Set[str]): + """Checks if the patterns in value are valid or throws an error""" for v in value: try: - tmp = re.compile(v) + re.compile(v) # Fails for invalid regex except re.error: - print( - f"{v} is not a valid regular expression. Ignoring your list update." - ) - return None - new_list.add(tmp) - return new_list + raise re.error(f"{v} is not a valid regex.") def _load_config_dict(self, config_dict: dict) -> None: for key, value in config_dict.items(): @@ -80,34 +75,32 @@ def allowlist(self) -> Set[re.Pattern]: @allowlist.setter def allowlist(self, value: Set[str]) -> None: - checked = self._check_set_regex(value) - if checked is None: - return - self._allowlist = checked + self._check_set_regex(value) + self._allowlist = value - def add_allowlist(self, value: Set[str]) -> None: + def add_allowlist(self, value: Set[str]): """ + Adds all regex to the allowlist if valid, else throws error + Parameters ---------- value : Set[str] A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be added to allowlist. """ - checked = self._check_set_regex(value) - if checked is None: - return - self._allowlist = self._allowlist.union(checked) + self._check_set_regex(value) + self._allowlist = self._allowlist.union(value) - def remove_allowlist(self, value: Set[str]) -> None: + def remove_allowlist(self, value: Set[str]): """ + Removes all in the value set from the allowlist or throws an error + Parameters ---------- value : Set[str] A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be removed from allowlist. """ - checked = self._check_set_regex(value) - if checked is None: - return - self._allowlist = self._allowlist - checked + self._check_set_regex(value) + self._allowlist = self._allowlist - value @property def blocklist(self) -> Set[re.Pattern]: @@ -122,10 +115,8 @@ def blocklist(self) -> Set[re.Pattern]: @blocklist.setter def blocklist(self, value: Set[str]) -> None: - checked = self._check_set_regex(value) - if checked is None: - return - self._blocklist = checked + self._check_set_regex(value) + self._blocklist = value def add_blocklist(self, value: Set[str]) -> None: """ @@ -134,10 +125,8 @@ def add_blocklist(self, value: Set[str]) -> None: value : Set[str] A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be added to blocklist. """ - checked = self._check_set_regex(value) - if checked is None: - return - self._blocklist = self._blocklist.union(checked) + self._check_set_regex(value) + self._blocklist = self._blocklist.union(value) def remove_blocklist(self, value: Set[str]) -> None: """ @@ -146,7 +135,5 @@ def remove_blocklist(self, value: Set[str]) -> None: value : Set[str] A set of strings which represent Matrix IDs or a regular expression matching Matrix IDs to be removed from blocklist. """ - checked = self._check_set_regex(value) - if checked is None: - return - self._blocklist = self._blocklist - checked + self._check_set_regex(value) + self._blocklist = self._blocklist - value diff --git a/tests/config/sample_config_files/config5.toml b/tests/config/sample_config_files/config5.toml new file mode 100644 index 0000000..e0be816 --- /dev/null +++ b/tests/config/sample_config_files/config5.toml @@ -0,0 +1,3 @@ +[simplematrixbotlib.config] +allowlist = ["[."] +blocklist = ["@test2:example\\.org"] diff --git a/tests/config/test_config.py b/tests/config/test_config.py index ac42796..8adba1d 100644 --- a/tests/config/test_config.py +++ b/tests/config/test_config.py @@ -69,3 +69,14 @@ def test_manual_set(): assert config.blocklist == set(map(re.compile, ['@test:matrix\\.org'])) config.blocklist = {'*:example\\.org'} assert config.blocklist == set(map(re.compile, ['@test:matrix\\.org'])) + + +def test_botlib(): + config = botlib.Config() + with pytest.raises(re.error): + config._check_set_regex(set(r"[.")) + # Valid patterns + config._check_set_regex(set(r"@testuser:example.org")) + + with pytest.raises(re.error): + config.load_toml(os.path.join(sample_config_path, 'config5.toml'))