forked from LBRYCommunity/lbry-sdk
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:
parent
21d9b9bc71
commit
0d3493ef73
1 changed files with 27 additions and 0 deletions
|
@ -311,6 +311,11 @@ class Config(object):
|
||||||
TYPE_RUNTIME, TYPE_CLI, TYPE_ENV, TYPE_PERSISTED, TYPE_DEFAULT
|
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(self._fixed_defaults)
|
||||||
self._data[TYPE_DEFAULT].update(
|
self._data[TYPE_DEFAULT].update(
|
||||||
{k: v[1] for (k, v) in self._adjustable_defaults.iteritems()})
|
{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():
|
if currency not in self._fixed_defaults['CURRENCIES'].keys():
|
||||||
raise InvalidCurrencyError(currency)
|
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):
|
def get(self, name, data_type=None):
|
||||||
"""Get a config value
|
"""Get a config value
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue