Commits

Anonymous committed 36d5c6b

subssh/tools.py: to_bool(): Boolean values defined explicitely for True and False. Raise ValueError if not valid value.

  • Participants
  • Parent commits 45c5aa2

Comments (0)

Files changed (1)

File subssh/tools.py

     """Exception class that will not get logged as system error when uncaught
     """
 
+_LOWER_BOOL_STRS_TRUE = ("true", "1", "enabled", "yes")
+_LOWER_BOOL_STRS_FALSE = ("false", "0", "disabled", "no")
+
 def to_bool(value):
-        return str(value).lower() in ("true", "1",  "enabled")
-
+    """Return a boolean value represented by `value` string.
+    
+    Valid values for True: "true", "1", "enabled" or "yes"
+    Valid values for False: "false", "0", "disabled" or "no"
+    
+    Case of the `value` is ignored. Raise ValueError if `value` is invalid.
+    """
+    
+    val = value.lower()
+    if val in _LOWER_BOOL_STRS_TRUE:
+        return True
+    elif val in _LOWER_BOOL_STRS_FALSE:
+        return False
+    raise ValueError("%s is not a valid boolean value" % value)
 
 def to_cmd_args(input):
     if isinstance(input, str):