From 8bc42d20fbb507b48375d65b2f8156e8cbfd1129 Mon Sep 17 00:00:00 2001 From: MattHag <16444067+MattHag@users.noreply.github.com> Date: Sun, 3 Nov 2024 22:53:50 +0100 Subject: [PATCH] Enforce rules on RuleComponentUI subclasses Enforce create_widgets and collect_values. Related #2273 --- lib/solaar/ui/diversion_rules.py | 4 ++-- lib/solaar/ui/rule_base.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/solaar/ui/diversion_rules.py b/lib/solaar/ui/diversion_rules.py index f6660cda..788eab24 100644 --- a/lib/solaar/ui/diversion_rules.py +++ b/lib/solaar/ui/diversion_rules.py @@ -1818,7 +1818,7 @@ class SettingUI(_SettingWithValueUI, ConditionUI): _SettingWithValueUI._on_update(self, *_args) -COMPONENT_UI = { +COMPONENT_UI: dict[Any, RuleComponentUI] = { diversion.Rule: RuleUI, diversion.Not: NotUI, diversion.Or: OrUI, @@ -1843,7 +1843,7 @@ COMPONENT_UI = { diversion.MouseClick: rule_actions.MouseClickUI, diversion.Execute: rule_actions.ExecuteUI, diversion.Set: SetUI, - type(None): RuleComponentUI, # placeholders for empty rule/And/Or + # type(None): RuleComponentUI, # placeholders for empty rule/And/Or } _all_devices = AllDevicesInfo() diff --git a/lib/solaar/ui/rule_base.py b/lib/solaar/ui/rule_base.py index d7096727..1fcc5dcd 100644 --- a/lib/solaar/ui/rule_base.py +++ b/lib/solaar/ui/rule_base.py @@ -13,8 +13,10 @@ ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import abc from contextlib import contextmanager as contextlib_contextmanager +from typing import Any from typing import Callable from gi.repository import Gtk @@ -47,7 +49,7 @@ class CompletionEntry(Gtk.Entry): liststore.append((v,)) -class RuleComponentUI: +class RuleComponentUI(abc.ABC): CLASS = diversion.RuleComponent def __init__(self, panel, on_update: Callable = None): @@ -58,15 +60,17 @@ class RuleComponentUI: self._on_update_callback = (lambda: None) if on_update is None else on_update self.create_widgets() - def create_widgets(self): + @abc.abstractmethod + def create_widgets(self) -> dict: pass def show(self, component, editable=True): self._show_widgets(editable) self.component = component - def collect_value(self): - return None + @abc.abstractmethod + def collect_value(self) -> Any: + pass @contextlib_contextmanager def ignore_changes(self): @@ -105,5 +109,5 @@ class RuleComponentUI: for c in self.panel.get_children(): self.panel.remove(c) - def update_devices(self): + def update_devices(self): # noqa: B027 pass