Add is_boolean and use when calling settings_set

The CLI always passes the value as a string so we need to manage this
type casting step by hand.
This commit is contained in:
Luke Murphy 2019-09-11 01:48:08 +01:00
parent b470c9192c
commit 25f270298f
No known key found for this signature in database
GPG key ID: 5E2EF5A63E3718CC

View file

@ -15,6 +15,25 @@ logger = getActionLogger('yunohost.settings')
SETTINGS_PATH = "/etc/yunohost/settings.json"
SETTINGS_PATH_OTHER_LOCATION = "/etc/yunohost/settings-%s.json"
def is_boolean(value):
"""
Ensure a string value is intended as a boolean
Keyword arguments:
arg -- The string to check
Returns:
Boolean
"""
if isinstance(value, bool):
return True
elif isinstance(value, basestring):
return str(value).lower() in ['true', 'on', 'yes', 'false', 'off', 'no']
else:
return False
# a settings entry is in the form of:
# namespace.subnamespace.name: {type, value, default, description, [choices]}
# choices is only for enum
@ -95,7 +114,7 @@ def settings_set(key, value):
key_type = settings[key]["type"]
if key_type == "bool":
if not isinstance(value, bool):
if not is_boolean(value):
raise YunohostError('global_settings_bad_type_for_setting', setting=key,
received_type=type(value).__name__, expected_type=key_type)
elif key_type == "int":