libb.load_options

load_options(func=None, *, cls=<class 'libb.config.ConfigOptions'>)[source]

Wrapper that builds dataclass options from config file.

Standard interface:

  • options: str | dict | ConfigOptions | None

  • config: config module that defines options in Settings format

  • kwargs: additional kw-args to pass to function

Setup:

>>> from libb import Setting, create_mock_module
>>> Setting.unlock()
>>> test = Setting()
>>> test.foo.ftp.host = 'foo'
>>> test.foo.ftp.user = 'bar'
>>> test.foo.ftp.pazz = 'baz'
>>> Setting.lock()
>>> create_mock_module('test_config', {'test': test})
>>> import test_config
>>> @dataclass
... class Options(ConfigOptions):
...     host: str = None
...     user: str = None
...     pazz: str = None

On a Function:

>>> @load_options(cls=Options)
... def testfunc(options=None, config=None, **kwargs):
...     return options.host, options.user, options.pazz
>>> testfunc('test.foo.ftp', config=test_config)
('foo', 'bar', 'baz')

As Simple Kwargs:

>>> testfunc(host='foo', user='bar', pazz='baz')
('foo', 'bar', 'baz')

On a Class:

>>> class Test:
...     @load_options(cls=Options)
...     def __init__(self, options, config, **kwargs):
...         self.host = options.host
...         self.user = options.user
...         self.pazz = options.pazz
>>> t = Test('test.foo.ftp', test_config)
>>> t.host, t.user, t.pazz
('foo', 'bar', 'baz')