Config API¶
-
class
lymph.config.
ConfigView
(config, prefix)¶ A ConfigView allows access to a subtree of a
Configuration
object. It implements the mapping protocol. Dotted path keys are translated into nested dictionary lookups, i.e.cv.get('a.b')
is (roughly) equivalent tocv.get('a').get('b')
.If a value returned by
ConfigView
methods is a dict, it will be wrapped in aConfigView
itself. This – and getting dicts from aConfiguration
object – are the preferred way to create new ConfigViews.-
root
¶ A reference to the root
Configuration
instance.
-
-
class
lymph.config.
Configuration
(values=None)¶ Parameters: values – an optional initial mapping Configuration implements the same interface as
ConfigView
in addition to the methods described here.-
load
(file, sections=None)¶ Reads yaml configuration from a file-like object. If sections is not None, only the keys given are imported
-
load_file
(path, sections=None)¶ Reads yaml configuration from the file at
path
.
-
get_raw
(key, default)¶ Like
get()
, but doesn’t wrap dict values inConfigView
.
-
create_instance
(key, default_class=None, **kwargs)¶ Parameters: - key – dotted config path (e.g.
"container.rpc"
) - default_class – class object or fully qualified name of a class
- kwargs – extra keyword arguments to be passed to the factory
Creates an object from the config dict at
key
. The instance is created by a factory that is specified by its fully qualified name in aclass
key of the config dict.If the factory has a
from_config()
method it is called with aConfigView
ofkey
. Otherwise, the factory is called directly with the config values as keyword arguments.Extra keyword arguments to
create_instance()
are passed through tofrom_config()
or mixed into the arguments if the factory is a plain callable.If the config doesn’t have a
class
key the instance is create bydefault_class
, which can be either a fully qualifed name or a factory object.Given the following config file
foo: class: pack.age:SomeClass extra_arg: 42
you can create an instance of SomeClass
# in pack/age.py class SomeClass(object): @classmethod def from_config(cls, config, **kwargs): assert config['extra_arg'] == 42 assert kwargs['bar'] is True return cls(...) # in any module config = Configuration() config.load(...) config.create_instance('foo', bar=True)
- key – dotted config path (e.g.
-
get_instance
(key, default_class, **kwargs)¶ Like
create_instance()
, but only creates a single instance for each key.
-