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_merge_existing_list():
existing = ["bruno", "karla"]
object_merge(existing, existing)
# calling twice the same object does not duplicate
assert existing == ["bruno", "karla"]
new = ["erik", "bruno"]
object_merge(existing, new)
assert new == ["bruno", "karla", "erik", "bruno"]
def test_merge_existing_list_unique():
existing = ["bruno", "karla"]
new = ["erik", "bruno"]
object_merge(existing, new, unique=True)
assert new == ["karla", "erik", "bruno"]
elif "," in value.value:
# @merge foo,bar
value.value = value.value.split(",")
else:
# @merge foo
value.value = [value.value]
if existing:
object_merge(existing, value.value)
value = value.value
if existing is not None and existing != value:
# `dynaconf_merge` used in file root `merge=True`
if merge:
object_merge(existing, value)
else:
# `dynaconf_merge` may be used within the key structure
value = self._merge_before_set(key, existing, value, is_secret)
if isinstance(value, dict):
value = DynaBox(value, box_it_up=True)
setattr(self, key, value)
self.store[key] = value
self._deleted.discard(key)
# set loader identifiers so cleaners know which keys to clean
if loader_identifier and loader_identifier in self.loaded_by_loaders:
self.loaded_by_loaders[loader_identifier][key] = value
elif loader_identifier:
self.loaded_by_loaders[loader_identifier] = {key: value}
tomlfy {bool} -- Perform toml parsing (default: {False})
"""
split_keys = dotted_key.split(".")
existing_data = self.get(split_keys[0], {})
new_data = DynaBox(default_box=True)
tree = new_data
for k in split_keys[:-1]:
tree = tree.setdefault(k, {})
value = parse_conf_data(value, tomlfy=tomlfy)
tree[split_keys[-1]] = value
if existing_data:
object_merge({split_keys[0]: existing_data}, new_data)
self.update(data=new_data, tomlfy=tomlfy, **kwargs)
# @merge 1, @merge 1.1, @merge False
value.value = [value.value]
elif isinstance(value.value, str):
if "=" in value.value:
# @merge foo=bar
k, v = value.value.split("=")
value.value = {k: v}
elif "," in value.value:
# @merge foo,bar
value.value = value.value.split(",")
else:
# @merge foo
value.value = [value.value]
if existing:
object_merge(existing, value.value)
value = value.value
if existing is not None and existing != value:
# `dynaconf_merge` used in file root `merge=True`
if merge:
object_merge(existing, value)
else:
# `dynaconf_merge` may be used within the key structure
value = self._merge_before_set(key, existing, value, is_secret)
if isinstance(value, dict):
value = DynaBox(value, box_it_up=True)
setattr(self, key, value)
self.store[key] = value
self.logger.debug("%s merged to %s", key, _value)
global_merge = getattr(self, "MERGE_ENABLED_FOR_DYNACONF", False)
if isinstance(value, dict):
local_merge = value.pop(
"dynaconf_merge", value.pop("dynaconf_merge_unique", None)
)
if local_merge not in (True, False, None) and not value:
# In case `dynaconf_merge:` holds value not boolean - ref #241
value = local_merge
if global_merge or local_merge:
safe_value = {k: "***" for k in value} if is_secret else value
_log_before_merging(safe_value)
object_merge(existing, value)
safe_value = (
{
k: ("***" if k in safe_value else v)
for k, v in value.items()
}
if is_secret
else value
)
_log_after_merge(safe_value)
if isinstance(value, (list, tuple)):
local_merge = (
"dynaconf_merge" in value or "dynaconf_merge_unique" in value
)
if global_merge or local_merge:
value = list(value)
def write(settings_path, settings_data, merge=True):
"""Write data to a settings file.
:param settings_path: the filepath
:param settings_data: a dictionary with data
:param merge: boolean if existing file should be merged with new data
"""
settings_path = Path(settings_path)
if settings_path.exists() and merge: # pragma: no cover
existing = DynaconfDict()
load(existing, str(settings_path))
object_merge(existing, settings_data)
with io.open(
str(settings_path),
"w",
encoding=default_settings.ENCODING_FOR_DYNACONF,
) as f:
f.writelines(
[
"{} = {}\n".format(upperfy(k), repr(v))
for k, v in settings_data.items()
]
if global_merge or local_merge:
value = list(value)
unique = False
if local_merge:
try:
value.remove("dynaconf_merge")
except ValueError: # EAFP
value.remove("dynaconf_merge_unique")
unique = True
original = set(value)
_log_before_merging(
["***" for item in value] if is_secret else value
)
object_merge(existing, value, unique=unique)
safe_value = (
["***" if item in original else item for item in value]
if is_secret
else value
)
_log_after_merge(safe_value)
return value
def write(settings_path, settings_data, merge=True):
"""Write data to a settings file.
:param settings_path: the filepath
:param settings_data: a dictionary with data
:param merge: boolean if existing file should be merged with new data
"""
settings_path = Path(settings_path)
if settings_path.exists() and merge: # pragma: no cover
with io.open(
str(settings_path), encoding=default_settings.ENCODING_FOR_DYNACONF
) as open_file:
object_merge(json.load(open_file), settings_data)
with io.open(
str(settings_path),
"w",
encoding=default_settings.ENCODING_FOR_DYNACONF,
) as open_file:
json.dump(settings_data, open_file)