[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):
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.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(
self, button=core.input.LEFT_MOUSE, cmd="gnome-system-monitor"
)
@property
def _format(self):
fmt = self.parameter("format", "{:.01f}%")
if self._percpu:
fmt = [fmt]*len(self._utilization)
fmt = " ".join(fmt)
return fmt
return self.parameter("format", "{:.01f}%")
def utilization(self, _):
return self._format.format(*self._utilization)
def utilization(self, widget):
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):
self._utilization = psutil.cpu_percent(percpu=self._percpu)
if not self._percpu:
self._utilization = [self._utilization]
for idx, cpu_perc in enumerate(self.cpu_utilization()):
self.widgets()[idx].set("utilization", cpu_perc)
def state(self, _):
return self.threshold_state(sum(self._utilization)/len(self._utilization), 70, 80)
def state(self, widget):
return self.threshold_state(widget.get("utilization", 0.0), 70, 80)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4