How to use the everett.component.RequiredConfigMixin 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_component.py View on Github external
def test_get_required_config():
    """Verify that get_required_config works for a trivial component"""

    class Component(RequiredConfigMixin):
        required_config = ConfigOptions()
        required_config.add_option("user", doc="no help")

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

    required = Component.get_required_config()
    assert [op.key for op in required] == ["user"]
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_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:
github willkg / everett / tests / test_sphinxext.py View on Github external
"""\
    .. autocomponent:: test_sphinxext.ComponentOptionDefault
    """
    )

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

           Options:
              **user** (*str*) -- Defaults to "\'ou812\'".
        """
    )


class ComponentOptionDoc(RequiredConfigMixin):
    required_config = ConfigOptions()
    required_config.add_option("user", doc="ou812")


def test_option_doc(tmpdir):
    rst = dedent(
        """\
    .. autocomponent:: test_sphinxext.ComponentOptionDoc
    """
    )

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

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

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

           A different docstring

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


class ComponentOptionDefault(RequiredConfigMixin):
    required_config = ConfigOptions()
    required_config.add_option("user", default="ou812")


def test_option_default(tmpdir):
    rst = dedent(
        """\
    .. autocomponent:: test_sphinxext.ComponentOptionDefault
    """
    )

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

           Options:
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):
github willkg / everett / tests / test_sphinxext.py View on Github external
:show-docstring:

    """
    )

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

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


class ComponentWithDocstring(RequiredConfigMixin):
    """This component is the best.

    The best!

    """

    required_config = ConfigOptions()
    required_config.add_option("user")

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


def test_show_docstring(tmpdir):
    rst = dedent(
        """\
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_sphinxext.py View on Github external
Defaults to "\'ou812\'".
        """
    )


class Foo(object):
    @classmethod
    def parse_foo_class(cls, value):
        pass

    def parse_foo_instance(self, value):
        pass


class ComponentOptionParser(RequiredConfigMixin):
    required_config = ConfigOptions()
    required_config.add_option("user_builtin", parser=int)
    required_config.add_option("user_parse_class", parser=parse_class)
    required_config.add_option("user_listof", parser=ListOf(str))
    required_config.add_option("user_class_method", parser=Foo.parse_foo_class)
    required_config.add_option("user_instance_method", parser=Foo().parse_foo_instance)


def test_option_parser(tmpdir):
    rst = dedent(
        """\
    .. autocomponent:: test_sphinxext.ComponentOptionParser
    """
    )

    assert parse(tmpdir, rst) == dedent(
github willkg / everett / docs / code / recipes_appconfig.py View on Github external
'CRITICAL': 50,
        '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