[modules/battery-upower] Do not throw exception (dbus errors)

Try to "gracefully" display n/a instead.
This commit is contained in:
Tobias Witek 2019-10-20 15:20:48 +02:00
parent 8481874cb7
commit bc621823b2
2 changed files with 24 additions and 12 deletions

View file

@ -9,6 +9,7 @@ Parameters:
""" """
import dbus import dbus
import logging
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
@ -194,9 +195,12 @@ class UPowerManager():
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity))
try:
self.power = UPowerManager() self.power = UPowerManager()
self.device = self.power.get_display_device() self.device = self.power.get_display_device()
super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity)) 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, engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-power-statistics") cmd="gnome-power-statistics")
@ -207,6 +211,8 @@ class Module(bumblebee.engine.Module):
def capacity(self, widget): def capacity(self, widget):
widget.set("capacity", -1) widget.set("capacity", -1)
widget.set("ac", False) widget.set("ac", False)
output = "n/a"
try:
capacity = int(self.power.get_device_percentage(self.device)) capacity = int(self.power.get_device_percentage(self.device))
capacity = capacity if capacity < 100 else 100 capacity = capacity if capacity < 100 else 100
widget.set("capacity", capacity) widget.set("capacity", capacity)
@ -216,6 +222,8 @@ class Module(bumblebee.engine.Module):
if bumblebee.util.asbool(self.parameter("showremaining", True)) \ if bumblebee.util.asbool(self.parameter("showremaining", True)) \
and self.power.get_state(self.device) == "Discharging": and self.power.get_state(self.device) == "Discharging":
output = "{} {}".format(output, self.remaining()) output = "{} {}".format(output, self.remaining())
except Exception as e:
logging.exception("unable to get battery capacity: {}".format(str(e)))
return output return output
@ -233,7 +241,11 @@ class Module(bumblebee.engine.Module):
if widget.get("ac"): if widget.get("ac"):
state.append("AC") state.append("AC")
else: else:
charge = "Unknown"
try:
charge = self.power.get_state(self.device) charge = self.power.get_state(self.device)
except Exception as e:
logging.exception("unable to get charge value: {}".format(str(e)))
if charge == "Discharging": if charge == "Discharging":
state.append("discharging-{}".format(min([10, 25, 50, 80, 100], key=lambda i: abs(i - capacity)))) state.append("discharging-{}".format(min([10, 25, 50, 80, 100], key=lambda i: abs(i - capacity))))
elif charge == "Unknown": elif charge == "Unknown":

View file

@ -30,7 +30,7 @@ class Module(bumblebee.engine.Module):
for battery in os.listdir('/sys/class/power_supply/'): for battery in os.listdir('/sys/class/power_supply/'):
if not any(i in battery for i in ['AC', 'hidpp']): if not any(i in battery for i in ['AC', 'hidpp']):
self._batteries.append("/sys/class/power_supply/" + battery) self._batteries.append("/sys/class/power_supply/" + battery)
except as e: except Exception as e:
logging.exception("unable to detect batteries: {}".format(str(e))) logging.exception("unable to detect batteries: {}".format(str(e)))
super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity)) super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity))