[modules/core/cpu] optionally add per-cpu widget

if the parameter "percpu" is set to true, create one widget per cpu, and
also handle warning/error state on a per-widget basis.

see #785
This commit is contained in:
tobi-wan-kenobi 2021-04-27 17:17:13 +02:00
parent 8001ed3ada
commit 10c169af8a

View file

@ -26,32 +26,35 @@ import util.format
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.utilization)) super().__init__(config, theme, [])
self._percpu = util.format.asbool(self.parameter("percpu", False)) self._percpu = util.format.asbool(self.parameter("percpu", False))
self.update()
self.widget().set("theme.minwidth", self._format.format(*[100.0 - 10e-20]*len(self._utilization))) for idx, cpu_perc in enumerate(self.cpu_utilization()):
widget = self.add_widget(name="cpu#{}".format(idx), full_text=self.utilization)
widget.set("utilization", cpu_perc)
widget.set("theme.minwidth", self._format.format(100.0 - 10e-20))
core.input.register( core.input.register(
self, button=core.input.LEFT_MOUSE, cmd="gnome-system-monitor" self, button=core.input.LEFT_MOUSE, cmd="gnome-system-monitor"
) )
@property @property
def _format(self): def _format(self):
fmt = self.parameter("format", "{:.01f}%") return self.parameter("format", "{:.01f}%")
if self._percpu:
fmt = [fmt]*len(self._utilization)
fmt = " ".join(fmt)
return fmt
def utilization(self, _): def utilization(self, widget):
return self._format.format(*self._utilization) return self._format.format(widget.get("utilization", 0.0))
def cpu_utilization(self):
tmp = psutil.cpu_percent(percpu=self._percpu)
return tmp if self._percpu else [tmp]
def update(self): def update(self):
self._utilization = psutil.cpu_percent(percpu=self._percpu) for idx, cpu_perc in enumerate(self.cpu_utilization()):
if not self._percpu: self.widgets()[idx].set("utilization", cpu_perc)
self._utilization = [self._utilization]
def state(self, _): def state(self, widget):
return self.threshold_state(sum(self._utilization)/len(self._utilization), 70, 80) return self.threshold_state(widget.get("utilization", 0.0), 70, 80)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4