Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parser_comes_from_options():
"""Verify the parser is picked up from options"""
config = ConfigManager([ConfigDictEnv({"FOO": "1"})])
class SomeComponent(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("foo", parser=int)
def __init__(self, config):
self.config = config.with_options(self)
comp = SomeComponent(config)
assert comp.config("foo") == 1
def test_with_options():
"""Verify .with_options() restricts configuration"""
config = ConfigManager(
[ConfigDictEnv({"FOO_BAR": "a", "FOO_BAZ": "b", "BAR": "c", "BAZ": "d"})]
)
class SomeComponent(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option("baz", default="", doc="some help here", parser=str)
def __init__(self, config):
self.config = config.with_options(self)
# Create the component with regular config
comp = SomeComponent(config)
assert comp.config("baz") == "d"
with pytest.raises(ConfigurationError):
# This is not a valid option for this component
comp.config("bar")
def test_with_namespace():
config = ConfigManager(
[ConfigDictEnv({"FOO_BAR": "foobaz", "BAR": "baz", "BAT": "bat"})]
)
# Verify the values first
assert config("bar", namespace=["foo"]) == "foobaz"
assert config("bar") == "baz"
assert config("bat") == "bat"
# Create the namespaced config
config_with_namespace = config.with_namespace("foo")
assert config_with_namespace("bar") == "foobaz"
# Verify 'bat' is not available because it's not in the namespace
with pytest.raises(ConfigurationError):
config_with_namespace("bat")
def test_ConfigDictEnv():
cde = ConfigDictEnv(
{"FOO": "bar", "A_FOO": "a_bar", "A_B_FOO": "a_b_bar", "lower_foo": "bar"}
)
assert cde.get("foo") == "bar"
assert cde.get("foo", namespace=["a"]) == "a_bar"
assert cde.get("foo", namespace=["a", "b"]) == "a_b_bar"
assert cde.get("FOO", namespace=["a"]) == "a_bar"
assert cde.get("foo", namespace=["A"]) == "a_bar"
assert cde.get("FOO", namespace=["A"]) == "a_bar"
cde = ConfigDictEnv({"foo": "bar"})
assert cde.get("foo") == "bar"
assert cde.get("FOO") == "bar"
def test_config_manager_doc():
config = ConfigManager(
[ConfigDictEnv({"foo": "bar"})], doc="See http://example.com/configuration"
)
# Test ConfigManager doc shows up
with pytest.raises(ConfigurationError) as exc_info:
config("foo", parser=int)
assert (
str(exc_info.value)
== "ValueError: invalid literal for int() with base 10: 'bar'\n"
"namespace=None key=foo requires a value parseable by int\n"
"See http://example.com/configuration"
)
# Test config doc and ConfigManager doc show up
with pytest.raises(ConfigurationError) as exc_info:
config("foo", parser=int, doc="Port to listen on.")
assert (
config_overrides = {}
if rv_home is None:
home = os.path.expanduser('~')
rv_home = os.path.join(home, '.rastervision')
self.rv_home = rv_home
config_file_locations = self._discover_config_file_locations(profile)
help_doc = ('Check https://docs.rastervision.io/ for docs.')
self.config = ConfigManager(
# Specify one or more configuration environments in
# the order they should be checked
[
# Allow overrides
ConfigDictEnv(config_overrides),
# Looks in OS environment first
ConfigOSEnv(),
# Look for an .env file
ConfigEnvFileEnv('.env'),
# Looks in INI files in order specified
ConfigIniEnv(config_file_locations),
],
# Make it easy for users to find your configuration docs
doc=help_doc)
This is shorthand for::
config = ConfigManager([ConfigDictEnv(dict_config)])
This is handy for writing tests for the app you're using Everett in.
:arg dict_config: Python dict holding the configuration for this
manager
:returns: ConfigManager with specified configuration
.. versionadded:: 0.3
"""
return cls([ConfigDictEnv(dict_config)])