示例#1
0
def settings():
    """Settings fixture with some defaults"""
    mode = "TRAVIS" if os.environ.get("TRAVIS") else "TEST"
    loaders = ["dynaconf.loaders.env_loader"]
    os.environ[f"DYNA{mode}_HOSTNAME"] = "host.com"
    os.environ[f"DYNA{mode}_PORT"] = "@int 5000"
    os.environ[f"DYNA{mode}_VALUE"] = "@float 42.1"
    os.environ[f"DYNA{mode}_ALIST"] = '@json ["item1", "item2", "item3"]'
    os.environ[f"DYNA{mode}_ADICT"] = '@json {"key": "value"}'
    os.environ[f"DYNA{mode}_DEBUG"] = "@bool true"
    os.environ[f"DYNA{mode}_TODELETE"] = "@bool true"
    sets = LazySettings(
        LOADERS_FOR_DYNACONF=loaders,
        ENVVAR_PREFIX_FOR_DYNACONF=f"DYNA{mode}",
        ROOT_PATH_FOR_DYNACONF=os.path.dirname(os.path.abspath(__file__)),
        environments=True,
        boxed_data={
            "HOST": "server.com",
            "port": 8080,
            "PARAMS": {
                "username": "******",
                "PASSWORD": "******",
                "token": {
                    "TYPE": 1,
                    "value": 2
                },
            },
        },
    )
    sets.SIMPLE_BOOL = False
    sets.configure()
    return sets
示例#2
0
def settings():
    """Settings fixture with some defaults"""
    mode = 'TRAVIS' if os.environ.get('TRAVIS') else 'TEST'
    loaders = ['dynaconf.loaders.env_loader']
    os.environ['DYNA%s_HOSTNAME' % mode] = 'host.com'
    os.environ['DYNA%s_PORT' % mode] = '@int 5000'
    os.environ['DYNA%s_VALUE' % mode] = '@float 42.1'
    os.environ['DYNA%s_ALIST' % mode] = '@json ["item1", "item2", "item3"]'
    os.environ['DYNA%s_ADICT' % mode] = '@json {"key": "value"}'
    os.environ['DYNA%s_DEBUG' % mode] = '@bool true'
    os.environ['DYNA%s_TODELETE' % mode] = '@bool true'
    os.environ['PROJECT1_HOSTNAME'] = 'otherhost.com'
    sets = LazySettings(LOADERS_FOR_DYNACONF=loaders,
                        NAMESPACE_FOR_DYNACONF="DYNA%s" % mode,
                        boxed_data={
                            'HOST': 'server.com',
                            'port': 8080,
                            'PARAMS': {
                                'username': '******',
                                'PASSWORD': '******',
                                'token': {
                                    'TYPE': 1,
                                    'value': 2
                                }
                            }
                        })
    sets.SIMPLE_BOOL = False
    sets.configure()
    return sets
示例#3
0
def test_include_via_python_module_name_and_others(tmpdir):
    """Check if an include can be a Python module name plus others"""
    settings_file = tmpdir.join("settings.toml")
    settings_file.write("""
       [default]
       default_var = 'default'
    """)

    dummy_folder = tmpdir.mkdir("dummy")
    dummy_folder.join("dummy_module.py").write('FOO = "164110"')
    dummy_folder.join("__init__.py").write('print("initing dummy...")')

    yaml_file = tmpdir.join("otherfile.yaml")
    yaml_file.write("""
       default:
         yaml_value: 748632
    """)

    settings = LazySettings(
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
        INCLUDES_FOR_DYNACONF=["dummy.dummy_module", "otherfile.yaml"],
    )

    assert settings.DEFAULT_VAR == "default"
    assert settings.FOO == "164110"
    assert settings.YAML_VALUE == 748632
示例#4
0
def test_load_nested_different_types_with_merge(tmpdir):
    """Check merge works for includes."""

    settings_file = tmpdir.join("settings.toml")
    settings_file.write(MIXED_MERGE)

    toml_plugin_file = tmpdir.join("plugin1.toml")
    toml_plugin_file.write(TOML_PLUGIN)

    for ext in ["toml", "json", "yaml", "ini", "py"]:
        json_plugin_file = tmpdir.join(f"plugin2.{ext}")
        json_plugin_file.write(PLUGIN_TEXT[ext])

    settings = LazySettings(
        ENV_FOR_DYNACONF="custom",
        silent=False,
        LOADERS_FOR_DYNACONF=False,
        ROOT_PATH_FOR_DYNACONF=str(tmpdir),
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
        MERGE_ENABLED_FOR_DYNACONF=True,
    )

    assert settings.DEBUG is False
    assert settings.DATABASE_URI == f"{ext}.example.com"
    assert settings.PORT == 8080
    assert settings.SERVER == "toml.example.com"
    assert settings.PLUGIN_NAME == "testing"

    # merge asserts
    assert settings.NESTED_1.base == 1
    assert settings.NESTED_1.nested_2.base == 2
    assert settings.NESTED_1.nested_2.nested_3.base == 3
    assert settings.NESTED_1.nested_2.nested_3.nested_4.base == 4
    for ext in ["toml", "json", "yaml", "ini", "py"]:
        assert settings.NESTED_1.nested_2.nested_3.nested_4[ext] == 5
示例#5
0
def test_load_nested_different_types(ext, tmpdir):
    """Load a TOML file that includes other various settings file types."""

    settings_file = tmpdir.join("settings.toml")
    settings_file.write(MIXED.format(ext))

    toml_plugin_file = tmpdir.join("plugin1.toml")
    toml_plugin_file.write(TOML_PLUGIN)

    json_plugin_file = tmpdir.join(f"plugin2.{ext}")
    json_plugin_file.write(PLUGIN_TEXT[ext])

    settings = LazySettings(
        ENV_FOR_DYNACONF="DEFAULT",
        silent=False,
        LOADERS_FOR_DYNACONF=False,
        ROOT_PATH_FOR_DYNACONF=str(tmpdir),
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
    )

    assert settings.DEBUG is False
    assert settings.DATABASE_URI == f"{ext}.example.com"
    assert settings.PORT == 8080
    assert settings.SERVER == "toml.example.com"
    assert settings.PLUGIN_NAME == "testing"
示例#6
0
def test_load_nested_toml(tmpdir):
    """Load a TOML file that includes other TOML files."""

    settings_file = tmpdir.join("settings.toml")
    settings_file.write(TOML)

    toml_plugin_file = tmpdir.join("plugin1.toml")
    toml_plugin_file.write(TOML_PLUGIN)

    toml_plugin_file = tmpdir.join("plugin2.toml")
    toml_plugin_file.write(TOML_PLUGIN_2)

    settings = LazySettings(
        ENV_FOR_DYNACONF="DEFAULT",
        silent=False,
        LOADERS_FOR_DYNACONF=False,
        ROOT_PATH_FOR_DYNACONF=str(tmpdir),
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
    )

    # Ensure overrides that happen via TOML plugin config load.
    assert settings.SERVER == "plugin2.example.com"
    assert settings.DEBUG is False
    assert settings.PLUGIN_NAME == "testing"

    # From the second TOML plugin
    assert settings.PORT == 4040
    assert settings.PLUGIN_2_SPECIAL is True
示例#7
0
def settings():
    """Settings fixture with some defaults"""
    mode = 'TRAVIS' if os.environ.get('TRAVIS') else 'TEST'
    os.environ['DYNA%s_HOSTNAME' % mode] = 'host.com'
    os.environ['DYNA%s_PORT' % mode] = '@int 5000'
    os.environ['DYNA%s_VALUE' % mode] = '@float 42.1'
    os.environ['DYNA%s_ALIST' % mode] = '@json ["item1", "item2", "item3"]'
    os.environ['DYNA%s_ADICT' % mode] = '@json {"key": "value"}'
    os.environ['DYNA%s_DEBUG' % mode] = '@bool true'
    os.environ['DYNA%s_TODELETE' % mode] = '@bool true'
    os.environ['PROJECT1_HOSTNAME'] = 'otherhost.com'
    sets = LazySettings(LOADERS_FOR_DYNACONF=[
        'dynaconf.loaders.env_loader', 'dynaconf.loaders.redis_loader'
    ],
                        DYNACONF_NAMESPACE="DYNA%s" % mode)
    sets.SIMPLE_BOOL = False
    sets.configure()
    return sets
示例#8
0
def test_programmatically_file_load(tmpdir):
    """Check file can be included programmatically"""
    settings_file = tmpdir.join("settings.toml")
    settings_file.write("""
       [default]
       default_var = 'default'
    """)

    settings = LazySettings(SETTINGS_FILE_FOR_DYNACONF=str(settings_file))
    assert settings.DEFAULT_VAR == "default"

    toml_plugin_file = tmpdir.join("plugin1.toml")
    toml_plugin_file.write("""
        [development]
        plugin_value = 'plugin'
    """)

    settings.load_file(path=str(toml_plugin_file))
    assert settings.PLUGIN_VALUE == "plugin"

    settings.setenv("production")
    assert settings.DEFAULT_VAR == "default"
    # programmatically loaded file is not persisted
    # once `env` is changed, or a `reload` it will be missed
    # to persist it needs to go to `INCLUDES_FOR_DYNACONF` variable
    with pytest.raises(AttributeError):
        assert settings.PLUGIN_VALUE == "plugin"
示例#9
0
def test_invalid_include_path(tmpdir):
    """Ensure non existing paths are not loaded."""

    settings_file = tmpdir.join("settings.toml")
    settings_file.write(TOML)

    settings = LazySettings(
        ENV_FOR_DYNACONF="DEFAULT",
        silent=False,
        LOADERS_FOR_DYNACONF=False,
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
    )

    # Ensure overrides not happened
    assert settings.SERVER == "base.example.com"
    assert settings.DEBUG is False
示例#10
0
def test_include_via_python_module_name(tmpdir):
    """Check if an include can be a Python module name"""
    settings_file = tmpdir.join("settings.toml")
    settings_file.write("""
       [default]
       default_var = 'default'
    """)

    dummy_folder = tmpdir.mkdir("dummy")
    dummy_folder.join("dummy_module.py").write('FOO = "164110"')
    dummy_folder.join("__init__.py").write('print("initing dummy...")')

    settings = LazySettings(
        environments=True,
        SETTINGS_FILE_FOR_DYNACONF=str(settings_file),
        INCLUDES_FOR_DYNACONF=["dummy.dummy_module"],
    )

    assert settings.DEFAULT_VAR == "default"
    assert settings.FOO == "164110"
示例#11
0
    "PRODUCTION",
    "SECRETS",
    "STAGING",
    "STARTUP",
    "TESTING",
]

settings = LazySettings(
    load_dotenv=True,
    dotenv_override=True,
    dotenv_path='.env',
    redis_enabled=True,
    includes=[],
    environments=True,
    envvar_prefix="DYNACONF",
    lowercase_read=False,
    LOWERCASE_READ_FOR_DYNACONF=False,
    merge_enabled=True,
    settings_files=[
        'settings/settings.toml', '.secrets.toml', 'settings.py',
        '.secrets.py', 'settings.yaml', '.secrets.yaml'
    ],
    secrets=[],
)
"""
# Extra file, or list of files where to look for secrets
# useful for CI environment like jenkins
# where you can export this variable pointing to a local
# absolute path of the secrets file.
SECRETS_FOR_DYNACONF = get("SECRETS_FOR_DYNACONF", None)
示例#12
0
# coding: utf-8
from dynaconf.base import LazySettings
from dynaconf.validator import Validator, ValidationError
from dynaconf.contrib import FlaskDynaconf

settings = LazySettings()

__all__ = [
    'settings', 'LazySettings', 'Validator',
    'FlaskDynaconf', 'ValidationError'
]
示例#13
0
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), "vendor"))

from dynaconf.base import LazySettings  # noqa
from dynaconf.contrib import DjangoDynaconf  # noqav
from dynaconf.contrib import FlaskDynaconf  # noqa
from dynaconf.validator import ValidationError  # noqa
from dynaconf.validator import Validator  # noqa
"""This global settings is deprecated from 3.0.0+"""
settings = LazySettings(warn_dynaconf_global_settings=True)
"""This is the new recommended base class alias"""
Dynaconf = LazySettings  # noqa

__all__ = [
    "Dynaconf",
    "LazySettings",
    "Validator",
    "FlaskDynaconf",
    "ValidationError",
    "DjangoDynaconf",
]
from dynaconf.base import LazySettings  # noqa
from dynaconf.constants import DEFAULT_SETTINGS_FILES
from dynaconf.contrib import DjangoDynaconf  # noqa
from dynaconf.contrib import FlaskDynaconf  # noqa
from dynaconf.validator import ValidationError  # noqa
from dynaconf.validator import Validator  # noqa

settings = LazySettings(
    # This global `settings` is deprecated from v3.0.0+
    # kept here for backwards compatibility
    # To Be Removed in 4.0.x
    warn_dynaconf_global_settings=True,
    environments=True,
    lowercase_read=False,
    load_dotenv=True,
    default_settings_paths=DEFAULT_SETTINGS_FILES,
)

# This is the new recommended base class alias
Dynaconf = LazySettings  # noqa

__all__ = [
    "Dynaconf",
    "LazySettings",
    "Validator",
    "FlaskDynaconf",
    "ValidationError",
    "DjangoDynaconf",
]