How to use the everett.component.ConfigOptions function in everett

To help you get started, we’ve selected a few everett examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github willkg / everett / tests / test_sphinxext.py View on Github external
.. autocomponent:: test_sphinxext.ComponentOptionDoc
    """
    )

    assert parse(tmpdir, rst) == dedent(
        """\
        component test_sphinxext.ComponentOptionDoc

           Options:
              **user** (*str*) -- ou812
        """
    )


class ComponentOptionDocDefault(RequiredConfigMixin):
    required_config = ConfigOptions()
    required_config.add_option("user", doc="This is some docs.", default="ou812")


def test_option_doc_default(tmpdir):
    rst = dedent(
        """\
    .. autocomponent:: test_sphinxext.ComponentOptionDocDefault
    """
    )

    assert parse(tmpdir, rst) == dedent(
        """\
        component test_sphinxext.ComponentOptionDocDefault

           Options:
              **user** (*str*) --
github willkg / everett / tests / test_component.py View on Github external
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
github willkg / everett / tests / test_component.py View on Github external
def test_raw_value():
    config = ConfigManager.from_dict({"FOO_BAR": "1"})

    class SomeComponent(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("foo_bar", parser=int)

        def __init__(self, config):
            self.config = config.with_options(self)

    comp = SomeComponent(config)

    assert comp.config("foo_bar") == 1
    assert comp.config("foo_bar", raw_value=True) == "1"

    class SomeComponent(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("bar", parser=int)

        def __init__(self, config):
            self.config = config.with_options(self)
github willkg / everett / tests / test_component.py View on Github external
def test_tree(self):
        config = ConfigManager.from_dict({})

        class ComponentB(RequiredConfigMixin):
            required_config = ConfigOptions()
            required_config.add_option("foo", parser=int, default="2")
            required_config.add_option("bar", parser=int, default="1")

            def __init__(self, config):
                self.config = config.with_options(self)

        class ComponentA(RequiredConfigMixin):
            required_config = ConfigOptions()
            required_config.add_option("baz", default="abc")

            def __init__(self, config):
                self.config = config.with_options(self)
                self.comp = ComponentB(config.with_namespace("biff"))

            def get_runtime_config(self, namespace=None):
                for item in super(ComponentA, self).get_runtime_config(namespace):
                    yield item

                for item in self.comp.get_runtime_config(["biff"]):
                    yield item

        comp = ComponentA(config)

        assert list(comp.get_runtime_config()) == [
github willkg / everett / tests / test_component.py View on Github external
def test_nested_options():
    """Verify nested BoundOptions works."""
    config = ConfigManager.from_dict({})

    class Foo(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("option1", default="opt1default", parser=str)

    class Bar(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("option2", default="opt2default", parser=str)

    config = ConfigManager.basic_config()
    config = config.with_options(Foo)
    config = config.with_options(Bar)

    assert config("option2") == "opt2default"
    with pytest.raises(ConfigurationError):
        config("option1")
github willkg / everett / tests / test_component.py View on Github external
def test_alternate_keys():
    config = ConfigManager.from_dict(
        {"COMMON": "common_abc", "FOO": "abc", "FOO_BAR": "abc", "FOO_BAR_BAZ": "abc"}
    )

    class SomeComponent(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("bad_key", alternate_keys=["root:common"])

        def __init__(self, config):
            self.config = config.with_options(self)

    comp = SomeComponent(config)

    # The key is invalid, so it tries the alternate keys
    assert comp.config("bad_key") == "common_abc"
github willkg / everett / tests / test_component.py View on Github external
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")

    # Create the component with config in the "foo" namespace
    comp2 = SomeComponent(config.with_namespace("foo"))
    assert comp2.config("baz") == "b"
    with pytest.raises(ConfigurationError):
github willkg / everett / tests / test_component.py View on Github external
def test_not_bound_config(self):
        class ComponentA(RequiredConfigMixin):
            required_config = ConfigOptions()
            required_config.add_option("bar", parser=int)

            def __init__(self):
                self.config = "boo"

        comp = ComponentA()
        assert list(comp.get_runtime_config()) == []
github willkg / everett / tests / test_component.py View on Github external
def test_bound_config(self):
        config = ConfigManager.from_dict({"foo": 12345})

        class ComponentA(RequiredConfigMixin):
            required_config = ConfigOptions()
            required_config.add_option("foo", parser=int)
            required_config.add_option("bar", parser=int, default="1")

            def __init__(self, config):
                self.config = config.with_options(self)

        comp = ComponentA(config)
        assert list(comp.get_runtime_config()) == [
            ([], "foo", "12345", Option(key="foo", parser=int)),
            ([], "bar", "1", Option(key="bar", parser=int, default="1")),
        ]
github willkg / everett / docs / code / recipes_appconfig.py View on Github external
'ERROR': 40,
        'WARNING': 30,
        'INFO': 20,
        'DEBUG': 10
    }
    try:
        return text_to_level[value.upper()]
    except KeyError:
        raise ValueError(
            '"%s" is not a valid logging level. Try CRITICAL, ERROR, '
            'WARNING, INFO, DEBUG' % value
        )


class AppConfig(RequiredConfigMixin):
    required_config = ConfigOptions()
    required_config.add_option(
        'debug',
        parser=bool,
        default='false',
        doc='Turns on debug mode for the applciation'
    )
    required_config.add_option(
        'loglevel',
        parser=parse_loglevel,
        default='INFO',
        doc='Log level for the application'
    )

    def __init__(self, config):
        self.raw_config = config
        self.config = config.with_options(self)