[modules] remove setting widgets via "self.widgets(new list)"

use self.add_widget() for all modules
This commit is contained in:
tobi-wan-kenobi 2020-05-09 10:57:44 +02:00
parent 85ac953111
commit 99e3571eee
17 changed files with 52 additions and 121 deletions

View file

@ -28,6 +28,7 @@ def load(module_name, config=core.config.Config([]), theme=None):
) )
return getattr(mod, "Module")(config, theme) return getattr(mod, "Module")(config, theme)
except ImportError as e: except ImportError as e:
log.debug("failed to import {}: {}".format(module_name, e))
error = e error = e
log.fatal("failed to import {}: {}".format(module_name, error)) log.fatal("failed to import {}: {}".format(module_name, error))
return Error(config=config, module=module_name, error=error) return Error(config=config, module=module_name, error=error)
@ -76,11 +77,12 @@ class Module(core.input.Object):
self.__widgets = [module.widget()] self.__widgets = [module.widget()]
self.update = module.update self.update = module.update
def widgets(self, widgets=None): def widgets(self):
if widgets:
self.__widgets = widgets
return self.__widgets return self.__widgets
def clear_widgets(self):
del self.__widgets[:]
def add_widget(self, full_text="", name=None): def add_widget(self, full_text="", name=None):
widget = core.widget.Widget(full_text=full_text, name=name, module=self) widget = core.widget.Widget(full_text=full_text, name=name, module=self)
self.widgets().append(widget) self.widgets().append(widget)

View file

@ -72,7 +72,8 @@ Otherwise, you have a number of ways to handle widgets: - During the
``super().init__(...)`` inside the modules constructor, you can specify ``super().init__(...)`` inside the modules constructor, you can specify
a **list** of widgets, and those will comprise the widgets (in ordered a **list** of widgets, and those will comprise the widgets (in ordered
fashion) - During runtime, you can set a new list of widgets by using fashion) - During runtime, you can set a new list of widgets by using
the ``self.widgets(<new list>)`` method of the module the ``self.add_widget()`` method of the module to add new widgets and
``self.clear_widgets()`` method to remove all widgets.
Adding widgets at runtime Adding widgets at runtime
------------------------- -------------------------

View file

@ -16,10 +16,8 @@
## TODO ## TODO
- themes: use colors to improve theme readability - themes: use colors to improve theme readability
- convert some stuff to simple attributes to reduce LOCs - convert some stuff to simple attributes to reduce LOCs
- use add_widget() instead of core.widget.Widget()
- use widget index for bumblebee-ctl as alternative (??) - use widget index for bumblebee-ctl as alternative (??)
# documentation # documentation
Add info about error widget and events for error logging Add info about error widget and events for error logging
- add module contributor doc
- which location APIs are used? - which location APIs are used?

View file

@ -26,7 +26,6 @@ except ImportError:
log.warning('unable to import module "power": Time estimates will not be available') log.warning('unable to import module "power": Time estimates will not be available')
import core.module import core.module
import core.widget
import core.input import core.input
import util.format import util.format
@ -105,8 +104,7 @@ class BatteryManager(object):
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme):
widgets = [] super().__init__(config, theme, [])
super().__init__(config, theme, widgets)
self.__manager = BatteryManager() self.__manager = BatteryManager()
@ -123,17 +121,15 @@ class Module(core.module.Module):
) )
if util.format.asbool(self.parameter("compact-devices", False)): if util.format.asbool(self.parameter("compact-devices", False)):
widget = core.widget.Widget( widget = self.add_widget(
full_text=self.capacity, name="all-batteries", module=self full_text=self.capacity, name="all-batteries"
) )
widgets.append(widget)
else: else:
for battery in self._batteries: for battery in self._batteries:
log.debug("adding new widget for {}".format(battery)) log.debug("adding new widget for {}".format(battery))
widget = core.widget.Widget( widget = self.add_widget(
full_text=self.capacity, name=battery, module=self full_text=self.capacity, name=battery
) )
widgets.append(widget)
for w in self.widgets(): for w in self.widgets():
if util.format.asbool(self.parameter("decorate", True)) == False: if util.format.asbool(self.parameter("decorate", True)) == False:
widget.set("theme.exclude", "suffix") widget.set("theme.exclude", "suffix")

View file

@ -27,7 +27,6 @@ import os
import string import string
import core.module import core.module
import core.widget
import core.input import core.input
import core.decorators import core.decorators
@ -51,11 +50,9 @@ class Module(core.module.Module):
self._tags = defaultdict(lambda: "") self._tags = defaultdict(lambda: "")
# Create widgets # Create widgets
widget_list = []
widget_map = {} widget_map = {}
for widget_name in self._layout.split(): for widget_name in self._layout.split():
widget = core.widget.Widget(name=widget_name, module=self) widget = self.add_widget(name=widget_name)
widget_list.append(widget)
self._cmd = "cmus-remote" self._cmd = "cmus-remote"
if self._server is not None: if self._server is not None:
self._cmd = "{cmd} --server {server}".format( self._cmd = "{cmd} --server {server}".format(
@ -98,7 +95,6 @@ class Module(core.module.Module):
widget_name=widget_name widget_name=widget_name
) )
) )
self.widgets(widget_list)
# Register input callbacks # Register input callbacks
for widget, callback_options in widget_map.items(): for widget, callback_options in widget_map.items():

View file

@ -37,7 +37,6 @@ contributed by `somospocos <https://github.com/somospocos>`_ - many thanks!
import psutil import psutil
import core.module import core.module
import core.widget
import util.cli import util.cli
import util.graph import util.graph
@ -53,27 +52,24 @@ class Module(core.module.Module):
) )
self.__widget_names = self.__layout.split() self.__widget_names = self.__layout.split()
self.__colored = util.format.asbool(self.parameter("colored", False)) self.__colored = util.format.asbool(self.parameter("colored", False))
widget_list = []
for widget_name in self.__widget_names: for widget_name in self.__widget_names:
if widget_name == "cpu2.maxfreq": if widget_name == "cpu2.maxfreq":
widget = core.widget.Widget(name=widget_name, full_text=self.maxfreq) widget = self.add_widget(name=widget_name, full_text=self.maxfreq)
widget.set("type", "freq") widget.set("type", "freq")
elif widget_name == "cpu2.cpuload": elif widget_name == "cpu2.cpuload":
widget = core.widget.Widget(name=widget_name, full_text=self.cpuload) widget = self.add_widget(name=widget_name, full_text=self.cpuload)
widget.set("type", "load") widget.set("type", "load")
elif widget_name == "cpu2.coresload": elif widget_name == "cpu2.coresload":
widget = core.widget.Widget(name=widget_name, full_text=self.coresload) widget = self.add_widget(name=widget_name, full_text=self.coresload)
widget.set("type", "loads") widget.set("type", "loads")
elif widget_name == "cpu2.temp": elif widget_name == "cpu2.temp":
widget = core.widget.Widget(name=widget_name, full_text=self.temp) widget = self.add_widget(name=widget_name, full_text=self.temp)
widget.set("type", "temp") widget.set("type", "temp")
elif widget_name == "cpu2.fanspeed": elif widget_name == "cpu2.fanspeed":
widget = core.widget.Widget(name=widget_name, full_text=self.fanspeed) widget = self.add_widget(name=widget_name, full_text=self.fanspeed)
widget.set("type", "fan") widget.set("type", "fan")
if self.__colored: if self.__colored:
widget.set("pango", True) widget.set("pango", True)
widget_list.append(widget)
self.widgets(widget_list)
self.__temp_pattern = self.parameter("temp_pattern") self.__temp_pattern = self.parameter("temp_pattern")
if self.__temp_pattern is None: if self.__temp_pattern is None:
self.__temp = "n/a" self.__temp = "n/a"

View file

@ -10,7 +10,6 @@ contributed by `freed00m <https://github.com/freed00m>`_ - many thanks!
""" """
import core.module import core.module
import core.widget
import util.cli import util.cli
import util.format import util.format
@ -42,8 +41,7 @@ class Module(core.module.Module):
for indicator in self.__include: for indicator in self.__include:
widget = self.widget(indicator) widget = self.widget(indicator)
if not widget: if not widget:
widget = core.widget.Widget(name=indicator, module=self) widget = self.add_widget(name=indicator, full_text=indicator)
self.widgets().append(widget)
widget.set( widget.set(
"status", "status",
@ -51,7 +49,6 @@ class Module(core.module.Module):
if "{}:on".format(indicator.lower()) in status_line.lower() if "{}:on".format(indicator.lower()) in status_line.lower()
else False, else False,
) )
widget.full_text(indicator)
def state(self, widget): def state(self, widget):
states = [] states = []

View file

@ -53,7 +53,6 @@ import string
import os import os
import core.module import core.module
import core.widget
import core.input import core.input
import core.decorators import core.decorators
@ -80,11 +79,9 @@ class Module(core.module.Module):
self._hostcmd = " -h " + self.parameter("host") self._hostcmd = " -h " + self.parameter("host")
# Create widgets # Create widgets
widget_list = []
widget_map = {} widget_map = {}
for widget_name in self._layout.split(): for widget_name in self._layout.split():
widget = core.widget.Widget(name=widget_name, module=self) widget = self.add_widget(name=widget_name)
widget_list.append(widget)
if widget_name == "mpd.prev": if widget_name == "mpd.prev":
widget_map[widget] = { widget_map[widget] = {
@ -118,7 +115,6 @@ class Module(core.module.Module):
widget_name=widget_name widget_name=widget_name
) )
) )
self.widgets(widget_list)
# Register input callbacks # Register input callbacks
for widget, callback_options in widget_map.items(): for widget, callback_options in widget_map.items():

View file

@ -7,7 +7,6 @@ Requires the following executable:
""" """
import core.module import core.module
import core.widget
import core.input import core.input
import util.cli import util.cli
@ -34,7 +33,7 @@ class Module(core.module.Module):
widget = self.widget(display) widget = self.widget(display)
if not widget: if not widget:
widget = core.widget.Widget(full_text=display, name=display) widget = self.add_widget(full_text=display, name=display)
core.input.register( core.input.register(
widget, button=core.input.LEFT_MOUSE, cmd=self.__toggle widget, button=core.input.LEFT_MOUSE, cmd=self.__toggle
) )

View file

@ -26,7 +26,6 @@ LINK = "https://github.com/tobi-wan-kenobi/bumblebee-status/wiki"
LABEL = "Click me" LABEL = "Click me"
import core.module import core.module
import core.widget
import core.input import core.input
import core.decorators import core.decorators
@ -45,8 +44,6 @@ class Module(core.module.Module):
def update_widgets(self): def update_widgets(self):
""" Creates a set of widget per user define shortcut.""" """ Creates a set of widget per user define shortcut."""
widgets = self.widgets()
cmds = self.__cmds.split(self.__delim) cmds = self.__cmds.split(self.__delim)
labels = self.__labels.split(self.__delim) labels = self.__labels.split(self.__delim)
@ -65,10 +62,8 @@ class Module(core.module.Module):
cmd = cmds[idx] cmd = cmds[idx]
label = labels[idx] label = labels[idx]
widget = core.widget.Widget(full_text=label) widget = self.add_widget(full_text=label)
core.input.register(widget, button=core.input.LEFT_MOUSE, cmd=cmd) core.input.register(widget, button=core.input.LEFT_MOUSE, cmd=cmd)
widgets.append(widget)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -16,7 +16,6 @@ import os
import shutil import shutil
import core.module import core.module
import core.widget
import core.decorators import core.decorators
import util.cli import util.cli
@ -32,26 +31,22 @@ class Module(core.module.Module):
self.display = self.parameter("display", "combined") self.display = self.parameter("display", "combined")
self.drives = self.parameter("drives", "sda") self.drives = self.parameter("drives", "sda")
self.show_names = util.format.asbool(self.parameter("show_names", True)) self.show_names = util.format.asbool(self.parameter("show_names", True))
self.widgets(self.create_widgets()) self.create_widgets()
def create_widgets(self): def create_widgets(self):
widgets = []
if self.display == "combined": if self.display == "combined":
widget = core.widget.Widget(module=self) widget = self.add_widget()
widget.set("device", "combined") widget.set("device", "combined")
widget.set("assessment", self.combined()) widget.set("assessment", self.combined())
self.output(widget) self.output(widget)
widgets.append(widget)
else: else:
for device in self.devices: for device in self.devices:
if self.display == "singles" and device not in self.drives: if self.display == "singles" and device not in self.drives:
continue continue
widget = core.widget.Widget(module=self) widget = self.add_widget()
widget.set("device", device) widget.set("device", device)
widget.set("assessment", self.smart(device)) widget.set("assessment", self.smart(device))
self.output(widget) self.output(widget)
widgets.append(widget)
return widgets
def update(self): def update(self):
for widget in self.widgets(): for widget in self.widgets():

View file

@ -24,7 +24,6 @@ except ImportError:
no_title = "n/a" no_title = "n/a"
import core.module import core.module
import core.widget
import core.decorators import core.decorators
import util.format import util.format
@ -41,14 +40,10 @@ class Module(core.module.Module):
self.__title = "" self.__title = ""
# set output of the module # set output of the module
self.widgets( self.add_widget(
[ full_text=self.__scrolling_focused_title
core.widget.Widget( if self.__scroll
full_text=self.__scrolling_focused_title else self.__focused_title
if self.__scroll
else self.__focused_title
)
]
) )
# create a connection with i3ipc # create a connection with i3ipc

View file

@ -21,7 +21,6 @@ import psutil
import netifaces import netifaces
import core.module import core.module
import core.widget
import util.format import util.format
import util.graph import util.graph
@ -29,8 +28,7 @@ import util.graph
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme):
widgets = [] super().__init__(config, theme, [])
super().__init__(config, theme, widgets)
self._exclude = tuple( self._exclude = tuple(
filter( filter(
@ -60,7 +58,7 @@ class Module(core.module.Module):
if self._graphlen > 0: if self._graphlen > 0:
self._graphdata = {} self._graphdata = {}
self._first_run = True self._first_run = True
self._update_widgets(widgets) self._update_widgets()
def state(self, widget): def state(self, widget):
if "traffic.rx" in widget.name: if "traffic.rx" in widget.name:
@ -70,12 +68,10 @@ class Module(core.module.Module):
return self._status return self._status
def update(self): def update(self):
self._update_widgets(self.widgets()) self._update_widgets()
def create_widget(self, widgets, name, txt=None, attributes={}): def create_widget(self, name, txt=None, attributes={}):
widget = core.widget.Widget(name=name, module=self) widget = self.add_widget(name=name, full_text=txt)
widget.full_text(txt)
widgets.append(widget)
for key in attributes: for key in attributes:
widget.set(key, attributes[key]) widget.set(key, attributes[key])
@ -114,12 +110,12 @@ class Module(core.module.Module):
minwidth_str += "KiB/s" minwidth_str += "KiB/s"
return minwidth_str return minwidth_str
def _update_widgets(self, widgets): def _update_widgets(self):
interfaces = [ interfaces = [
i for i in netifaces.interfaces() if not i.startswith(self._exclude) i for i in netifaces.interfaces() if not i.startswith(self._exclude)
] ]
del widgets[:] self.clear_widgets()
counters = psutil.net_io_counters(pernic=True) counters = psutil.net_io_counters(pernic=True)
now = time.time() now = time.time()
@ -158,12 +154,11 @@ class Module(core.module.Module):
name = "traffic-{}".format(interface) name = "traffic-{}".format(interface)
if self._showname: if self._showname:
self.create_widget(widgets, name, interface) self.create_widget(name, interface)
for direction in ["rx", "tx"]: for direction in ["rx", "tx"]:
name = "traffic.{}-{}".format(direction, interface) name = "traffic.{}-{}".format(direction, interface)
widget = self.create_widget( widget = self.create_widget(
widgets,
name, name,
attributes={"theme.minwidth": self.get_minwidth_str()}, attributes={"theme.minwidth": self.get_minwidth_str()},
) )

View file

@ -33,7 +33,6 @@ from pkg_resources import parse_version
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
import core.module import core.module
import core.widget
import util.cli import util.cli
import util.format import util.format
@ -58,7 +57,7 @@ class Module(core.module.Module):
self._warnfree = int(self.parameter("warnfree", default=10)) self._warnfree = int(self.parameter("warnfree", default=10))
def update(self): def update(self):
widgets = self.widgets() self.clear_widgets()
zfs_version_path = "/sys/module/zfs/version" zfs_version_path = "/sys/module/zfs/version"
# zpool list -H: List all zpools, use script mode (no headers and tabs as separators). # zpool list -H: List all zpools, use script mode (no headers and tabs as separators).
try: try:
@ -77,9 +76,6 @@ class Module(core.module.Module):
("sudo " if self._usesudo else "") + "zpool list -H" ("sudo " if self._usesudo else "") + "zpool list -H"
).split("\n") ).split("\n")
for widget in widgets:
widget.set("visited", False)
for raw_zpool in raw_zpools: for raw_zpool in raw_zpools:
try: try:
# Ignored fields (assigned to _) are 'expandsz' and 'altroot', also 'ckpoint' in ZFS 0.8.0+ # Ignored fields (assigned to _) are 'expandsz' and 'altroot', also 'ckpoint' in ZFS 0.8.0+
@ -136,10 +132,9 @@ class Module(core.module.Module):
widget = self.widget(name) widget = self.widget(name)
if not widget: if not widget:
widget = core.widget.Widget(name=name) widget = self.add_widget(name=name)
widget.set("last_iostat", [0, 0, 0, 0]) widget.set("last_iostat", [0, 0, 0, 0])
widget.set("last_timestamp", 0) widget.set("last_timestamp", 0)
widgets.append(widget)
delta_iostat = [b - a for a, b in zip(iostat, widget.get("last_iostat"))] delta_iostat = [b - a for a, b in zip(iostat, widget.get("last_iostat"))]
widget.set("last_iostat", iostat) widget.set("last_iostat", iostat)
@ -183,9 +178,8 @@ class Module(core.module.Module):
widget_w = self.widget(wname) widget_w = self.widget(wname)
widget_r = self.widget(rname) widget_r = self.widget(rname)
if not widget_w or not widget_r: if not widget_w or not widget_r:
widget_r = core.widget.Widget(name=rname) widget_r = self.add_widget(name=rname)
widget_w = core.widget.Widget(name=wname) widget_w = self.add_widget(name=wname)
widgets.extend([widget_r, widget_w])
for w in [widget_r, widget_w]: for w in [widget_r, widget_w]:
w.set( w.set(
"theme.minwidth", "theme.minwidth",
@ -193,7 +187,6 @@ class Module(core.module.Module):
ops=9999, band=util.format.bytefmt(999.99 * (1024 ** 2)) ops=9999, band=util.format.bytefmt(999.99 * (1024 ** 2))
), ),
) )
w.set("visited", True)
widget_w.full_text( widget_w.full_text(
self._ioformat.format( self._ioformat.format(
ops=round(writes), band=util.format.bytefmt(nwritten) ops=round(writes), band=util.format.bytefmt(nwritten)
@ -205,10 +198,6 @@ class Module(core.module.Module):
) )
) )
for widget in widgets:
if widget.get("visited") is False:
widgets.remove(widget)
self.widgets(widgets)
def state(self, widget): def state(self, widget):
if widget.name.endswith("__read"): if widget.name.endswith("__read"):

View file

@ -12,7 +12,6 @@ import os
import pygit2 import pygit2
import core.module import core.module
import core.widget
import util.cli import util.cli
@ -28,15 +27,13 @@ class Module(core.module.Module):
def update(self): def update(self):
state = {} state = {}
new_widgets = [] self.clear_widgets()
try: try:
directory = util.cli.execute("xcwd").strip() directory = util.cli.execute("xcwd").strip()
directory = self.__get_git_root(directory) directory = self.__get_git_root(directory)
repo = pygit2.Repository(directory) repo = pygit2.Repository(directory)
new_widgets.append( self.add_widget(name="git.main", full_text=repo.head.shorthand)
core.widget.Widget(name="git.main", full_text=repo.head.shorthand)
)
for filepath, flags in repo.status().items(): for filepath, flags in repo.status().items():
if ( if (
@ -56,14 +53,12 @@ class Module(core.module.Module):
state["modified"] = True state["modified"] = True
self.__error = False self.__error = False
if "new" in state: if "new" in state:
new_widgets.append(core.widget.Widget(name="git.new")) self.add_widget(name="git.new")
if "modified" in state: if "modified" in state:
new_widgets.append(core.widget.Widget(name="git.modified")) self.add_widget(name="git.modified")
if "deleted" in state: if "deleted" in state:
new_widgets.append(core.widget.Widget(name="git.deleted")) self.add_widget(name="git.deleted")
self.widgets().clear()
self.widget(new_widgets)
except Exception as e: except Exception as e:
self.__error = True self.__error = True

View file

@ -16,7 +16,6 @@ import shutil
import netifaces import netifaces
import subprocess import subprocess
import core.widget
import core.module import core.module
import core.decorators import core.decorators
import util.cli import util.cli
@ -87,14 +86,12 @@ class Module(core.module.Module):
return retval return retval
def _update_widgets(self, widgets): def _update_widgets(self, widgets):
self.clear_widgets()
interfaces = [ interfaces = [
i for i in netifaces.interfaces() if not i.startswith(self._exclude) i for i in netifaces.interfaces() if not i.startswith(self._exclude)
] ]
interfaces.extend([i for i in netifaces.interfaces() if i in self._include]) interfaces.extend([i for i in netifaces.interfaces() if i in self._include])
for widget in widgets:
widget.set("visited", False)
for intf in interfaces: for intf in interfaces:
addr = [] addr = []
state = "down" state = "down"
@ -112,8 +109,7 @@ class Module(core.module.Module):
widget = self.widget(intf) widget = self.widget(intf)
if not widget: if not widget:
widget = core.widget.Widget(name=intf, module=self) widget = self.add_widget(name=intf)
widgets.append(widget)
# join/split is used to get rid of multiple whitespaces (in case SSID is not available, for instance # join/split is used to get rid of multiple whitespaces (in case SSID is not available, for instance
widget.full_text( widget.full_text(
" ".join( " ".join(
@ -127,11 +123,6 @@ class Module(core.module.Module):
) )
widget.set("intf", intf) widget.set("intf", intf)
widget.set("state", state) widget.set("state", state)
widget.set("visited", True)
for widget in widgets:
if widget.get("visited") is False:
widgets.remove(widget)
def get_ssid(self, intf): def get_ssid(self, intf):
if self._iswlan(intf) and self.iwgetid: if self._iswlan(intf) and self.iwgetid:

View file

@ -21,7 +21,6 @@ import re
import sys import sys
import core.module import core.module
import core.widget
import core.input import core.input
import core.decorators import core.decorators
@ -52,7 +51,7 @@ class Module(core.module.Module):
self._needs_update = True self._needs_update = True
def update(self): def update(self):
new_widgets = [] self.clear_widgets()
if self._autoupdate == False and self._needs_update == False: if self._autoupdate == False and self._needs_update == False:
return return
@ -67,22 +66,18 @@ class Module(core.module.Module):
widget = self.widget(display) widget = self.widget(display)
if not widget: if not widget:
widget = core.widget.Widget( widget = self.add_widget(
full_text=display, name=display, module=self full_text=display, name=display
) )
core.input.register(widget, button=1, cmd=self._toggle) core.input.register(widget, button=1, cmd=self._toggle)
core.input.register(widget, button=3, cmd=self._toggle) core.input.register(widget, button=3, cmd=self._toggle)
new_widgets.append(widget)
widget.set("state", "on" if m else "off") widget.set("state", "on" if m else "off")
widget.set("pos", int(m.group(1)) if m else sys.maxsize) widget.set("pos", int(m.group(1)) if m else sys.maxsize)
self.widgets(new_widgets)
if self._autoupdate == False: if self._autoupdate == False:
widget = core.widget.Widget(full_text="", module=self) widget = self.add_widget(full_text="")
widget.set("state", "refresh") widget.set("state", "refresh")
core.input.register(widget, button=1, cmd=self._refresh) core.input.register(widget, button=1, cmd=self._refresh)
self.widgets().append(widget)
def state(self, widget): def state(self, widget):
return widget.get("state", "off") return widget.get("state", "off")