How to use the dynaconf.default_settings.ENCODING_FOR_DYNACONF function in dynaconf

To help you get started, weā€™ve selected a few dynaconf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rochacbruno / dynaconf / tests / test_py_loader.py View on Github external
def test_py_loader_from_file_dunder(clean_env, tmpdir):
    """Test load with dunder settings"""

    settings = LazySettings(
        DATABASES={
            "default": {
                "NAME": "db",
                "ENGINE": "module.foo.engine",
                "ARGS": {"timeout": 30},
                "PORTS": [123, 456],
            }
        }
    )
    dummy_path = tmpdir.join("dummy_module.py")
    with io.open(
        str(dummy_path), "w", encoding=default_settings.ENCODING_FOR_DYNACONF
    ) as f:
        f.write('F = "bar"')
        f.write("\n")
        f.write('COLORS__white__code = "#FFFFFF"')
        f.write("\n")
        f.write('DATABASES__default__ENGINE = "other.module"')

    load(settings, "dummy_module.py")
    os.remove("dummy_module.py")
    load(settings, "dummy_module.py")  # will be ignored not found

    assert settings.get("F") == "bar"
    assert settings.COLORS == {"white": {"code": "#FFFFFF"}}
    assert settings.DATABASES.default.NAME == "db"
    assert settings.DATABASES.default.ENGINE == "other.module"
github rochacbruno / dynaconf / tests / test_cli.py View on Github external
[
            "write",
            "env",
            "-v",
            "TESTVALUE=1",
            "-s",
            "SECRETVALUE=2",
            "-y",
            "-p",
            str(env_file),
        ]
    )

    assert "Data successful written to {}".format(env_file) in result
    assert "TESTVALUE" in read_file(
        str(env_file), encoding=default_settings.ENCODING_FOR_DYNACONF
    )
    assert "SECRETVALUE" in read_file(
        str(env_file), encoding=default_settings.ENCODING_FOR_DYNACONF
    )
github rochacbruno / dynaconf / tests / test_utils.py View on Github external
curr_dir = tmpdir
    dirs = []
    for f in ["child1", "child2", "child3", "child4"]:
        curr_dir = os.path.join(str(curr_dir), f)
        dirs.append(curr_dir)
        os.mkdir(curr_dir)

    child4 = dirs[-1]

    assert find_file("file-does-not-exist") == ""

    # now place a .env file a few levels up and make sure it's found
    filename = os.path.join(str(child4), ".env")
    with io.open(
        filename, "w", encoding=default_settings.ENCODING_FOR_DYNACONF
    ) as f:
        f.write("TEST=test\n")

    assert find_file(project_root=str(child4)) == filename

    # skip the inner child4/.env and force the find of /tmp.../.env
    assert find_file(
        project_root=str(child4), skip_files=[filename]
    ) == os.path.join(str(tmpdir), ".env")
github rochacbruno / dynaconf / dynaconf / loaders / toml_loader.py View on Github external
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(toml.load(open_file), settings_data)

    with io.open(
        str(settings_path),
        "w",
        encoding=default_settings.ENCODING_FOR_DYNACONF,
    ) as open_file:
        toml.dump(settings_data, open_file)
github rochacbruno / dynaconf / dynaconf / loaders / yaml_loader.py View on Github external
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(yaml.full_load(open_file), settings_data)

    with io.open(
        str(settings_path),
        "w",
        encoding=default_settings.ENCODING_FOR_DYNACONF,
    ) as open_file:
        yaml.dump(settings_data, open_file)
github rochacbruno / dynaconf / dynaconf / loaders / json_loader.py View on Github external
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)
github rochacbruno / dynaconf / dynaconf / loaders / yaml_loader.py View on Github external
: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(yaml.full_load(open_file), settings_data)

    with io.open(
        str(settings_path),
        "w",
        encoding=default_settings.ENCODING_FOR_DYNACONF,
    ) as open_file:
        yaml.dump(settings_data, open_file)
github rochacbruno / dynaconf / dynaconf / loaders / py_loader.py View on Github external
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()
            ]
github rochacbruno / dynaconf / dynaconf / cli.py View on Github external
from dynaconf import default_settings
from dynaconf import LazySettings
from dynaconf import loaders
from dynaconf.loaders.py_loader import get_module
from dynaconf.utils import upperfy
from dynaconf.utils.files import read_file
from dynaconf.utils.parse_conf import parse_conf_data
from dynaconf.validator import ValidationError
from dynaconf.validator import Validator


CWD = Path.cwd()
EXTS = ["ini", "toml", "yaml", "json", "py", "env"]
WRITERS = ["ini", "toml", "yaml", "json", "py", "redis", "vault", "env"]

ENC = default_settings.ENCODING_FOR_DYNACONF


def set_settings(instance=None):
    """Pick correct settings instance and set it to a global variable."""

    global settings

    settings = None

    if instance:
        settings = import_settings(instance)

    elif "INSTANCE_FOR_DYNACONF" in os.environ:
        settings = import_settings(os.environ["INSTANCE_FOR_DYNACONF"])

    elif "FLASK_APP" in os.environ:  # pragma: no cover
github rochacbruno / dynaconf / dynaconf / loaders / py_loader.py View on Github external
"or rename your program file.".format(filename)
        )

    _find_file = getattr(obj, "find_file", find_file)
    if not filename.endswith(".py"):
        filename = "{0}.py".format(filename)

    if filename in default_settings.SETTINGS_FILE_FOR_DYNACONF:
        silent = True
    mod = types.ModuleType(filename.rstrip(".py"))
    mod.__file__ = filename
    mod._is_error = False
    try:
        with io.open(
            _find_file(filename),
            encoding=default_settings.ENCODING_FOR_DYNACONF,
        ) as config_file:
            exec(compile(config_file.read(), filename, "exec"), mod.__dict__)
    except IOError as e:
        e.strerror = ("py_loader: error loading file (%s %s)\n") % (
            e.strerror,
            filename,
        )
        if silent and e.errno in (errno.ENOENT, errno.EISDIR):
            return
        raw_logger().debug(e.strerror)
        mod._is_error = True
    return mod