From 0d3493ef73ff1c5b939d1030aa3784063323179a Mon Sep 17 00:00:00 2001 From: Antonio Quartulli Date: Wed, 29 Nov 2017 16:24:56 +0800 Subject: [PATCH 1/3] 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 From 79054037a760b190214650f0ba5a950f85d5b732 Mon Sep 17 00:00:00 2001 From: Antonio Quartulli Date: Fri, 29 Dec 2017 19:55:30 +0800 Subject: [PATCH 2/3] conf: make config file a CLI parameter The config file might be stores in an alternate location for several reasons (i.e. testing different configs or running multiple nodes on the same host). Make the config file a CLI parameter so that it can be specified when launching the lbrynet-daemon. Related to #1039 Signed-off-by: Antonio Quartulli --- lbrynet/conf.py | 14 ++++++++++++-- lbrynet/daemon/DaemonControl.py | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lbrynet/conf.py b/lbrynet/conf.py index 014019806..3fad69588 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -56,6 +56,8 @@ settings_encoders = { '.yml': yaml.safe_dump } +# set by CLI when the user specifies an alternate config file path +conf_file = None def _win_path_to_bytes(path): """ @@ -476,7 +478,11 @@ class Config(object): } def save_conf_file_settings(self): - path = self.get_conf_filename() + if conf_file: + path = conf_file + else: + path = self.get_conf_filename() + ext = os.path.splitext(path)[1] encoder = settings_encoders.get(ext, False) assert encoder is not False, 'Unknown settings format %s' % ext @@ -484,7 +490,11 @@ class Config(object): settings_file.write(encoder(self._data[TYPE_PERSISTED])) def load_conf_file_settings(self): - path = self.get_conf_filename() + if conf_file: + path = conf_file + else: + path = self.get_conf_filename() + ext = os.path.splitext(path)[1] decoder = settings_decoders.get(ext, False) assert decoder is not False, 'Unknown settings format %s' % ext diff --git a/lbrynet/daemon/DaemonControl.py b/lbrynet/daemon/DaemonControl.py index db60ce01c..2638bca4b 100644 --- a/lbrynet/daemon/DaemonControl.py +++ b/lbrynet/daemon/DaemonControl.py @@ -20,9 +20,18 @@ def test_internet_connection(): def start(): """The primary entry point for launching the daemon.""" - conf.initialize_settings() + + # postpone loading the config file to after the CLI arguments + # have been parsed, as they may contain an alternate config file location + conf.initialize_settings(load_conf_file=False) parser = argparse.ArgumentParser(description="Launch lbrynet-daemon") + parser.add_argument( + "--conf", + help="specify an alternative configuration file", + type=str, + default=None + ) parser.add_argument( "--wallet", help="lbryum or ptc for testing, default lbryum", @@ -49,6 +58,8 @@ def start(): args = parser.parse_args() update_settings_from_args(args) + conf.settings.load_conf_file_settings() + if args.version: version = system_info.get_platform(get_ip=False) version['installation_id'] = conf.settings.installation_id @@ -83,6 +94,8 @@ def update_settings_from_args(args): 'wallet': args.wallet, }, data_types=(conf.TYPE_CLI,)) + conf.conf_file = args.conf + @defer.inlineCallbacks def start_server_and_listen(use_auth, analytics_manager): From 1a234b0b88026315f8de1dcd1eeccb7bd2444107 Mon Sep 17 00:00:00 2001 From: Antonio Quartulli Date: Fri, 29 Dec 2017 20:14:27 +0800 Subject: [PATCH 3/3] Update CHANGELOG.md Signed-off-by: Antonio Quartulli --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb821fa83..dc6312aab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ at anytime. * Add `peer_port` to settings configurable using `settings_set` * Added an option to disable max key fee check. * Add `wallet_unlock`, a command available during startup to unlock an encrypted wallet + * Add `--conf` CLI flag to specify an alternate config file ### Changed * claim_show API command no longer takes name as argument