multiconf.decorators module¶
-
multiconf.decorators.mc_config(env_factory, mc_json_filter=None, mc_json_fallback=None, mc_5_migration=False, load_now=False)[source]¶ Function decorator for ConfigItem hierarchy for all Envs defined in ‘env_factory’.
This decorator creates a wrapped config in a object which is then used for loading the config (for all envs) and retrieving the configuration for a specific env. The name of the object will be the name of the wrapped function.
The class will have two public methods, load and __call__, see _McConfig for details.
E.g.:
@mc_config(envf) def conf(root): with someitem() as it: it.setattr('aa', default=1, tst=2, prod=3) # Load the configuration for all envs conf.load() # Get the cfg instantiated for 'prod' prod_cfg = conf(prod)
NOTE, There can only be one current config env! It is possible to get the config multiple times for different envs, but storing references to items in the configuration, and accessing attributes at a later time, will return the value from the last env specified in ‘__call__’.
Parameters: - env_factory (EnvFactory) – The EnvFactory defining the envs for which we instantiate the configuration.
- mc_json_filter (func(obj, key, value)) – User defined function for filtering objects in json output. - filter_callable is called for each key/value pair of attributes on each ConfigItem obj. - It must return a tuple of (key, value). If key is False, the key/value pair is removed from the json output
- mc_json_fallback (func(obj)) – User defined function for handling objects not otherwise encoded in json output. - fallback_callable is called for objects that are not handled by the builtin encoder. - It must return a tuple (object, handled). If handled is True, the object must be encodable by the standard json encoder.
- mc_5_migration (bool) – This changes the attribute overwriting rule to me more compatible with version 5. Do not use this in any new configurations.
- load_now (bool) – Load the configuration now instead of calling load later. Note: this is only for simple cases, to get more control use load.
-
multiconf.decorators.named_as(insert_as_name)[source]¶ Determine the name used to insert item in parent
-
multiconf.decorators.nested_repeatables(*attr_names)[source]¶ Specify which nested (child) items will be repeatable.
-
multiconf.decorators.repeatable_key(**name_value)[source]¶ Set name and default value for the mc_key repeatable item __init__ argument.
Parameters: **name_value (dict[name, val]) – There must be exactly one name/value pair. E.g.:
Use ‘name’ argument as the mc_key.:
@named_as('xses') @repeatable_key(name=None) class X1(RepeatableConfigItem): def __init__(name, ...)
Only a single item of the following class can be created in the ‘xses’ repeatable. Use ‘name’ argument as the mc_key for the parent class, use value ‘xxx’ as the mc_key.:
@repeatable_key(name='xxx') class X2(X1): def __init__(...) # No 'name' argument super(X2, self)._init__(name=None, ...)
Only a single item of the following class can be created in the ‘ys’ repeatable. Use value ‘nicekey’ as the mc_key.:
@named_as('ys') @repeatable_key(mc_key='nicekey') class X2(X1): def __init__(...) # No 'mc_key' argument super(X2, self)._init__(...)