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
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)
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 settings_manager.set_option("ENV_INT", None, envvar_type=int) print(ENV_INT) # setting default settings_manager.set_default("DEFAULT_SETTING", "my_defualt") print(DEFAULT_SETTING)
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()) # Contact email, from address, support email settings_manager.set_from_env("SERVER_EMAIL")