diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index 9ad6ccd8d4..3dd6809f73 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -509,19 +509,18 @@ def charging(f, r, d, _a): "mouse-noop": [], } -# COMPONENTS = {} - -class RuleComponent: - def compile(self, c): - if isinstance(c, RuleComponent): - return c - elif isinstance(c, dict) and len(c) == 1: - k, v = next(iter(c.items())) - if k in COMPONENTS: - return COMPONENTS[k](v) - logger.warning("illegal component in rule: %s", c) - return Condition() +def compile_component(c): + if isinstance(c, Rule) or isinstance(c, Condition) or isinstance(c, Action): + return c + elif isinstance(c, dict) and len(c) == 1: + k, v = next(iter(c.items())) + try: + return COMPONENTS[k](v) + except KeyError: + pass + logger.warning("illegal component in rule: %s", c) + return FalllbackCondition() def _evaluate(components, feature, notification: HIDPPNotification, device, result) -> Any: @@ -535,9 +534,9 @@ def _evaluate(components, feature, notification: HIDPPNotification, device, resu return res -class Rule(RuleComponent): +class Rule: def __init__(self, args, source=None, warn=True): - self.components = [self.compile(a) for a in args] + self.components = [compile_component(a) for a in args] self.source = source def __str__(self): @@ -557,7 +556,7 @@ def data(self): return {"Rule": [c.data() for c in self.components]} -class Condition(RuleComponent): +class Condition: def __init__(self, *args): pass @@ -575,7 +574,7 @@ def __init__(self, op, warn=True): if isinstance(op, list) and len(op) == 1: op = op[0] self.op = op - self.component = self.compile(op) + self.component = compile_component(op) def __str__(self): return "Not: " + str(self.component) @@ -592,7 +591,7 @@ def data(self): class Or(Condition): def __init__(self, args, warn=True): - self.components = [self.compile(a) for a in args] + self.components = [compile_component(a) for a in args] def __str__(self): return "Or: [" + ", ".join(str(c) for c in self.components) + "]" @@ -615,7 +614,7 @@ def data(self): class And(Condition): def __init__(self, args, warn=True): - self.components = [self.compile(a) for a in args] + self.components = [compile_component(a) for a in args] def __str__(self): return "And: [" + ", ".join(str(c) for c in self.components) + "]" @@ -1143,7 +1142,7 @@ def data(self): return {"Host": self.host} -class Action(RuleComponent): +class Action: def __init__(self, *args): pass diff --git a/lib/solaar/ui/diversion_rules.py b/lib/solaar/ui/diversion_rules.py index f502d7eab5..901e149c38 100644 --- a/lib/solaar/ui/diversion_rules.py +++ b/lib/solaar/ui/diversion_rules.py @@ -490,7 +490,7 @@ def menu_do_copy(self, _mitem: Gtk.MenuItem, m: Gtk.TreeStore, it: Gtk.TreeIter) wrapped = m[it][0] c = wrapped.component - _rule_component_clipboard = _DIV.RuleComponent().compile(c.data()) + _rule_component_clipboard = _DIV.compile_component(c.data()) def menu_do_cut(self, _mitem, m, it): global _rule_component_clipboard @@ -511,7 +511,7 @@ def menu_do_paste(self, _mitem, m, it, below=False): c = _rule_component_clipboard _rule_component_clipboard = None if c: - _rule_component_clipboard = _DIV.RuleComponent().compile(c.data()) + _rule_component_clipboard = _DIV.compile_component(c.data()) self._menu_do_insert(_mitem, m, it, new_c=c, below=below) self._on_update() diff --git a/lib/solaar/ui/rule_base.py b/lib/solaar/ui/rule_base.py index d70967272c..00b71ca7c1 100644 --- a/lib/solaar/ui/rule_base.py +++ b/lib/solaar/ui/rule_base.py @@ -18,7 +18,6 @@ from typing import Callable from gi.repository import Gtk -from logitech_receiver import diversion def norm(s): @@ -48,8 +47,6 @@ def add_completion_to_entry(cls, entry, values): class RuleComponentUI: - CLASS = diversion.RuleComponent - def __init__(self, panel, on_update: Callable = None): self.panel = panel self.widgets = {} # widget -> coord. in grid