conf: add method to check if a setting has default value

Sometimes it might be helpful to check if a given config
setting is still the default or if it was touched by the user
in any way (e.g. env, config file, ...).

Add is_default() method to Config object to perform such check.

Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
This commit is contained in:
Antonio Quartulli 2017-11-29 16:24:56 +08:00
parent 21d9b9bc71
commit 0d3493ef73
No known key found for this signature in database
GPG key ID: 07A53C580EF2CD74

View file

@ -311,6 +311,11 @@ class Config(object):
TYPE_RUNTIME, TYPE_CLI, TYPE_ENV, TYPE_PERSISTED, TYPE_DEFAULT
)
# types of data where user specified config values can be stored
self._user_specified = (
TYPE_RUNTIME, TYPE_CLI, TYPE_ENV, TYPE_PERSISTED
)
self._data[TYPE_DEFAULT].update(self._fixed_defaults)
self._data[TYPE_DEFAULT].update(
{k: v[1] for (k, v) in self._adjustable_defaults.iteritems()})
@ -383,6 +388,28 @@ class Config(object):
if currency not in self._fixed_defaults['CURRENCIES'].keys():
raise InvalidCurrencyError(currency)
def is_default(self, name):
"""Check if a config value is wasn't specified by the user
Args:
name: the name of the value to check
Returns: true if config value is the default one, false if it was specified by
the user
Sometimes it may be helpful to understand if a config value was specified
by the user or if it still holds its default value. This function will return
true when the config value is still the default. Note that when the user
specifies a value that is equal to the default one, it will still be considered
as 'user specified'
"""
self._assert_valid_setting(name)
for possible_data_type in self._user_specified:
if name in self._data[possible_data_type]:
return False
return True
def get(self, name, data_type=None):
"""Get a config value