Config Manager

The module provides convenient stuff to interact with dictionary instances that keep some configuration data.

Conf

class dooku.conf.Conf(*confs, **options)

A dict wrapper that extends its functionality.

The class was designed with one purpose: to provide the more convenient way to interact with dictionary instances that keep some configuration data. In many ways it behaves exactly like a dictionary, but unlike the last one it can be accessed by a compound key and supports recursive update.

A Compound Key is a key that consists of two or more keys that allow you to retrieve value from subnode of subnode. For example, we have the following structure

{ 'ukraine': {
    'kharkiv': {
      'timezone': 'UTC+2', }, }, }

and you want to get the timezone’s value with one query. Well, with compound keys you can do it this way

timezone = conf['ukraine.kharkiv.timezone']

instead of usual

timezone = conf['ukraine']['kharkiv']['timezone']

On the other hand we have a usual dictionary behaviour:

  • a KeyError exception will be thrown in case of key absence
  • a get() method is provided to retrieve values safely

The class overrides an update() method so it can be used to recursively update values. For example, we have the following structure

{ 'ukraine': {
    'kharkiv': {
      'timezone': 'UTC+2', }, }, }

and want to update it with next one

{ 'ukraine': {
    'kharkiv': {
      'population': '1 427 000', }, }, }

In vanilla Python’s dictionary we will have the same result as statement above since it completely overwrites top-level keys, but our class can handle it properly and merge it automatically, so we will have next result

{ 'ukraine': {
    'kharkiv': {
      'timezone': 'UTC+2',
      'population': '1 427 000', }, }, }

In addition to compound keys and update method, the class provides two helpers - from_json() and from_yaml() - for loading and updating a Conf instance from files.

conf = Conf(default_conf)
conf.from_json('path/to/user/conf.json')

Note

You have to install PyYAML before using from_yaml().

Parameters:
  • confs – A list of dictionaries to create a Conf instance based on it. Each next dictionary overrides settings from the previous one.
  • separator – A character that’s used as separator in compound keys