示例#1
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"
示例#2
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
示例#3
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
示例#4
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"
示例#5
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
示例#6
0
def test_set_option():
    scope = {}
    settings_manager = SettingsManager(scope)
    settings_manager.set_option("TEST_SETTING", "hello")
    assert scope["TEST_SETTING"] == "hello"
示例#7
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"
示例#8
0
文件: example.py 项目: 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
示例#9
0
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")
try_include(env_file)

print_debug(f"Release env is '{RELEASE_ENV}'")

settings_manager.set_option(
    "PACKAGE_VERSION",
    read_file(os.path.join(BASE_DIR, "etc/VERSION")).strip())