diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index a22c8d8..5d00768 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -6,7 +6,7 @@ Parameters: * battery.device : Comma-separated list of battery devices to read information from (defaults to auto for auto-detection) * battery.warning : Warning threshold in % of remaining charge (defaults to 20) * battery.critical : Critical threshold in % of remaining charge (defaults to 10) - * battery.showdevice : If set to "true", add the device name to the widget + * battery.showdevice : If set to "true", add the device name to the widget (defaults to False) """ import os @@ -15,6 +15,7 @@ import glob import bumblebee.input import bumblebee.output import bumblebee.engine +import bumblebee.util class Module(bumblebee.engine.Module): def __init__(self, engine, config): @@ -57,7 +58,7 @@ class Module(bumblebee.engine.Module): return "n/a" capacity = capacity if capacity < 100 else 100 widget.set("capacity", capacity) - if self.parameter("showdevice") == "true": + if bumblebee.util.asbool(self.parameter("showdevice", False)): widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name))) return "{}% ({})".format(capacity, os.path.basename(widget.name)) widget.set("theme.minwidth", "100%") diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index 0ed4895..e679794 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -17,6 +17,7 @@ import os import bumblebee.input import bumblebee.output import bumblebee.engine +import bumblebee.util class Module(bumblebee.engine.Module): def __init__(self, engine, config): @@ -24,9 +25,9 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text=self.diskspace) ) self._path = self.parameter("path", "/") - self._sused = self.parameter("showUsed", "yes") - self._ssize = self.parameter("showSize", "yes") - self._spercent = self.parameter("showPercent", "yes") + self._sused = bumblebee.util.asbool(self.parameter("showUsed", True)) + self._ssize = bumblebee.util.asbool(self.parameter("showSize", True)) + self._spercent = bumblebee.util.asbool(self.parameter("showPercent", True)) self._app = self.parameter("open", "xdg-open") self._perc = 0 self._used = 0 @@ -37,19 +38,19 @@ class Module(bumblebee.engine.Module): self._path)) def diskspace(self, widget): - if self._sused == "yes": + if self._sused: used_str = bumblebee.util.bytefmt(self._used) else: used_str = "" - if self._ssize == "yes": + if self._ssize: size_str = bumblebee.util.bytefmt(self._size) else: size_str = "" - if self._spercent == "yes": + if self._spercent: percent_str = self._perc else: percent_str = "" - if self._sused != "yes" or self._ssize != "yes": + if not self._sused or not self._ssize: separator = "" else: separator = "/" diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 9f38624..42816b9 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -5,7 +5,7 @@ Parameters: * memory.warning : Warning threshold in % of memory used (defaults to 80%) * memory.critical: Critical threshold in % of memory used (defaults to 90%) - * memory.usedonly: Only show the amount of RAM in use. + * memory.usedonly: Only show the amount of RAM in use (defaults to False). """ try: @@ -29,7 +29,7 @@ class Module(bumblebee.engine.Module): def memory_usage(self, widget): used = self._mem.total - self._mem.available - if bool(self.parameter("usedonly", 0)) == 1: + if bumblebee.util.asbool(self.parameter("usedonly", False)): return bumblebee.util.bytefmt(used) return "{}/{} ({:05.02f}%)".format( bumblebee.util.bytefmt(used), diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index eeba28c..5e60d55 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -111,7 +111,7 @@ class Module(bumblebee.engine.Module): return except Exception: self._failed = True - if self.parameter("autostart", "false") == "true": + if bumblebee.util.asbool(self.parameter("autostart", False)): try: bumblebee.util.execute("pulseaudio --start") self.update(widgets) diff --git a/bumblebee/modules/stock.py b/bumblebee/modules/stock.py index 2297c3a..390c3dd 100644 --- a/bumblebee/modules/stock.py +++ b/bumblebee/modules/stock.py @@ -15,6 +15,8 @@ Parameters: import bumblebee.input import bumblebee.output import bumblebee.engine +import bumblebee.util + import requests from requests.exceptions import RequestException @@ -24,7 +26,7 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text=self.value) ) self._symbols = self.parameter("symbols", "") - self._change = self.parameter("change", True) + self._change = bumblebee.util.asbool(self.parameter("change", True)) self._currencies = self.parameter('currencies', None) self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._value = self.fetch() diff --git a/bumblebee/util.py b/bumblebee/util.py index 9efa146..30719ea 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -10,6 +10,15 @@ except ImportError: # Python3 doesn't require this anymore pass + +def asbool(val): + if val is None: + return False + if isinstance(val, bool): + return val + val = str(val).strip().lower() + return val in ("t", "true", "y", "yes", "on", "1") + def execute(cmd, wait=True): logging.info("executing command '{}'".format(cmd)) args = shlex.split(cmd)