Add bumblebee.util.asbool function

Harmonize the boolean parameter value. Now `t`, `true`, `y`, `yes`, `on`, `1` are considered truthy and
everything else falsy.
This commit is contained in:
Frederic Junod 2017-07-08 06:44:08 +02:00
parent cc6da2b70e
commit b0268a412b
6 changed files with 26 additions and 13 deletions

View file

@ -6,7 +6,7 @@ Parameters:
* battery.device : Comma-separated list of battery devices to read information from (defaults to auto for auto-detection) * 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.warning : Warning threshold in % of remaining charge (defaults to 20)
* battery.critical : Critical threshold in % of remaining charge (defaults to 10) * 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 import os
@ -15,6 +15,7 @@ import glob
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
import bumblebee.util
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
@ -57,7 +58,7 @@ class Module(bumblebee.engine.Module):
return "n/a" return "n/a"
capacity = capacity if capacity < 100 else 100 capacity = capacity if capacity < 100 else 100
widget.set("capacity", capacity) 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))) widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name)))
return "{}% ({})".format(capacity, os.path.basename(widget.name)) return "{}% ({})".format(capacity, os.path.basename(widget.name))
widget.set("theme.minwidth", "100%") widget.set("theme.minwidth", "100%")

View file

@ -17,6 +17,7 @@ import os
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
import bumblebee.util
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
@ -24,9 +25,9 @@ class Module(bumblebee.engine.Module):
bumblebee.output.Widget(full_text=self.diskspace) bumblebee.output.Widget(full_text=self.diskspace)
) )
self._path = self.parameter("path", "/") self._path = self.parameter("path", "/")
self._sused = self.parameter("showUsed", "yes") self._sused = bumblebee.util.asbool(self.parameter("showUsed", True))
self._ssize = self.parameter("showSize", "yes") self._ssize = bumblebee.util.asbool(self.parameter("showSize", True))
self._spercent = self.parameter("showPercent", "yes") self._spercent = bumblebee.util.asbool(self.parameter("showPercent", True))
self._app = self.parameter("open", "xdg-open") self._app = self.parameter("open", "xdg-open")
self._perc = 0 self._perc = 0
self._used = 0 self._used = 0
@ -37,19 +38,19 @@ class Module(bumblebee.engine.Module):
self._path)) self._path))
def diskspace(self, widget): def diskspace(self, widget):
if self._sused == "yes": if self._sused:
used_str = bumblebee.util.bytefmt(self._used) used_str = bumblebee.util.bytefmt(self._used)
else: else:
used_str = "" used_str = ""
if self._ssize == "yes": if self._ssize:
size_str = bumblebee.util.bytefmt(self._size) size_str = bumblebee.util.bytefmt(self._size)
else: else:
size_str = "" size_str = ""
if self._spercent == "yes": if self._spercent:
percent_str = self._perc percent_str = self._perc
else: else:
percent_str = "" percent_str = ""
if self._sused != "yes" or self._ssize != "yes": if not self._sused or not self._ssize:
separator = "" separator = ""
else: else:
separator = "/" separator = "/"

View file

@ -5,7 +5,7 @@
Parameters: Parameters:
* memory.warning : Warning threshold in % of memory used (defaults to 80%) * memory.warning : Warning threshold in % of memory used (defaults to 80%)
* memory.critical: Critical threshold in % of memory used (defaults to 90%) * 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: try:
@ -29,7 +29,7 @@ class Module(bumblebee.engine.Module):
def memory_usage(self, widget): def memory_usage(self, widget):
used = self._mem.total - self._mem.available 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 bumblebee.util.bytefmt(used)
return "{}/{} ({:05.02f}%)".format( return "{}/{} ({:05.02f}%)".format(
bumblebee.util.bytefmt(used), bumblebee.util.bytefmt(used),

View file

@ -111,7 +111,7 @@ class Module(bumblebee.engine.Module):
return return
except Exception: except Exception:
self._failed = True self._failed = True
if self.parameter("autostart", "false") == "true": if bumblebee.util.asbool(self.parameter("autostart", False)):
try: try:
bumblebee.util.execute("pulseaudio --start") bumblebee.util.execute("pulseaudio --start")
self.update(widgets) self.update(widgets)

View file

@ -15,6 +15,8 @@ Parameters:
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
import bumblebee.util
import requests import requests
from requests.exceptions import RequestException from requests.exceptions import RequestException
@ -24,7 +26,7 @@ class Module(bumblebee.engine.Module):
bumblebee.output.Widget(full_text=self.value) bumblebee.output.Widget(full_text=self.value)
) )
self._symbols = self.parameter("symbols", "") 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._currencies = self.parameter('currencies', None)
self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv'
self._value = self.fetch() self._value = self.fetch()

View file

@ -10,6 +10,15 @@ except ImportError:
# Python3 doesn't require this anymore # Python3 doesn't require this anymore
pass 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): def execute(cmd, wait=True):
logging.info("executing command '{}'".format(cmd)) logging.info("executing command '{}'".format(cmd))
args = shlex.split(cmd) args = shlex.split(cmd)