[modules/cpu] Add initial version of CPU utilization module

Re-enable the CPU utilization module as proof-of-concept for the new
core engine.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-04 17:45:42 +01:00
parent 8855f1155b
commit aacc56a4e2
9 changed files with 85 additions and 10 deletions

View file

@ -55,10 +55,12 @@ class Engine(object):
while self.running():
widgets = []
for module in self._modules:
widgets += module.widgets()
self._output.draw(widgets)
module_widgets = module.widgets()
widgets += module_widgets if isinstance(module_widgets, list) else [module_widgets]
self._output.draw(widgets=widgets, engine=self)
self._output.flush()
time.sleep(1)
if self.running():
time.sleep(1)
self._output.stop()

18
bumblebee/modules/cpu.py Normal file
View file

@ -0,0 +1,18 @@
# pylint: disable=C0111,R0903
"""Displays CPU utilization across all CPUs."""
import psutil
import bumblebee.engine
class Module(bumblebee.engine.Module):
def __init__(self, engine):
super(Module, self).__init__(engine)
self._utilization = psutil.cpu_percent(percpu=False)
def widgets(self):
self._utilization = psutil.cpu_percent(percpu=False)
return bumblebee.output.Widget(full_text="{:05.02f}%".format(self._utilization))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -9,6 +9,6 @@ class Module(bumblebee.engine.Module):
super(Module, self).__init__(engine)
def widgets(self):
return []
return bumblebee.output.Widget(full_text="test")
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -5,6 +5,15 @@
import sys
import json
class Widget(object):
"""Represents a single visible block in the status bar"""
def __init__(self, full_text):
self._full_text = full_text
def full_text(self):
"""Retrieve the full text to display in the widget"""
return self._full_text
class I3BarOutput(object):
"""Manage output according to the i3bar protocol"""
def __init__(self):
@ -18,14 +27,14 @@ class I3BarOutput(object):
"""Finish i3bar protocol"""
sys.stdout.write("]\n")
def draw(self, widgets):
def draw(self, widgets, engine=None):
"""Draw a number of widgets"""
if not isinstance(widgets, list):
widgets = [widgets]
result = []
for widget in widgets:
result.append({
u"full_text": widget.text()
u"full_text": widget.full_text()
})
sys.stdout.write(json.dumps(result))