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:
parent
cc6da2b70e
commit
b0268a412b
6 changed files with 26 additions and 13 deletions
|
@ -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%")
|
||||
|
|
|
@ -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 = "/"
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue