[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(): for module in config.modules():
modules.append(core.module.load(module, config)) modules.append(core.module.load(module, config))
modules[-1].theme = theme # TODO: make this nice (ctor)
output.modules(modules) output.modules(modules)
core.event.trigger('start') core.event.trigger('start')
while True: while True:

View file

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

View file

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

View file

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