Skip to content

Commit

Permalink
diversion: Remove RuleComponent class to avoid coupling of classes
Browse files Browse the repository at this point in the history
Replace tight coupling of Action and Condition classes by removing their
common base class and converting it into a function.

The RuleComponent was a base class solely holding a compile function and
passing it to its children. There is no need for tight coupling with
inheritance for that purpose.

Related pwr-Solaar#2659
  • Loading branch information
MattHag committed Jan 2, 2025
1 parent 810cda9 commit fc25353
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
38 changes: 19 additions & 19 deletions lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,19 @@ 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:
cls = COMPONENTS[k]
return cls(v)
except KeyError:
pass
logger.warning("illegal component in rule: %s", c)
return FalllbackCondition()


def _evaluate(components, feature, notification: HIDPPNotification, device, result) -> Any:
Expand All @@ -537,9 +537,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):
Expand All @@ -559,7 +559,7 @@ def data(self):
return {"Rule": [c.data() for c in self.components]}


class Condition(RuleComponent):
class Condition:
def __init__(self, *args):
pass

Expand All @@ -577,7 +577,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)
Expand All @@ -594,7 +594,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) + "]"
Expand All @@ -617,7 +617,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) + "]"
Expand Down Expand Up @@ -1145,7 +1145,7 @@ def data(self):
return {"Host": self.host}


class Action(RuleComponent):
class Action:
def __init__(self, *args):
pass

Expand Down
4 changes: 2 additions & 2 deletions lib/solaar/ui/diversion_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,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 = diversion.RuleComponent().compile(c.data())
_rule_component_clipboard = diversion.compile_component(c.data())

def menu_do_cut(self, _mitem, m, it):
global _rule_component_clipboard
Expand All @@ -533,7 +533,7 @@ def menu_do_paste(self, _mitem, m, it, below=False):
c = _rule_component_clipboard
_rule_component_clipboard = None
if c:
_rule_component_clipboard = diversion.RuleComponent().compile(c.data())
_rule_component_clipboard = diversion.compile_component(c.data())
self._menu_do_insert(_mitem, m, it, new_c=c, below=below)
self._on_update()

Expand Down
15 changes: 4 additions & 11 deletions lib/solaar/ui/rule_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
## 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
from logitech_receiver import diversion


def norm(s):
Expand Down Expand Up @@ -49,9 +46,7 @@ def add_completion_to_entry(cls, entry, values):
liststore.append((v,))


class RuleComponentUI(abc.ABC):
CLASS = diversion.RuleComponent

class RuleComponentUI:
def __init__(self, panel, on_update: Callable = None):
self.panel = panel
self.widgets = {} # widget -> coord. in grid
Expand All @@ -60,16 +55,14 @@ def __init__(self, panel, on_update: Callable = None):
self._on_update_callback = (lambda: None) if on_update is None else on_update
self.create_widgets()

@abc.abstractmethod
def create_widgets(self) -> dict:
def create_widgets(self):
pass

def show(self, component, editable=True):
self._show_widgets(editable)
self.component = component

@abc.abstractmethod
def collect_value(self) -> Any:
def collect_value(self):
pass

@contextlib_contextmanager
Expand Down Expand Up @@ -109,5 +102,5 @@ def _remove_panel_items(self):
for c in self.panel.get_children():
self.panel.remove(c)

def update_devices(self): # noqa: B027
def update_devices(self):
pass

0 comments on commit fc25353

Please sign in to comment.