Commit 27198876 authored by Michael Büsch's avatar Michael Büsch

Move str2bool to common

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent 787e1144
......@@ -167,6 +167,19 @@ def awlFileWrite(filename, data, encoding="latin_1"):
with contextlib.suppress(IOError, OSError):
os.unlink(tmpFile)
def str2bool(string, default=False):
"""Convert a human readable string to a boolean.
"""
s = string.lower().strip()
if s in {"true", "yes", "on", "enable", "enabled"}:
return True
if s in {"false", "no", "off", "disable", "disabled"}:
return False
try:
return bool(int(s, 10))
except ValueError:
return default
def strToBase64(string, ignoreErrors=False):
"""Convert a string to a base64 encoded ascii string.
Throws ValueError on errors, if ignoreErrors is False."""
......
......@@ -77,17 +77,6 @@ def strPartitionFull(string, sep, keepEmpty=True):
first = False
return ret
def str2bool(string, default=False):
s = string.lower()
if s in ("true", "yes", "on", "enable", "enabled"):
return True
if s in ("false", "no", "off", "disable", "disabled"):
return False
try:
return bool(int(s, 10))
except ValueError:
return default
# Returns value, if value is a list.
# Returns a list with the elements of value, if value is a tuple.
# Returns a list with the elements of value, if value is a set.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment