Skip to content
Snippets Groups Projects
Commit 74b21b0e authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Disallow overwriting config settings via python attribute assignment

parent 7f8a7a93
No related branches found
No related tags found
1 merge request!4ENH: Config file templating
......@@ -56,6 +56,12 @@ specifying a directory.
"""
class ConfigError(Exception):
"""Custom Exception class used by the Config class. Raised by
Config.__setattr__ on attempts to overwrite a configuration setting.
"""
def nested_lookup(d, key):
"""Look up a value in a nested dictionary based on the given key.
For example, imagine we have a dictionary d:
......@@ -443,7 +449,20 @@ class Config:
def __getattr__(self, key):
"""Return the configuration item with the specified key. """
return self[key]
try:
return self[key]
except KeyError:
raise AttributeError(key)
def __setattr__(self, key, value):
"""Raise an error on attempts to modify a setting value. """
# Ignore the initial self.__settings assignment,
# otherwise we infinitely recurse.
if key != '_Config__settings' and key in self:
raise ConfigError(f'Attempt to overwrite config value '
f'{key} (value: {value})')
super().__setattr__(key, value)
def get(self, key, default=None):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment