Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert "GLOBAL_ENV_FOR_DYNACONF: 'DYNACONF'" in result
@pytest.mark.parametrize("loader", WRITERS)
def test_list_with_loader(loader):
result = run(["list", "-l", loader])
assert "Working in development environment" in result
@pytest.mark.parametrize("env", ["default", "development"])
def test_list_with_env(env):
result = run(["list", "-e", env])
assert "Working in {} environment".format(env) in result
settings = LazySettings(OPTION_FOR_TESTS=True)
def test_list_with_instance():
result = run(["-i", "tests.test_cli.settings", "list"])
assert "OPTION_FOR_TESTS: True" in result
def test_list_with_instance_from_env():
result = run(
["list"], {"INSTANCE_FOR_DYNACONF": "tests.test_cli.settings"}
)
assert "OPTION_FOR_TESTS: True" in result
def test_instance_attribute_error():
result = run(["-i", "tests.test_cli.idontexist", "list"])
import pytest
from dynaconf import LazySettings
from dynaconf.loaders.ini_loader import load
settings = LazySettings(ENV_FOR_DYNACONF="PRODUCTION")
INI = """
a = 'a,b'
[default]
password = '@int 99999'
host = "server.com"
port = '@int 8080'
alist = item1, item2, '@int 23'
[[service]]
url = "service.com"
port = '@int 80'
[[[auth]]]
password = "qwerty"
"default": {
"name": "Bruno",
"colors": ["red", "green"],
"data": {
"links": {"twitter": "rochacbruno", "site": "brunorocha.org"}
},
}
}
toml_loader.write(str(tmpdir.join("settings.toml")), data, merge=False)
data = {"default": {"colors": "@merge ['blue']"}}
toml_loader.write(
str(tmpdir.join("settings.local.toml")), data, merge=False
)
settings = LazySettings()
assert settings.NAME == "Bruno"
assert settings.COLORS == ["red", "green", "blue"]
assert settings.DATA.links == {
"twitter": "rochacbruno",
"site": "brunorocha.org",
}
def test_envvar_prefix_for_dynaconf(tmpdir, recwarn):
os.environ["AWESOMEAPP_FOO"] = "1"
os.environ["AWESOMEAPP_BAR"] = "false"
os.environ["AWESOMEAPP_LIST"] = '["item1", "item2"]'
os.environ["AWESOMEAPP_FLOAT"] = "42.2"
settings = LazySettings(ENVVAR_PREFIX_FOR_DYNACONF="AWESOMEAPP")
assert settings.FOO == 1
assert settings.BAR is False
assert settings.LIST == ["item1", "item2"]
assert settings.FLOAT == 42.2
settings2 = LazySettings(GLOBAL_ENV_FOR_DYNACONF="AWESOMEAPP")
assert len(recwarn) == 1
assert issubclass(recwarn[0].category, DeprecationWarning)
assert "GLOBAL_ENV_FOR_DYNACONF" in str(recwarn[0].message)
assert settings2.FOO == 1
assert settings2.BAR is False
assert settings2.LIST == ["item1", "item2"]
assert settings2.FLOAT == 42.2
}
}
toml_loader.write(str(tmpdir.join("settings.toml")), data, merge=False)
data = {
"dynaconf_merge": True,
"default": {
"colors": ["blue"],
"data": {"links": {"github": "rochacbruno.github.io"}},
},
}
toml_loader.write(
str(tmpdir.join("settings.local.toml")), data, merge=False
)
settings = LazySettings()
assert settings.NAME == "Bruno"
assert settings.COLORS == ["red", "green", "blue"]
assert settings.DATA.links == {
"twitter": "rochacbruno",
"site": "brunorocha.org",
"github": "rochacbruno.github.io",
}
from dynaconf import LazySettings
settings = LazySettings(
ENV_FOR_DYNACONF="example", ENVVAR_PREFIX_FOR_DYNACONF="PROGRAM"
)
print(settings.USERNAME)
print(settings.SERVER)
print(settings.PASSWORD)
assertions = {
"SERVER": "fromenv.com",
"USERNAME": "admin",
"PASSWORD": "My5up3r53c4et",
}
for key, value in assertions.items():
found = settings.get(key)
settings_file = os.path.abspath(django_settings_module.__file__)
_root_path = os.path.dirname(settings_file)
# 1) Create the lazy settings object reusing settings_module consts
options = {
k: v for k, v in django_settings_module.__dict__.items() if k.isupper()
}
options.update(kwargs)
options.setdefault(
"SKIP_FILES_FOR_DYNACONF", [settings_file, "dynaconf_merge"]
)
options.setdefault("ROOT_PATH_FOR_DYNACONF", _root_path)
options.setdefault("ENVVAR_PREFIX_FOR_DYNACONF", "DJANGO")
options.setdefault("ENV_SWITCHER_FOR_DYNACONF", "DJANGO_ENV")
lazy_settings = dynaconf.LazySettings(**options)
# 2) Set all settings back to django_settings_module for 'django check'
lazy_settings.populate_obj(django_settings_module)
# 3) Bind `settings` and `DYNACONF`
setattr(django_settings_module, "settings", lazy_settings)
setattr(django_settings_module, "DYNACONF", lazy_settings)
# 4) keep django original settings
dj = {}
for key in dir(django_settings):
if (
key.isupper()
and (key != "SETTINGS_MODULE")
and key not in lazy_settings.store
):
dj = {}
for key in dir(django_settings):
if key.isupper() and key != "SETTINGS_MODULE":
dj[key] = getattr(django_settings, key, None)
dj["ORIGINAL_SETTINGS_MODULE"] = django_settings.SETTINGS_MODULE
dj.setdefault("ENVVAR_PREFIX_FOR_DYNACONF", "DJANGO")
env_prefix = "{0}_ENV".format(dj["ENVVAR_PREFIX_FOR_DYNACONF"]) # DJANGO_ENV
dj.setdefault(
"ENV_FOR_DYNACONF", os.environ.get(env_prefix, "DEVELOPMENT").upper()
)
settings = LazySettings(**dj)
# 0 given a bare settings
settings = LazySettings()
# 1 Ensure all renamed vars exists in object
for old, new in RENAMED_VARS.items():
assert old in settings
assert new in settings
# 2 Ensure pairs has the same value
for old, new in RENAMED_VARS.items():
assert settings.get(old) == settings.get(new)
assert getattr(settings, new) == getattr(settings, old)
# 0 given a full old-style configured setting
settings = LazySettings(
DYNACONF_NAMESPACE="FOO",
DYNACONF_SETTINGS_MODULE="/tmp/foo.toml",
PROJECT_ROOT="/tmp/",
DYNACONF_SILENT_ERRORS=False,
DYNACONF_ALWAYS_FRESH_VARS=["baz", "zaz", "caz"],
BASE_NAMESPACE_FOR_DYNACONF="original",
GLOBAL_ENV_FOR_DYNACONF="RAZAMANAZ",
)
# 1 Ensure all renamed vars exists in object
for old, new in RENAMED_VARS.items():
assert old in settings, old
assert new in settings, new
# 2 Ensure pairs has the same value
for old, new in RENAMED_VARS.items():
# Backwards compatible with old django extension (pre 2.0.0)
import dynaconf.contrib.django_dynaconf # noqa
from django.conf import settings as django_settings
django_settings.configure()
settings = django_settings
if settings is not None:
click.echo(
click.style(
"Django app detected", fg="white", bg="bright_black"
)
)
if settings is None:
settings = LazySettings()