Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def env():
return environs.Env()
def test_env_isolation(set_env):
set_env({"FOO": "foo"})
env1 = environs.Env()
@env1.parser_for("foo")
def foo(value):
return value
env2 = environs.Env()
# env1 has a parser for foo, but env2 does not
assert env1.foo("FOO") == "foo"
with pytest.raises(AttributeError):
env2.foo("FOO")
def env(self):
return environs.Env(eager=False)
def enum_parser(value, choices):
if value not in choices:
raise environs.EnvError("Invalid!")
return value
def always_fail(value):
raise environs.EnvError("something went wrong")
def test_error_message_for_prefixed_var(self, env):
with env.prefixed("APP_"):
with pytest.raises(environs.EnvError, match='Environment variable "APP_INT" invalid'):
env.int("INT", validate=lambda val: val < 42)
def test_custom_parser_not_called_after_seal(self, env, set_env):
set_env({"URL": "test.test/"})
@env.parser_for("https_url")
def https_url(value):
return "https://" + value
env.seal()
with pytest.raises(environs.EnvSealedError, match="Env has already been sealed"):
env.https_url("URL")
def test_validation(self, env, set_env):
set_env({"INT": "invalid", "DTIME": "notadatetime"})
env.int("INT")
env.datetime("DTIME")
env.str("REQUIRED")
with pytest.raises(environs.EnvValidationError) as excinfo:
env.seal()
exc = excinfo.value
msg = exc.args[0]
assert "REQUIRED" in msg
assert "INT" in msg
assert "DTIME" in msg
assert "REQUIRED" in exc.error_messages
assert "INT" in exc.error_messages
assert "DTIME" in exc.error_messages
def test_cannot_override_built_in_parser(self, set_env, env):
def https_url(value):
return "https://" + value
with pytest.raises(environs.ParserConflictError):
env.add_parser("url", https_url)
# coding=utf-8
from environs import Env
env = Env()
# 拨号间隔,单位秒
DIAL_CYCLE = env.int('DIAL_CYCLE', 100)
# 拨号出错重试间隔
DIAL_ERROR_CYCLE = env.int('DIAL_ERROR_CYCLE', 5)
# 拨号命令
DIAL_BASH = env.str('DIAL_BASH', 'adsl-stop;adsl-start')
# 拨号网卡
DIAL_IFNAME = env.str('DIAL_IFNAME', 'ppp0')
# 客户端唯一标识
CLIENT_NAME = env.str('CLIENT_NAME', 'adsl1')
# Redis数据库IP
REDIS_HOST = env.str('REDIS_HOST', 'localhost')
# Redis数据库密码, 如无则填None