[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:
parent
8001ed3ada
commit
10c169af8a
1 changed files with 18 additions and 15 deletions
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue