From 0d3493ef73ff1c5b939d1030aa3784063323179a Mon Sep 17 00:00:00 2001 From: Antonio Quartulli Date: Wed, 29 Nov 2017 16:24:56 +0800 Subject: [PATCH] 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 --- lbrynet/conf.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lbrynet/conf.py b/lbrynet/conf.py index de48a7c65..014019806 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -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