Working with configurations

Working with configurations

A common pattern making use of configuration files:

from argdeco import main, command, arg, opt, config_factory
form os.path import expanduser

main.configure(compiler_factory=config_factory(
    config_file=arg('--config-file', '-C', help="configuration file", default=expanduser('~/.config/myconfig.yaml'))
))

@command('ls', opt('--all'))
def mycmd(cfg):
    if cfg['ls.all']:
        pass

main()

If you want to have foo.bar expanded to {'foo': {'bar': ...}}, use following:

from argdeco import main, command, arg, opt, config_factory, ConfigDict
form os.path import expanduser

main.configure(compiler_factory=config_factory(ConfigDict,
    config_file=arg('--config-file', '-C', help="configuration file", default=expanduser('~/.config/myconfig.yaml'))
))

@command('ls', opt('--all'))
def mycmd(cfg):
    if cfg['ls']['all']:
        pass

main()
class argdeco.config.ConfigDict(E=None, **F)[source]

dictionary-like class

This class implements a dictionary, which creates deep objects from keys like “foo.bar”. Example:

>>> c = Config()
>>> c['foo.bar'] = 'x'
>>> c
{'foo': {'bar': 'x'}}
>>> c['foo.bar']
'x'
assimilate(value)[source]

If value is a dictionary, then make it beeing a dictionary of same class like this. Copy all attributes, which are not controlled by dict class

flatten(D)[source]

flatten a nested dictionary D to a flat dictionary

nested keys are separated by ‘.’

update(E=None, **F)[source]

flatten nested dictionaries to update pathwise

>>> Config({'foo': {'bar': 'glork'}}).update({'foo': {'blub': 'bla'}})
{'foo': {'bar': 'glork', 'blub': 'bla'}

In contrast to:

>>> {'foo': {'bar': 'glork'}}.update({'foo': {'blub': 'bla'}})
{'foo: {'blub': 'bla'}'}
argdeco.config.config_factory(ConfigClass=<type 'dict'>, prefix=None, config_file=None)[source]

return a class, which implements the compiler_factory API

Parameters:
  • ConfigClass

    defaults to dict. A simple factory (without parameter) for a dictionary-like object, which implements __setitem__() method.

    Additionally you can implement following methods:

    init_args:A method to be called to initialize the config object by passing Namespace object resulting from parseargs method.

    You could load data from a configuration file here.

    compile_args:A method, which can return the same like a compile function does. If there is no such method, a tuple with a ConfigClass instance as single element is returned.
  • prefix – Add this prefix to config_name. (e.g. if prefix=”foo” and you have config_name=”x.y” final config_path results in “foo.x.y”)
  • config_file (argdeco.arguments.arg) –

    An arg to provide a config file.

    If you provide this argument, you can implement one of the following methods in your ConfigClass to load data from the configfile:

    load:If you pass config_file argument, this method can be implemented to load configuration data from resulting stream.

    If config_file is ‘-’, stdin stream is passed.

    load_from_file:If you prefer to open the file yourself, you can do this, by implementing load_from_file instead which has the filename as its single argument.
    update:method like dict.update(). If neither of load or load_from_file is present, but update is, it is assumed, that config_file is of type YAML (or JSON) and configuration is updated by calling update with the parsed data as parameter.

    If you implement neither of these, it is assumed, that configuration file is of type YAML (or plain JSON, as YAML is a superset of it).

    Data is loaded from file and will update configuration object using dict-like dict.update() method.

Returns:

ConfigFactory class, which implements compiler_factory API.