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_env_loader():
assert settings.HOSTNAME == "host.com"
assert settings.PORT == 5000
assert settings.ALIST == ["item1", "item2", "item3", 123]
assert settings.ADICT == {"key": "value", "int": 42}
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Type definitions for environment variables\n")
print("Note that the PORT variable must be integer")
print("And envvars are by default string typed")
print("Dynaconf allows type definition on exporting an envvar")
print('$ export DYNACONF_PORT="@int 8888"')
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_PORT"] = "@int 8888"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
assert settings.SERVER == "customserver.com"
assert settings.PORT == 8888
assert settings.USERNAME == "YetAnotherUser"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("DONT WORRY: if you dont like the `@type` strategy you can disable it")
print("and perform the explicit cast when reading")
print('>>> connect(..., settings.as_int("PORT"), ...)')
print(
"types: @int|.as_int, @float|.as_float, @bool|.as_bool and @json|.as_json"
)
print("#" * 79)
print("\n* There is more!\n")
print(
"Dynaconf can switch environments programatically with context managers."
)
print("Dynaconf has extensions for Flask and Django.")
print("Dynaconf can read values from yaml, ini, json and py files.")
assert settings.PORT == 5555
assert settings.USERNAME == "YetAnotherUser"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Type definitions for environment variables\n")
print("Note that the PORT variable must be integer")
print("And envvars are by default string typed")
print("Dynaconf allows type definition on exporting an envvar")
print('$ export DYNACONF_PORT="@int 8888"')
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_PORT"] = "@int 8888"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
assert settings.SERVER == "customserver.com"
assert settings.PORT == 8888
assert settings.USERNAME == "YetAnotherUser"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("DONT WORRY: if you dont like the `@type` strategy you can disable it")
print("and perform the explicit cast when reading")
print('>>> connect(..., settings.as_int("PORT"), ...)')
print(
"types: @int|.as_int, @float|.as_float, @bool|.as_bool and @json|.as_json"
)
print("#" * 79)
print("\n* There is more!\n")
print(
"Dynaconf can switch environments programatically with context managers."
)
os.environ["DYNACONF_VALUE"] = "@float 42.1"
os.environ["DYNACONF_ALIST"] = '@json ["item1", "item2", "item3"]'
os.environ["DYNACONF_ADICT"] = '@json {"key": "value"}'
os.environ[
"DYNACONF_DEBUG"
] = "@bool true" # on, enabled, 1, active accepted as keyword here
# Now when reading settings they will be get from your sources dynamically
# defined data types are casted (but you can also do explicitly if want)
# read more in https://github.com/rochacbruno/dynaconf
print("You server is:")
print(settings.HOSTNAME)
print("\nThe port is:")
print(settings.PORT)
print(type(settings.PORT))
print("\nThe value is:")
print(settings.VALUE)
print(type(settings.VALUE))
print("\nThe debug is:")
print(settings.DEBUG)
print(type(settings.DEBUG))
print("\nThe list is:")
print(settings.ALIST)
print(len(settings.ALIST))
print(type(settings.ALIST))
print("\nThe dict is:")
settings.find_file("settings.toml"),
encoding=os.environ.get("ENCODING_FOR_DYNACONF"),
) as settings_file:
print(settings_file.read())
print("$ cat .secrets.toml")
with io.open(
settings.find_file(".secrets.toml"),
encoding=os.environ.get("ENCODING_FOR_DYNACONF"),
) as secrets_file:
print(secrets_file.read())
print("#" * 79)
print("\n* Acessing settings defined in .toml files\n")
print('By default dynaconf will always work in "development" env')
print("Take a look at settings.toml and .secrets.toml")
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
assert settings.SERVER == "devserver.com"
assert settings.PORT == 5555
assert settings.USERNAME == "admin"
assert settings.PASSWORD == "SuperSecretDev"
print("#" * 79)
print("\n* Working on a different environment\n")
print("To switch the env export the ENV_FOR_DYNACONF variable")
print("$ export ENV_FOR_DYNACONF=production")
print("Now reading settings from PRODUCTION env:")
# this next line is not needed in your program, it is the same as
# `export ENV_FOR_DYNACONF..`
import os
os.environ["ENV_FOR_DYNACONF"] = "production"
settings.reload() # noqa
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Environments and Custom Environments\n")
print("Dynaconf works with 4 predefined envs:")
print(" [development](the default), [production], [staging], [testing]")
print("There is also the pseudo envs")
print(" [default](default values) and [global](overrides every value)")
print("It is also possible to define custom envs")
print("$ export ENV_FOR_DYNACONF=mycustomenv")
# this next line is not needed in your program, it is the same as
# `export ENV_FOR_DYNACONF..`
os.environ["ENV_FOR_DYNACONF"] = "mycustomenv"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
assert settings.SERVER == "customserver.com"
assert settings.PORT == 5555
assert settings.USERNAME == "admin"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Default and Global settings\n")
print("The env [global] is to override values defined in any other env")
print("It means a value in [global] will always take high precedence")
print("To override any value in [global] just put the value in .toml file")
print("Or via envvars export a value with DYNACONF_ prefix")
print('$ export DYNACONF_USERNAME="NewUsername"')
print('You can also put the value in .env file"')
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_USERNAME"] = "NewUsername"
from dynaconf import settings
# print all values in the file
# using [default] + [development] + [global] values
print("* All values")
print(settings.HOST)
print(settings.PORT)
print(settings.USERNAME)
print(settings.PASSWORD)
print(settings.LEVELS)
print(settings.TEST_LOADERS)
print(settings.MONEY)
print(settings.AGE)
print(settings.ENABLED)
print(settings.CUSTOM)
print("* Switiching to production")
# using [production] env values for context
with settings.using_env("PRODUCTION"):
print(settings.CUSTOM)
print(settings.HOST)
print("* Switiching to development")
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("#" * 79)
print("\n* Type definitions for environment variables\n")
print("Note that the PORT variable must be integer")
print("And envvars are by default string typed")
print("Dynaconf allows type definition on exporting an envvar")
print('$ export DYNACONF_PORT="@int 8888"')
# this next line is not needed in your program, it is the same as
# `export DYNACONF_USERNAME..`
os.environ["DYNACONF_PORT"] = "@int 8888"
settings.reload() # noqa
connect(settings.SERVER, settings.PORT, settings.USERNAME, settings.PASSWORD)
assert settings.SERVER == "customserver.com"
assert settings.PORT == 8888
assert settings.USERNAME == "YetAnotherUser"
assert settings.PASSWORD == "SuperSecret" # will use the [default]
print("DONT WORRY: if you dont like the `@type` strategy you can disable it")
print("and perform the explicit cast when reading")
print('>>> connect(..., settings.as_int("PORT"), ...)')
print(
"types: @int|.as_int, @float|.as_float, @bool|.as_bool and @json|.as_json"
)
print("#" * 79)
print("\n* There is more!\n")
print(
"Dynaconf can switch environments programatically with context managers."
)
print("Dynaconf has extensions for Flask and Django.")
print("Dynaconf can read values from yaml, ini, json and py files.")
from dynaconf import settings
print(
"# 1 all values in {} + {}: env of yaml file:".format(
settings.DEFAULT_ENV_FOR_DYNACONF, settings.current_env
)
)
assert settings.current_env == "DEVELOPMENT"
print("HOST::", settings.HOST)
print("PORT:", settings.PORT)
print("USERNAME:", settings.USERNAME)
print("PASSWORD:", settings.PASSWORD)
print("LEVELS:", settings.LEVELS)
print("TEST_LOADERS:", settings.TEST_LOADERS)
print("MONEY:", settings.MONEY)
print("AGE:", settings.AGE)
print("ENABLED:", settings.ENABLED)
print("ENVIRONMENT:", settings.get("ENVIRONMENT"))
print("WORKS:", settings.WORKS)
with settings.using_env("TESTING"):
print("# 2 using {}: env values for context:".format(settings.current_env))
assert settings.current_env == "TESTING"
print("HOST::", settings.HOST)
print("PORT:", settings.PORT)
print("USERNAME:", settings.USERNAME)
from dynaconf import settings
print(settings.YAML)
print(settings.HOST)
print(settings.PORT)
# using production values for context
with settings.using_env("PRODUCTION"):
print(settings.ENVIRONMENT)
print(settings.HOST)
# back to development env
print(settings.get("ENVIRONMENT"))
print(settings.HOST)
print(settings.WORKS)
assertions = {
"HOST": "dev_server.com",
"PORT": 5000,