From bc621823b28f9dd48764e13bbd8dea95c689d624 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 20 Oct 2019 15:20:48 +0200 Subject: [PATCH] [modules/battery-upower] Do not throw exception (dbus errors) Try to "gracefully" display n/a instead. --- bumblebee/modules/battery-upower.py | 34 +++++++++++++++++++---------- bumblebee/modules/battery_all.py | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/bumblebee/modules/battery-upower.py b/bumblebee/modules/battery-upower.py index 0f039b5..2e9aa13 100644 --- a/bumblebee/modules/battery-upower.py +++ b/bumblebee/modules/battery-upower.py @@ -9,6 +9,7 @@ Parameters: """ import dbus +import logging import bumblebee.input import bumblebee.output @@ -194,9 +195,12 @@ class UPowerManager(): class Module(bumblebee.engine.Module): def __init__(self, engine, config): - self.power = UPowerManager() - self.device = self.power.get_display_device() super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity)) + try: + self.power = UPowerManager() + self.device = self.power.get_display_device() + except Exception as e: + logging.exception("unable to get battery display device: {}".format(str(e))) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-power-statistics") @@ -207,15 +211,19 @@ class Module(bumblebee.engine.Module): def capacity(self, widget): widget.set("capacity", -1) widget.set("ac", False) - capacity = int(self.power.get_device_percentage(self.device)) - capacity = capacity if capacity < 100 else 100 - widget.set("capacity", capacity) - output = "{}%".format(capacity) - widget.set("theme.minwidth", "100%") + output = "n/a" + try: + capacity = int(self.power.get_device_percentage(self.device)) + capacity = capacity if capacity < 100 else 100 + widget.set("capacity", capacity) + output = "{}%".format(capacity) + widget.set("theme.minwidth", "100%") - if bumblebee.util.asbool(self.parameter("showremaining", True)) \ - and self.power.get_state(self.device) == "Discharging": - output = "{} {}".format(output, self.remaining()) + if bumblebee.util.asbool(self.parameter("showremaining", True)) \ + and self.power.get_state(self.device) == "Discharging": + output = "{} {}".format(output, self.remaining()) + except Exception as e: + logging.exception("unable to get battery capacity: {}".format(str(e))) return output @@ -233,7 +241,11 @@ class Module(bumblebee.engine.Module): if widget.get("ac"): state.append("AC") else: - charge = self.power.get_state(self.device) + charge = "Unknown" + try: + charge = self.power.get_state(self.device) + except Exception as e: + logging.exception("unable to get charge value: {}".format(str(e))) if charge == "Discharging": state.append("discharging-{}".format(min([10, 25, 50, 80, 100], key=lambda i: abs(i - capacity)))) elif charge == "Unknown": diff --git a/bumblebee/modules/battery_all.py b/bumblebee/modules/battery_all.py index 9882d7b..2fe965c 100644 --- a/bumblebee/modules/battery_all.py +++ b/bumblebee/modules/battery_all.py @@ -30,7 +30,7 @@ class Module(bumblebee.engine.Module): for battery in os.listdir('/sys/class/power_supply/'): if not any(i in battery for i in ['AC', 'hidpp']): self._batteries.append("/sys/class/power_supply/" + battery) - except as e: + except Exception as e: logging.exception("unable to detect batteries: {}".format(str(e))) super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity))