From 7c200d4506b7c21711346cd0de1ab55dc3f76aec Mon Sep 17 00:00:00 2001 From: Jaikinator Date: Wed, 13 Dec 2023 17:40:32 +0100 Subject: [PATCH] config.yml updating and reloaading default works --- scraibe/app/config.yml | 6 ++--- scraibe/app/utils.py | 51 ++++++++++++++++-------------------------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/scraibe/app/config.yml b/scraibe/app/config.yml index 4eda2e4..ad6ce98 100644 --- a/scraibe/app/config.yml +++ b/scraibe/app/config.yml @@ -8,9 +8,9 @@ launch: max-threads: 40 quiet: false auth: - enabled: false - username: admin - password: admin + auth_enabled: false + auth_username: admin + auth_password: admin auth_message: null show_error : false favicon_path : null diff --git a/scraibe/app/utils.py b/scraibe/app/utils.py index cff8b9e..17950c5 100644 --- a/scraibe/app/utils.py +++ b/scraibe/app/utils.py @@ -10,7 +10,7 @@ class ConfigLoader: self.config = config - def restore_defaults_for_keys(self, keys): + def restore_defaults_for_keys(self, *args): """ Restores specified keys to their default values, including nested keys. @@ -20,8 +20,8 @@ class ConfigLoader: """ default_config = self.get_default_config() - - self.apply_overrides(self.config, default_config, keys) + for key in args: + self.apply_overrides(self.config, default_config, key) @@ -52,42 +52,29 @@ class ConfigLoader: return cls(config) @staticmethod - def apply_overrides(orig_dict, override_dict, specific_keys=None): + def apply_overrides(orig_dict, override_dict, specific=None): """ Recursively apply overrides to the configuration, only for specific keys. """ - if specific_keys is None: - specific_keys = override_dict.keys() # If no specific keys provided, apply to all keys - - for key, value in override_dict.items(): - if key not in specific_keys: - - continue # Skip keys not in the specific keys set - if isinstance(value, dict): # If the value is a dict, apply recursively sub_dict = orig_dict.get(key, {}) - ConfigLoader.apply_overrides(sub_dict, value, specific_keys) + ConfigLoader.apply_overrides(sub_dict, value, specific) orig_dict[key] = sub_dict else: # Apply override for this key - print("HI iam here", key, value) - orig_dict[key] = value - print("HI", orig_dict) - - # @staticmethod - # def apply_overrides(orig_dict, override_dict): - - # """ Recursively apply overrides to the configuration. """ - # for key, value in override_dict.items(): - # if isinstance(value, dict): - # # If the value is a dict, apply recursively - # ConfigLoader.apply_overrides(orig_dict.get(key, {}), value) - # else: - # # If the value is not a dict, search for the key and update - # if ConfigLoader.update_nested_key(orig_dict, key, value): - # continue # Key was found and updated - # orig_dict[key] = value # Key not found, update at this level + if specific is None: + # If no specific keys are provided, update the key + # If the value is not a dict, search for the key and update + if ConfigLoader.update_nested_key(orig_dict, key, value): + continue # Key was found and updated + orig_dict[key] = value # Key not found, update at this level + + elif key in specific: + # If specific keys are provided, only update if the key is in the list + if ConfigLoader.update_nested_key(orig_dict, specific, value): + continue # Key was found and updated + orig_dict[specific] = value @staticmethod def update_nested_key(d, key, value): @@ -144,8 +131,8 @@ class AppConfig(ConfigLoader): launch_options = self.config.get("launch") - if launch_options.get('auth').pop('enabled'): - self.config['launch']['auth'] = (launch_options.get('auth').pop('username'), + if launch_options.get('auth').pop('auth_enabled'): + self.config['launch']['auth'] = (launch_options.get('auth').pop('auth_username'), launch_options.get('auth').pop('password')) else: self.config['launch']['auth'] = None