Browse code

Merge pull request #940 from bcl/master-acl_public

Fix handling of config tri-state bool values (like acl_public)

Florent Viard authored on 2018/03/04 05:31:52
Showing 1 changed files
... ...
@@ -44,6 +44,28 @@ def config_unicodise(string, encoding = "utf-8", errors = "replace"):
44 44
     except UnicodeDecodeError:
45 45
         raise UnicodeDecodeError("Conversion to unicode failed: %r" % string)
46 46
 
47
+def is_true(value):
48
+    """Check to see if a string is true, yes, on, or 1
49
+
50
+    value may be a str, or unicode.
51
+
52
+    Return True if it is
53
+    """
54
+    return value.lower() in ("true", "yes", "on", "1")
55
+
56
+def is_false(value):
57
+    """Check to see if a string is false, no, off, or 0
58
+
59
+    value may be a str, or unicode.
60
+
61
+    Return True if it is
62
+    """
63
+    return value.lower() in ("false", "no", "off", "0")
64
+
65
+def is_bool(value):
66
+    """Check a string value to see if it is bool"""
67
+    return is_true(value) or is_false(value)
68
+
47 69
 class Config(object):
48 70
     _instance = None
49 71
     _parsed_files = []
... ...
@@ -352,10 +374,12 @@ class Config(object):
352 352
                 raise ValueError("Config: value of option %s must have suffix m, k, or nothing, not '%s'" % (option, value))
353 353
 
354 354
         ## allow yes/no, true/false, on/off and 1/0 for boolean options
355
-        elif type(getattr(Config, option)) is type(True):   # bool
356
-            if str(value).lower() in ("true", "yes", "on", "1"):
355
+        ## Some options default to None, if that's the case check the value to see if it is bool
356
+        elif (type(getattr(Config, option)) is type(True) or              # Config is bool
357
+             (getattr(Config, option) is None and is_bool(value))):  # Config is None and value is bool
358
+            if is_true(value):
357 359
                 value = True
358
-            elif str(value).lower() in ("false", "no", "off", "0"):
360
+            elif is_false(value):
359 361
                 value = False
360 362
             else:
361 363
                 raise ValueError("Config: value of option '%s' must be Yes or No, not '%s'" % (option, value))