Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_set_and_get(self):
set = Settings()
set.set("tmp_dir", "blub")
self.assertEqual(set.get("tmp_dir"), "blub")
with self.assertRaises(SettingsError):
set.set("non existent", "bla")
with self.assertRaises(SettingsError):
set.set("tmp_dir", 4)
with self.assertRaises(SettingsError):
set.set("nice", 100)
set.set("env/randomize_binary", True)
self.assertEqual(set.get("env/randomize_binary/enable"), True)
set.set("env/randomize_binary", False)
self.assertEqual(set.get("env/randomize_binary/enable"), False)
shutil.rmtree("blub", ignore_errors=True)
def test_get(self):
set = Settings()
self.assertEqual(set.get("tmp_dir"), "/tmp/temci")
set["env/nice"] = 10
self.assertEqual(set.get("env/nice"), 10)
with self.assertRaises(SettingsError):
set.get("non existent")
def modify_type_scheme(self, key: str, modificator: t.Callable[[Type], Type]):
"""
Modifies the type scheme of the given key via a modificator function.
:param key: given key
:param modificator: gets the type scheme and returns its modified version
:raises: SettingsError if the setting with the given key doesn't exist
"""
if self.is_obsolete(key):
return
if not self.validate_key_path(key.split("/")):
raise SettingsError("Setting {} doesn't exist".format(key))
tmp_typ = self.type_scheme
subkeys = key.split("/")
for subkey in subkeys[:-1]:
tmp_typ = tmp_typ[subkey]
tmp_typ[subkeys[-1]] = modificator(tmp_typ[subkeys[-1]])
assert isinstance(tmp_typ[subkeys[-1]], Type)
"""
Sets the setting key to the passed new value
:param key: settings key
:param value: new value
:param validate: validate after the setting operation
:raises: SettingsError if the setting isn't valid
"""
tmp = copy.deepcopy(self.prefs)
path = key.split("/")
self._set(path, value)
if validate:
res = self._validate_settings_dict(self.prefs, "settings with new setting ({}={!r})".format(key, value))
if not res:
self.prefs = tmp
raise SettingsError(str(res))
self._setup()
def get_type_scheme(self, key: str) -> Type:
"""
Returns the type scheme of the given key.
:param key: given key
:return: type scheme
:raises: SettingsError if the setting with the given key doesn't exist
"""
if not self.validate_key_path(key.split("/")):
raise SettingsError("Setting {} doesn't exist".format(key))
tmp_typ = self.type_scheme
for subkey in key.split("/"):
tmp_typ = tmp_typ[subkey]
return tmp_typ
"""
Load the configuration from the passed dictionary.
:param config_dict: passed configuration dictionary
"""
self.prefs = self.type_scheme.get_default()
tmp = copy.deepcopy(self.prefs)
def func(key, path, value):
self._set_default(path, value)
recursive_exec_for_leafs(config_dict, func)
res = self._validate_settings_dict(self.prefs, "settings with ones config dict")
if not res:
self.prefs = tmp
raise SettingsError(str(res))
self._setup()
try:
with open(file, 'r') as stream:
map = yaml.safe_load(stream.read().replace("!!python/tuple", ""))
def func(key, path, value):
self._set_default(path, value)
self._set(path, value)
recursive_exec_for_leafs(map, func)
except (yaml.YAMLError, IOError) as err:
self.prefs = tmp
raise SettingsError(str(err))
res = self._validate_settings_dict(self.prefs, "settings with ones from file '{}'".format(file))
if not res:
self.prefs = tmp
raise SettingsError(str(res))
self._setup()
def __init__(self):
"""
Initializes a Settings singleton object and thereby loads the Settings files.
It loads the settings files from the app folder (config.yaml) and
the current working directory (temci.yaml) if they exist.
:raises: SettingsError if some of the settings aren't in the format described via the type_scheme class property
"""
self.prefs = copy.deepcopy(self.type_scheme.get_default()) # type: t.Dict[str, t.Any]
""" The set sonfigurations """
res = self._validate_settings_dict(self.prefs, "default settings")
if not res:
raise SettingsError(str(res))
self._setup()