def test_custom_var_format(self): def formatter(x): return "_{}".format(str.lower(x)) config = EnvFile(self.envfile, var_format=formatter) self.assertIn("VAR", config) self.assertEqual("test", config["VAR"])
def test_custom_var_format(envfile): def formatter(x): return "_{}".format(str.lower(x)) config = EnvFile(envfile, var_format=formatter) assert "VAR" in config assert "test" == config["VAR"]
def test_customized_loaders(self): os.environ["ENVVAR"] = "Environment Variable Value" os.environ["ENVVAR2"] = "Foo" loaders = [EnvFile(self.envfile), Environment(), IniFile(self.inifile)] config = Configuration(loaders=loaders) self.assertEqual(config("ENVVAR"), "Must be overrided") self.assertEqual(config("ENVVAR2"), "Foo") self.assertEqual(config("ENVFILE"), "Environment File Value") self.assertEqual(config("INIFILE"), "INI File Value") self.assertEqual(len(config.loaders), 3) del os.environ["ENVVAR"] del os.environ["ENVVAR2"]
def test_customized_loaders(env_config, ini_config): os.environ["ENVVAR"] = "Environment Variable Value" os.environ["ENVVAR2"] = "Foo" loaders = [EnvFile(env_config), Environment(), IniFile(ini_config)] config = Configuration(loaders=loaders) assert config("ENVVAR") == "Must be overrided" assert config("ENVVAR2") == "Foo" assert config("ENVFILE") == "Environment File Value" assert config("INIFILE") == "INI File Value" assert len(config.loaders) == 3 del os.environ["ENVVAR"] del os.environ["ENVVAR2"]
def test_config_file_parsing(envfile): config = EnvFile(envfile) assert config["KEY"] == "Value" assert config["KEY_EMPTY"] == "" assert config["KEY_EMPTY_WITH_COMMENTS"] == "" assert config["INLINE_COMMENTS"] == "Foo" assert config["HASH_CONTENT"] == "Foo Bar # Baz %(key)s" assert config["PERCENT_NOT_ESCAPED"] == "%%" assert config["NO_INTERPOLATION"] == "%(KeyOff)s" assert config["IGNORE_SPACE"] == "text" assert config["SINGLE_QUOTE_SPACE"] == " text" assert config["DOUBLE_QUOTE_SPACE"] == " text" assert config["UPDATED"] == "text" assert config[ "CACHE_URL_QUOTES"] == "cache+memcached://foo:bar@localhost:11211/?n=1&x=2,5" assert config[ "CACHE_URL"] == "cache+memcached://foo:bar@localhost:11211/?n=1&x=2,5" assert config["DOUBLE_QUOTE_INSIDE_QUOTE"] == 'foo "bar" baz' assert config["SINGLE_QUOTE_INSIDE_QUOTE"] == "foo 'bar' baz" assert config["MULTIPLE_LINES"] == "multiple lines config"
def test_fail_missing_envfile_get_item(): config = EnvFile("does-not-exist.env") with pytest.raises(KeyError): return config["error"]
def test_fail_missing_envfile_contains(): config = EnvFile("does-not-exist.env") assert "error" not in config
def test_basic_config_object(envfile): config = EnvFile(envfile) assert repr(config) == 'EnvFile("{}")'.format(envfile)
def test_default_var_format(envfile): config = EnvFile(envfile) assert "key" in config assert "Value" == config["key"]
def test_missing_invalid_keys_in_config_file_parsing(envfile): config = EnvFile(envfile) assert "COMMENTED_KEY" not in config assert "INVALID_KEY" not in config assert "OTHER_INVALID_KEY" not in config
from os import getcwd from prettyconf import Configuration from prettyconf.loaders import EnvFile, Environment env_file = f"{getcwd()}/.env" config = Configuration(loaders=[Environment(), EnvFile(filename=env_file)]) class Config: """Config class for variables.""" TOKEN = config("TOKEN") APP_ID = int(config("APP_ID")) API_HASH = config("API_HASH") OWNER_ID = int(config("OWNER_ID")) PREFIX_HANDLER = config("PREFIX_HANDLER", default="/ !").split() NO_LOAD = config("NO_LOAD", default=[]) WORKERS = int(config("WORKERS", default=8)) class Development: """Development class for variables.""" # Fill in these vars if you want to use Traditional method of deploying TOKEN = "YOUR TOKEN" APP_ID = 12345 # Your APP_ID from Telegram API_HASH = "YOUR TOKEN" # Your APP_HASH from Telegram OWNER_ID = "YOUR TOKEN" PREFIX_HANDLER = ["!", "/"] NO_LOAD = []
def test_fail_missing_envfile_get_item(self): config = EnvFile("does-not-exist.env") with self.assertRaises(KeyError): return config["error"]
def test_fail_missing_envfile_contains(self): config = EnvFile("does-not-exist.env") self.assertNotIn("error", config)
def test_default_var_format(self): config = EnvFile(self.envfile) self.assertIn("key", config) self.assertEqual("Value", config["key"])
def test_missing_invalid_keys_in_config_file_parsing(self): config = EnvFile(self.envfile) self.assertNotIn("COMMENTED_KEY", config) self.assertNotIn("INVALID_KEY", config) self.assertNotIn("OTHER_INVALID_KEY", config)
def test_basic_config_object(self): config = EnvFile(self.envfile) self.assertEqual(repr(config), 'EnvFile("{}")'.format(self.envfile))