diff --git a/core/module.py b/core/module.py index 49f8016..7aeac52 100644 --- a/core/module.py +++ b/core/module.py @@ -19,8 +19,9 @@ def load(module_name, config=None): mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name)) return getattr(mod, 'Module')(config) except ModuleNotFoundError as e: - pass + log.fatal('failed to import {}: {}'.format(module_name, e)) except ImportError as e: + log.fatal('failed to import {}: {}'.format(module_name, e)) error = str(e) if not error: error = 'No such module' diff --git a/doc/NOTES.md b/doc/NOTES.md index 1ad3a97..edb06d9 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -20,6 +20,7 @@ - WAL support / colorscheme support - tkinter / popups - scrolling decorator (incl. minwidth, alignment) +- theme.exclude?? ## Improvements - pango output (improve - maybe autodetect? see #531) diff --git a/modules/core/battery.py b/modules/core/battery.py index 31aae42..ffe39fe 100644 --- a/modules/core/battery.py +++ b/modules/core/battery.py @@ -13,7 +13,11 @@ Parameters: import os import glob -import power +import logging +try: + import power +except ImportError: + pass import core.module import core.widget @@ -21,6 +25,8 @@ import core.input import util.format +log = logging.getLogger(__name__) + class Module(core.module.Module): def __init__(self, config=None): widgets = [] @@ -33,24 +39,16 @@ class Module(core.module.Module): self._batteries = ['/sys/class/power_supply/{}'.format(b) for b in self._batteries] if len(self._batteries) == 0: raise Exceptions('no batteries configured/found') - self.update() core.input.register(self, button=core.input.LEFT_MOUSE, cmd='gnome-power-statistics') - def update(self, widgets): - new_widgets = [] for path in self._batteries: - widget = self.widget(path) - if not widget: - widget = bumblebee.output.Widget(full_text=self.capacity, name=path) - new_widgets.append(widget) - self.capacity(widget) - while len(widgets) > 0: del widgets[0] - for widget in new_widgets: - if bumblebee.util.asbool(self.parameter('decorate', True)) == False: - widget.set('theme.exclude', 'suffix') + log.debug('adding new widget for {}'.format(path)) + widget = core.widget.Widget(full_text=self.capacity, name=path, module=self) widgets.append(widget) - self._widgets = widgets + self.capacity(widget) + if util.format.asbool(self.parameter('decorate', True)) == False: + widget.set('theme.exclude', 'suffix') def remaining(self): estimate = 0.0 @@ -63,18 +61,18 @@ class Module(core.module.Module): return '' except Exception: return '' - return bumblebee.util.durationfmt(estimate*60, shorten=True, suffix=True) # estimate is in minutes + return util.format.duration(estimate*60, shorten=True, suffix=True) # estimate is in minutes def capacity(self, widget): widget.set('capacity', -1) widget.set('ac', False) - if not os.path.exists(widget.name): + if not os.path.exists(widget.name()): widget.set('capacity', 100) widget.set('ac', True) return 'ac' capacity = 100 try: - with open('{}/capacity'.format(widget.name)) as f: + with open('{}/capacity'.format(widget.name())) as f: capacity = int(f.read()) except IOError: return 'n/a' @@ -83,18 +81,18 @@ class Module(core.module.Module): widget.set('capacity', capacity) # Read power conumption - if bumblebee.util.asbool(self.parameter('showpowerconsumption', False)): - r=open(widget.name + '/power_now', 'r') + if util.format.asbool(self.parameter('showpowerconsumption', False)): + r=open(widget.name() + '/power_now', 'r') output = '{}% ({})'.format(capacity,str(int(r.read())/1000000) + 'W') else: output = '{}%'.format(capacity) widget.set('theme.minwidth', '100%') - if bumblebee.util.asbool(self.parameter('showremaining', True))\ + if util.format.asbool(self.parameter('showremaining', True))\ and self.getCharge(widget) == 'Discharging': output = '{} {}'.format(output, self.remaining()) - if bumblebee.util.asbool(self.parameter('showdevice', False)): + if util.format.asbool(self.parameter('showdevice', False)): output = '{} ({})'.format(output, os.path.basename(widget.name)) return output @@ -104,6 +102,7 @@ class Module(core.module.Module): capacity = widget.get('capacity') if capacity < 0: + log.debug('battery state: {}'.format(state)) return ['critical', 'unknown'] if capacity < int(self.parameter('critical', 10)): @@ -124,15 +123,15 @@ class Module(core.module.Module): state.append('charged') else: state.append('charging') - return state def getCharge(self, widget): charge = '' try: - with open('{}/status'.format(widget.name)) as f: + with open('{}/status'.format(widget.name())) as f: charge = f.read().strip() except IOError: pass return charge + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/util/format.py b/util/format.py index dc5cda9..141ef32 100644 --- a/util/format.py +++ b/util/format.py @@ -30,4 +30,19 @@ def byte(val, fmt='{:.2f}'): val /= 1024.0 return '{}GiB'.format(fmt).format(val*1024.0) +def duration(duration, shorten=False, suffix=False): + duration = int(duration) + minutes, seconds = divmod(duration, 60) + hours, minutes = divmod(minutes, 60) + suf = 'm' + res = '{:02d}:{:02d}'.format(minutes, seconds) + if hours > 0: + if shorten: + res = '{:02d}:{:02d}'.format(hours, minutes) + else: + res = '{:02d}:{}'.format(hours, res) + suf = 'h' + + return '{}{}'.format(res, suf if suffix else '') + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4