Exemplo n.º 1
0
def test_set_bool_global(globals_fixture):
    g = globals_fixture
    settings_manager = SettingsManager(g)

    assert "TEST_SETTING" not in g
    settings_manager.set_bool("TEST_SETTING", False)
    assert g["TEST_SETTING"] is False
Exemplo n.º 2
0
def test_set_option_w_env_var(envvar_fixture):
    """
    Environment variables take precedence over provided options
    """
    scope = {}
    settings_manager = SettingsManager(scope)
    settings_manager.set_option("TEST_SETTING", "hello")
    assert scope["TEST_SETTING"] == "world"
Exemplo n.º 3
0
def test_try_include():
    """
    We test using dev-default.py
    """
    scope = {}
    settings_manager = SettingsManager(scope)

    env_file = os.path.join(os.path.dirname(__file__), "dev-default.py")
    settings_manager.try_include(env_file)
    assert scope["TEST_SETTING"] == "hello"
Exemplo n.º 4
0
def test_try_include_non_default():
    """
    We test instantiating SettingsManager with name 'settings'
    and using dev-non-default.py
    """
    scope = {}
    settings = SettingsManager(scope, "settings")

    env_file = os.path.join(os.path.dirname(__file__), "dev-non-default.py")
    settings.try_include(env_file)
    assert scope["TEST_SETTING"] == "hello"
Exemplo n.º 5
0
def test_set_bool(envvar_fixture):
    """
    We coerce the environment variable to a boolean
    """
    scope = {}
    settings_manager = SettingsManager(scope)
    # 0 is interpreted as False
    os.environ["TEST_SETTING"] = "0"
    # env variables can never be set as integers
    settings_manager.set_bool("TEST_SETTING", False)
    assert scope["TEST_SETTING"] is False

    # the environment variable has precedence
    settings_manager.set_bool("TEST_SETTING", True)
    assert scope["TEST_SETTING"] is False

    # We raise an error if the env variable
    # cannot be reasonably coerced to a bool

    os.environ["TEST_SETTING"] = "100"
    with pytest.raises(ValueError):
        settings_manager.set_bool("TEST_SETTING", True)
Exemplo n.º 6
0
def test_set_option_booleans(envvar_fixture):

    scope = {}
    settings_manager = SettingsManager(scope)
    # env variables can only be set as strings
    os.environ["TEST_SETTING"] = "False"

    # setting the option with a boolean
    # will use set_bool to handle the
    # type coercion of the env variable
    settings_manager.set_option("TEST_SETTING", False)
    assert scope["TEST_SETTING"] is False

    # the environment variable has precedence
    settings_manager.set_option("TEST_SETTING", True)
    assert scope["TEST_SETTING"] is False

    del os.environ["TEST_SETTING"]
    del scope["TEST_SETTING"]
    settings_manager.set_option("TEST_SETTING", True)
    # We can set boolean values without env vars as well
    assert scope["TEST_SETTING"] is True
Exemplo n.º 7
0
def test_set_option_coerce_env_var(envvar_fixture):
    """
    We coerce the environment variable to the same type
    as the provided default.
    """
    scope = {}
    settings_manager = SettingsManager(scope)
    # env variables can never be set as integers
    os.environ["TEST_SETTING"] = "123"

    # setting an option with a default integer will coerce the env
    # variable as well (fix for issue #888)

    settings_manager.set_option("TEST_SETTING", 321)
    assert scope["TEST_SETTING"] == 123

    # setting an option with a default string will coerce the env
    # variable as well
    settings_manager.set_option("TEST_SETTING", "321")
    assert scope["TEST_SETTING"] == "123"

    settings_manager.set_option("TEST_SETTING", 123.1)
    assert scope["TEST_SETTING"] == 123.0
Exemplo n.º 8
0
def test_set_options_none(envvar_fixture):
    """
    We coerce the environment variable to a boolean
    """
    scope = {}
    settings_manager = SettingsManager(scope)
    # 0 is interpreted as False
    os.environ["TEST_SETTING"] = "0"

    # setting an option with None without setting the
    # envvar_type raises an error
    with pytest.raises(ValueError):
        settings_manager.set_option(
            "TEST_SETTING",
            None,
        )

    # setting an option with None but setting the
    # envvar_type is fine
    settings_manager.set_option("TEST_SETTING", None, envvar_type=int)
    assert scope["TEST_SETTING"] == 0

    settings_manager.set_option("TEST_SETTING", None, envvar_type=str)
    assert scope["TEST_SETTING"] == "0"
Exemplo n.º 9
0
def test_set_custom_type_TimeDuration():
    scope = {}
    settings_manager = SettingsManager(scope)
    settings_manager.set_option("TEST_SETTING", TimeDuration("2y 2d 2h 2m 2s 2ms"))
    assert scope["TEST_SETTING"] == 63295322.002
Exemplo n.º 10
0
def test_set_option():
    scope = {}
    settings_manager = SettingsManager(scope)
    settings_manager.set_option("TEST_SETTING", "hello")
    assert scope["TEST_SETTING"] == "hello"
Exemplo n.º 11
0
def test_set_from_env_no_env_w_default():
    scope = {}
    settings_manager = SettingsManager(scope)
    assert os.environ.get("TEST_SETTING") is None
    settings_manager.set_from_env("TEST_SETTING", default="default")
    assert scope["TEST_SETTING"] == "default"
Exemplo n.º 12
0
def test_set_from_env(envvar_fixture):
    scope = {}
    settings_manager = SettingsManager(scope)
    settings_manager.set_from_env("TEST_SETTING")
    assert scope["TEST_SETTING"] == "world"
Exemplo n.º 13
0
def test_settings_manager_manager():
    scope = {}
    settings_manager = SettingsManager(scope)
    assert settings_manager.scope == scope
Exemplo n.º 14
0
def test_set_option_global(globals_fixture):
    g = globals_fixture
    settings_manager = SettingsManager(g)
    settings_manager.set_option("TEST_SETTING", "world")
    assert g["TEST_SETTING"] == "world"
Exemplo n.º 15
0
Arquivo: example.py Projeto: 20c/confu
import os

from confu.util import SettingsManager

# using local scope g
g = {}
settings_manager = SettingsManager(g)
settings_manager.set_option("TEST_SETTING", "world")
print(g["TEST_SETTING"])

# using Global scope
settings_manager = SettingsManager(globals())
settings_manager.set_option("TEST_SETTING", "world")
print(TEST_SETTING)

# setting from env
os.environ["ENV_SETTING"] = "my_setting"  # setting env variable
settings_manager.set_from_env("ENV_SETTING")
print(ENV_SETTING)

# setting boolean
settings_manager.set_bool("BOOL_SETTING", False)
print(BOOL_SETTING)

# setting boolean overriden from env var from env
os.environ["ENV_BOOL"] = "True"  # setting env variable
settings_manager.set_bool("ENV_BOOL", False)
print(ENV_BOOL)

# setting int from env
os.environ["ENV_INT"] = "123"  # setting env variable
Exemplo n.º 16
0
        print_debug(f"loaded additional settings file '{filename}'")

    except FileNotFoundError:
        print_debug(
            f"additional settings file '{filename}' was not found, skipping")
        pass


def read_file(name):
    with open(name) as fh:
        return fh.read()


# Intialize settings manager with global variable

settings_manager = SettingsManager(globals())

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.abspath(
    os.path.join(os.path.dirname(__file__), "..", "..", ".."))

# set RELEASE_ENV, usually one of dev, beta, tutor, prod
settings_manager.set_option("RELEASE_ENV", "dev")

if RELEASE_ENV in ("dev", "run_tests"):
    settings_manager.set_bool("DEBUG", True)
else:
    settings_manager.set_bool("DEBUG", False)

# look for mainsite/settings/${RELEASE_ENV}.py and load if it exists
env_file = os.path.join(os.path.dirname(__file__), f"{RELEASE_ENV}.py")