config.yml updating and reloaading default works

This commit is contained in:
Jaikinator
2023-12-13 17:40:32 +01:00
parent 9a8cdb2a64
commit 7c200d4506
2 changed files with 22 additions and 35 deletions
+3 -3
View File
@@ -8,9 +8,9 @@ launch:
max-threads: 40 max-threads: 40
quiet: false quiet: false
auth: auth:
enabled: false auth_enabled: false
username: admin auth_username: admin
password: admin auth_password: admin
auth_message: null auth_message: null
show_error : false show_error : false
favicon_path : null favicon_path : null
+19 -32
View File
@@ -10,7 +10,7 @@ class ConfigLoader:
self.config = config 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. Restores specified keys to their default values, including nested keys.
@@ -20,8 +20,8 @@ class ConfigLoader:
""" """
default_config = self.get_default_config() default_config = self.get_default_config()
for key in args:
self.apply_overrides(self.config, default_config, keys) self.apply_overrides(self.config, default_config, key)
@@ -52,42 +52,29 @@ class ConfigLoader:
return cls(config) return cls(config)
@staticmethod @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. """ """ 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(): 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 isinstance(value, dict):
# If the value is a dict, apply recursively # If the value is a dict, apply recursively
sub_dict = orig_dict.get(key, {}) 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 orig_dict[key] = sub_dict
else: else:
# Apply override for this key # Apply override for this key
print("HI iam here", key, value) if specific is None:
orig_dict[key] = value # If no specific keys are provided, update the key
print("HI", orig_dict) # If the value is not a dict, search for the key and update
if ConfigLoader.update_nested_key(orig_dict, key, value):
# @staticmethod continue # Key was found and updated
# def apply_overrides(orig_dict, override_dict): orig_dict[key] = value # Key not found, update at this level
# """ Recursively apply overrides to the configuration. """ elif key in specific:
# for key, value in override_dict.items(): # If specific keys are provided, only update if the key is in the list
# if isinstance(value, dict): if ConfigLoader.update_nested_key(orig_dict, specific, value):
# # If the value is a dict, apply recursively continue # Key was found and updated
# ConfigLoader.apply_overrides(orig_dict.get(key, {}), value) orig_dict[specific] = 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
@staticmethod @staticmethod
def update_nested_key(d, key, value): def update_nested_key(d, key, value):
@@ -144,8 +131,8 @@ class AppConfig(ConfigLoader):
launch_options = self.config.get("launch") launch_options = self.config.get("launch")
if launch_options.get('auth').pop('enabled'): if launch_options.get('auth').pop('auth_enabled'):
self.config['launch']['auth'] = (launch_options.get('auth').pop('username'), self.config['launch']['auth'] = (launch_options.get('auth').pop('auth_username'),
launch_options.get('auth').pop('password')) launch_options.get('auth').pop('password'))
else: else:
self.config['launch']['auth'] = None self.config['launch']['auth'] = None