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_tomlfy():
assert parse_conf_data("1", tomlfy=True) == 1
assert parse_conf_data("true", tomlfy=True) is True
assert parse_conf_data("'true'", tomlfy=True) == "true"
assert parse_conf_data('"42"', tomlfy=True) == "42"
assert parse_conf_data("[1, 32, 3]", tomlfy=True) == [1, 32, 3]
assert parse_conf_data("[1.1, 32.1, 3.3]", tomlfy=True) == [1.1, 32.1, 3.3]
assert parse_conf_data("['a', 'b', 'c']", tomlfy=True) == ["a", "b", "c"]
assert parse_conf_data("[true, false]", tomlfy=True) == [True, False]
assert parse_conf_data("{key='value', v=1}", tomlfy=True) == {
"key": "value",
"v": 1,
}
def test_tomlfy():
assert parse_conf_data("1", tomlfy=True) == 1
assert parse_conf_data("true", tomlfy=True) is True
assert parse_conf_data("'true'", tomlfy=True) == "true"
assert parse_conf_data('"42"', tomlfy=True) == "42"
assert parse_conf_data("[1, 32, 3]", tomlfy=True) == [1, 32, 3]
assert parse_conf_data("[1.1, 32.1, 3.3]", tomlfy=True) == [1.1, 32.1, 3.3]
assert parse_conf_data("['a', 'b', 'c']", tomlfy=True) == ["a", "b", "c"]
assert parse_conf_data("[true, false]", tomlfy=True) == [True, False]
assert parse_conf_data("{key='value', v=1}", tomlfy=True) == {
"key": "value",
"v": 1,
}
def test_cast_bool(settings):
"""Covers https://github.com/rochacbruno/dynaconf/issues/14"""
assert parse_conf_data(False) is False
assert settings.get("SIMPLE_BOOL", cast="@bool") is False
dotted_key {str} -- A traversal name e.g: foo.bar.zaz
value {Any} -- The value to set to the nested value.
Keyword Arguments:
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)
def split_vars(_vars):
"""Splits values like foo=bar=zaz in {'foo': 'bar=zaz'}"""
return (
{
upperfy(k.strip()): parse_conf_data(v.strip(), tomlfy=True)
for k, _, v in [item.partition("=") for item in _vars]
}
if _vars
else {}
)
client = get_client(obj)
env_list = build_env_list(obj, env)
for env in env_list:
path = "/".join([obj.VAULT_PATH_FOR_DYNACONF, env])
try:
data = client.secrets.kv.read_secret_version(path)
except InvalidPath:
# If the path doesn't exist, ignore it and set data to None
data = None
if data:
# There seems to be a data dict within a data dict,
# extract the inner data
data = data.get("data", {}).get("data", {})
try:
if data and key:
value = parse_conf_data(data.get(key), tomlfy=True)
if value:
obj.logger.debug(
"vault_loader: loading by key: %s:%s (%s:%s)",
key,
"****",
IDENTIFIER,
path,
)
obj.set(key, value)
elif data:
obj.logger.debug(
"vault_loader: loading: %s (%s:%s)",
list(data.keys()),
IDENTIFIER,
path,
)
try:
if key:
value = environ.get("{0}{1}".format(env_, key))
if value:
obj.logger.debug(
"env_loader: loading by key: %s:%s (%s:%s)",
key,
value,
identifier,
env,
)
obj.set(key, value, loader_identifier=identifier, tomlfy=True)
else:
trim_len = len(env_)
data = {
key[trim_len:]: parse_conf_data(data, tomlfy=True)
for key, data in environ.items()
if key.startswith(env_)
}
if data:
obj.logger.debug(
"env_loader: loading: %s (%s:%s)", data, identifier, env
)
obj.update(data, loader_identifier=identifier)
except Exception as e: # pragma: no cover
e.message = ("env_loader: Error ({0})").format(str(e))
if silent:
obj.logger.error(str(e))
else:
raise
:param tomlfy: Bool define if value is parsed by toml (defaults False)
:param is_secret: Bool define if secret values is hidden on logs.
:param merge: Bool define if existing nested data will be merged.
"""
nested_sep = self.get("NESTED_SEPARATOR_FOR_DYNACONF")
if nested_sep and nested_sep in key:
# turn FOO__bar__ZAZ in `FOO.bar.ZAZ`
key = key.replace(nested_sep, ".")
if "." in key and dotted_lookup is True:
return self._dotted_set(
key, value, loader_identifier=loader_identifier, tomlfy=tomlfy
)
value = parse_conf_data(value, tomlfy=tomlfy)
key = upperfy(key.strip())
existing = getattr(self, key, None)
if getattr(value, "dynaconf_del", None):
self.unset(key, force=True)
return
if getattr(value, "dynaconf_reset", False):
# just in case someone use a `@reset` in a first level var.
value = value.value
if getattr(value, "dynaconf_merge", False):
# just in case someone use a `@merge` in a first level var
if isinstance(value.value, (int, float, bool)):
# @merge 1, @merge 1.1, @merge False