[modules/cpu2] Update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-26 10:33:41 +02:00
parent b34178ee71
commit cfa1a5c896
5 changed files with 63 additions and 53 deletions

View file

@ -55,6 +55,7 @@ def main():
for module in config.modules():
modules.append(core.module.load(module, config))
modules[-1].theme = theme # TODO: make this nice (ctor)
output.modules(modules)
core.event.trigger('start')
while True:

View file

@ -150,6 +150,8 @@ class i3(object):
blk = block(self.__theme, module, widget)
blk.set('min_width', widget.get('theme.minwidth'))
blk.set('full_text', self.__content[widget])
if widget.get('pango', False):
blk.set('markup', 'pango')
if self.__config.debug():
blk.set('__state', ', '.join(module.state(widget)))
return blk

View file

@ -49,6 +49,9 @@ class Theme(object):
def keywords(self):
return self.__keywords
def color(self, name, default=None):
return self.keywords().get(name, default)
def load(self, name, subdir=''):
if isinstance(name, dict): return name # support plain data
for path in PATHS:

View file

@ -25,3 +25,5 @@
## TODO
- themes: use colors to improve theme readability
- themes: add theme to module c'tor (cpu2)
- convert some stuff to simple attributes to reduce LOCs

View file

@ -33,105 +33,105 @@ lacking the aforementioned pattern settings or they have wrong values.
"""
try:
import psutil
except ImportError:
pass
import bumblebee.engine
import bumblebee.util
import bumblebee.output
import core.module
import core.widget
import util.cli
import util.graph
import util.format
class Module(bumblebee.engine.Module):
class Module(core.module.Module):
def __init__(self, config):
super().__init__(config, [])
def __init__(self, engine, config):
super(Module, self).__init__(engine, config, [])
self._layout = self.parameter('layout', 'cpu2.maxfreq cpu2.cpuload cpu2.coresload cpu2.temp cpu2.fanspeed')
self._widget_names = self._layout.split()
self.__layout = self.parameter('layout', 'cpu2.maxfreq cpu2.cpuload cpu2.coresload cpu2.temp cpu2.fanspeed')
self.__widget_names = self.__layout.split()
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':
widget = bumblebee.output.Widget(
widget = core.widget.Widget(
name=widget_name, full_text=self.maxfreq)
widget.set('type', 'freq')
elif widget_name == 'cpu2.cpuload':
widget = bumblebee.output.Widget(
widget = core.widget.Widget(
name=widget_name, full_text=self.cpuload)
widget.set('type', 'load')
elif widget_name == 'cpu2.coresload':
widget = bumblebee.output.Widget(
widget = core.widget.Widget(
name=widget_name, full_text=self.coresload)
widget.set('type', 'loads')
elif widget_name == 'cpu2.temp':
widget = bumblebee.output.Widget(
widget = core.widget.Widget(
name=widget_name, full_text=self.temp)
widget.set('type', 'temp')
elif widget_name == 'cpu2.fanspeed':
widget = bumblebee.output.Widget(
widget = core.widget.Widget(
name=widget_name, full_text=self.fanspeed)
widget.set('type', 'fan')
if self.__colored:
widget.set('pango', True)
widget_list.append(widget)
self.widgets(widget_list)
self._colored = bumblebee.util.asbool(self.parameter('colored', 0))
self._temp_pattern = self.parameter('temp_pattern')
if self._temp_pattern is None:
self._temp = 'n/a'
self._fan_pattern = self.parameter('fan_pattern')
if self._fan_pattern is None:
self._fan = 'n/a'
self.__temp_pattern = self.parameter('temp_pattern')
if self.__temp_pattern is None:
self.__temp = 'n/a'
self.__fan_pattern = self.parameter('fan_pattern')
if self.__fan_pattern is None:
self.__fan = 'n/a'
# maxfreq is loaded only once at startup
if 'cpu2.maxfreq' in self._widget_names:
self._maxfreq = psutil.cpu_freq().max / 1000
self.update(widget_list)
if 'cpu2.maxfreq' in self.__widget_names:
self.__maxfreq = psutil.cpu_freq().max / 1000
def maxfreq(self, _):
return '{:.2f}GHz'.format(self._maxfreq)
return '{:.2f}GHz'.format(self.__maxfreq)
def cpuload(self, _):
return '{:>3}%'.format(self._cpuload)
return '{:>3}%'.format(self.__cpuload)
def add_color(self, bar):
"""add color as pango markup to a bar"""
if bar in ['', '']:
color = self.theme().color('green', 'green')
color = self.theme.color('green', 'green')
elif bar in ['', '']:
color = self.theme().color('yellow', 'yellow')
color = self.theme.color('yellow', 'yellow')
elif bar in ['', '']:
color = self.theme().color('orange', 'orange')
color = self.theme.color('orange', 'orange')
elif bar in ['', '']:
color = self.theme().color('red', 'red')
colored_bar = '<span foreground='{}'>{}</span>'.format(color, bar)
color = self.theme.color('red', 'red')
colored_bar = '<span foreground="{}">{}</span>'.format(color, bar)
return colored_bar
def coresload(self, _):
mono_bars = [bumblebee.output.hbar(x) for x in self._coresload]
if not self._colored:
mono_bars = [util.graph.hbar(x) for x in self.__coresload]
if not self.__colored:
return ''.join(mono_bars)
colored_bars = [self.add_color(x) for x in mono_bars]
return ''.join(colored_bars)
def temp(self, _):
if self._temp == 'n/a' or self._temp == 0:
if self.__temp == 'n/a' or self.__temp == 0:
return 'n/a'
return '{}°C'.format(self._temp)
return '{}°C'.format(self.__temp)
def fanspeed(self, _):
if self._fanspeed == 'n/a':
if self.__fanspeed == 'n/a':
return 'n/a'
return '{}RPM'.format(self._fanspeed)
return '{}RPM'.format(self.__fanspeed)
def _parse_sensors_output(self):
output = bumblebee.util.execute('sensors -u')
output = util.cli.execute('sensors -u')
lines = output.split('\n')
temp = 'n/a'
fan = 'n/a'
temp_line = None
fan_line = None
for line in lines:
if self._temp_pattern is not None and self._temp_pattern in line:
if self.__temp_pattern is not None and self.__temp_pattern in line:
temp_line = line
if self._fan_pattern is not None and self._fan_pattern in line:
if self.__fan_pattern is not None and self.__fan_pattern in line:
fan_line = line
if temp_line is not None and fan_line is not None:
break
@ -141,16 +141,18 @@ class Module(bumblebee.engine.Module):
fan = int(fan_line.split(':')[1].strip()[:-4])
return temp, fan
def update(self, _):
if 'cpu2.maxfreq' in self._widget_names:
self._maxfreq = psutil.cpu_freq().max / 1000
if 'cpu2.cpuload' in self._widget_names:
self._cpuload = round(psutil.cpu_percent(percpu=False))
if 'cpu2.coresload' in self._widget_names:
self._coresload = psutil.cpu_percent(percpu=True)
if 'cpu2.temp' in self._widget_names or 'cpu2.fanspeed' in self._widget_names:
self._temp, self._fanspeed = self._parse_sensors_output()
def update(self):
if 'cpu2.maxfreq' in self.__widget_names:
self.__maxfreq = psutil.cpu_freq().max / 1000
if 'cpu2.cpuload' in self.__widget_names:
self.__cpuload = round(psutil.cpu_percent(percpu=False))
if 'cpu2.coresload' in self.__widget_names:
self.__coresload = psutil.cpu_percent(percpu=True)
if 'cpu2.temp' in self.__widget_names or 'cpu2.fanspeed' in self.__widget_names:
self.__temp, self.__fanspeed = self._parse_sensors_output()
def state(self, widget):
"""for having per-widget icons"""
return [widget.get('type', '')]
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4