From 9cd9ff626dde0873b605edba9b65f938d77f837c Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Thu, 30 Apr 2020 12:42:34 +0200 Subject: [PATCH] [core] make widget name an attribute first, this fixes #607 also, i think it slightly simplifies code to make "simple" stuff like names, etc. attributes instead of methods all the time. so, expect this to be extended to other components, as well. --- core/module.py | 2 +- core/widget.py | 5 +---- doc/NOTES.md | 1 + modules/contrib/gpmdp.py | 4 ++-- modules/contrib/traffic.py | 4 ++-- modules/core/battery.py | 14 +++++++------- tests/core/test_module.py | 4 ++-- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/core/module.py b/core/module.py index 21fbb1c..3919ea3 100644 --- a/core/module.py +++ b/core/module.py @@ -81,7 +81,7 @@ class Module(core.input.Object): if not name: return self.widgets()[0] for w in self.widgets(): - if w.name() == name: return w + if w.name == name: return w return None def state(self, widget): diff --git a/core/widget.py b/core/widget.py index d76aca6..3c42efb 100644 --- a/core/widget.py +++ b/core/widget.py @@ -7,10 +7,7 @@ class Widget(util.store.Store, core.input.Object): super(Widget, self).__init__() self.__full_text = full_text self.__module = module - self.__name = name - - def name(self): - return self.__name + self.name = name def full_text(self, value=None): if value: diff --git a/doc/NOTES.md b/doc/NOTES.md index 498df8b..2369f7a 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -25,3 +25,4 @@ - bumblebee-ctrl - theme.exclude (battery) - help output +- configuration files diff --git a/modules/contrib/gpmdp.py b/modules/contrib/gpmdp.py index 549983d..e40f3d1 100644 --- a/modules/contrib/gpmdp.py +++ b/modules/contrib/gpmdp.py @@ -38,9 +38,9 @@ class Module(core.module.Module): self.__load_song() def state(self, widget): - if widget.name() == 'gpmdp.prev': + if widget.name == 'gpmdp.prev': return 'prev' - if widget.name() == 'gpmdp.next': + if widget.name == 'gpmdp.next': return 'next' return self.__status diff --git a/modules/contrib/traffic.py b/modules/contrib/traffic.py index 1c85405..a4a66e2 100644 --- a/modules/contrib/traffic.py +++ b/modules/contrib/traffic.py @@ -51,9 +51,9 @@ class Module(core.module.Module): self._update_widgets(widgets) def state(self, widget): - if 'traffic.rx' in widget.name(): + if 'traffic.rx' in widget.name: return 'rx' - if 'traffic.tx' in widget.name(): + if 'traffic.tx' in widget.name: return 'tx' return self._status diff --git a/modules/core/battery.py b/modules/core/battery.py index 54af015..283b54c 100644 --- a/modules/core/battery.py +++ b/modules/core/battery.py @@ -124,28 +124,28 @@ class Module(core.module.Module): widget.set('theme.exclude', 'suffix') def capacity(self, widget): - if widget.name() == 'all-batteries': + if widget.name == 'all-batteries': capacity = self.__manager.capacity_all(self._batteries) else: - capacity = self.__manager.capacity(widget.name()) + capacity = self.__manager.capacity(widget.name) widget.set('capacity', capacity) widget.set('ac', self.__manager.isac_any(self._batteries)) widget.set('theme.minwidth', '100%') # Read power conumption if util.format.asbool(self.parameter('showpowerconsumption', False)): - output = '{}% ({})'.format(capacity, self.__manager.consumption(widget.name())) + output = '{}% ({})'.format(capacity, self.__manager.consumption(widget.name)) else: output = '{}%'.format(capacity) if util.format.asbool(self.parameter('showremaining', True))\ - and self.__manager.charge(widget.name()) == 'Discharging': + and self.__manager.charge(widget.name) == 'Discharging': remaining = self.__manager.remaining() if remaining >= 0: output = '{} {}'.format(output, util.format.duration(remaining, compact=True, unit=True)) if util.format.asbool(self.parameter('showdevice', False)): - output = '{} ({})'.format(output, widget.name()) + output = '{} ({})'.format(output, widget.name) return output @@ -165,10 +165,10 @@ class Module(core.module.Module): if widget.get('ac'): state.append('AC') else: - if widget.name() == 'all-batteries': + if widget.name == 'all-batteries': charge = self.__manager.charge_any(self._batteries) else: - charge = self.__manager.charge(widget.name()) + charge = self.__manager.charge(widget.name) if charge == 'Discharging': state.append('discharging-{}'.format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity)))) elif charge == 'Unknown': diff --git a/tests/core/test_module.py b/tests/core/test_module.py index 56c2da0..8f9b96d 100644 --- a/tests/core/test_module.py +++ b/tests/core/test_module.py @@ -97,8 +97,8 @@ class module(unittest.TestCase): cfg = core.config.Config([]) module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget]) - self.assertEqual(self.someWidget, module.widget(self.someWidget.name())) - self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name())) + self.assertEqual(self.someWidget, module.widget(self.someWidget.name)) + self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name)) self.assertEqual(None, module.widget(self.unusedWidgetName)) self.assertEqual(self.someWidget, module.widget())