mozilla/configman — pull request #33 by twobraids (2012-03-28 01:30:10+00:00)
Title: added a search algorithm to the DotDict class

One of the interesting problems that I've encountered in working with configman is that classes instantiated from options need to have configuration information about themselves as well as global configuration. For example, let's say we have this hierarchy

config
config.logger
config.source
config.source.class1
config.source.class1.kls
config.source.class1.param1
config.source.class1.param2

where 'config', 'source' and 'class1' are all namespaces. 'logger' is an object to do logging. 'kls' is a python class. 'param1' and 'param2' are requirements of the class in 'kls'.

if we try to instantiate 'kls', we're tempted to do so like this:

config.source.class1.kls(config)

but that would be wrong. 'kls' will end up looking for its requirements 'param1' and 'param2' directly in 'config' rather than way down the hierarchy in 'config.source.class1'. 'kls' doesn't know where it is in the hierarchy. The correct way to instantiate 'kls' is like this:

config.source.class1.kls(config.source.class1)

that aims 'kls' at the right level of the hierarchy to find its parameters. Unfortunately, that cuts off 'kls' from the opportunity to use parameters (like 'logger') found all the way down in 'config'.

The solution proposed here is to make the config DotDict object more intelligent. Rather than just return a KeyError when a key is missing from a nested DotDict, a search happens automatically up through each parent until the missing key is found. If the key is not found, even in the root DotDict, only then is the KeyError raised.