Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
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(
"""\
component test_sphinxext.ComponentOptionParser
def test_parse_missing_class():
with pytest.raises(ImportError):
parse_class("doesnotexist.class")
with pytest.raises(ValueError):
parse_class("hashlib.doesnotexist")
def test_parse_class():
from hashlib import md5
assert parse_class("hashlib.md5") == md5
def test_parse_missing_class():
with pytest.raises(ImportError):
parse_class("doesnotexist.class")
with pytest.raises(ValueError):
parse_class("hashlib.doesnotexist")
def test_parse_class_config():
config = ConfigManager.from_dict(
{"foo_cls": "hashlib.doesnotexist", "bar_cls": "doesnotexist.class"}
)
with pytest.raises(InvalidValueError) as exc_info:
config("foo_cls", parser=parse_class)
assert (
str(exc_info.value)
== 'ValueError: "doesnotexist" is not a valid member of hashlib\n'
"namespace=None key=foo_cls requires a value parseable by everett.manager.parse_class"
)
with pytest.raises(InvalidValueError) as exc_info:
config("bar_cls", parser=parse_class)
assert str(exc_info.value) in [
# Python 3
"ImportError: No module named 'doesnotexist'\n"
"namespace=None key=bar_cls requires a value parseable by everett.manager.parse_class",
# Python 3.6
"ModuleNotFoundError: No module named 'doesnotexist'\n"
"namespace=None key=bar_cls requires a value parseable by everett.manager.parse_class",
import os
from everett.component import RequiredConfigMixin, ConfigOptions
from everett.manager import ConfigManager, parse_class
class App(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option(
'basedir'
)
required_config.add_option(
'reader',
parser=parse_class
)
required_config.add_option(
'writer',
parser=parse_class
)
def __init__(self, config):
self.config = config.with_options(self)
self.basedir = self.config('basedir')
self.reader = self.config('reader')(config, self.basedir)
self.writer = self.config('writer')(config, self.basedir)
class FSReader(RequiredConfigMixin):
required_config = ConfigOptions()
from everett.component import RequiredConfigMixin, ConfigOptions
from everett.manager import ConfigManager, parse_class
class App(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option(
'basedir'
)
required_config.add_option(
'reader',
parser=parse_class
)
required_config.add_option(
'writer',
parser=parse_class
)
def __init__(self, config):
self.config = config.with_options(self)
self.basedir = self.config('basedir')
self.reader = self.config('reader')(config, self.basedir)
self.writer = self.config('writer')(config, self.basedir)
class FSReader(RequiredConfigMixin):
required_config = ConfigOptions()
required_config.add_option(
'file_type',
default='json'
)