wiki:ConfigBestPractice

ConfigBestPractice

The ndg.utils.config module is the start of an ndg-wide configuration system. From the developer's perspective it would be used something like this:

from ndg.utils.config import config

# Get a specific option
MY_OPTION = config.get('ndg.my.package', 'MY_OPTION')

# Read all options from a section into a dictionary
options = dict(config.items('ndg.my.package'))

From the user's perspective you create .ini-style configuration files in one of two standard locations. There is also a mechanism for including other files from within the standard files. This excerpt from the source explains:

  1. SafeConfigParser is subclassed so that any call to the read() or readfp() methods examines the section 'ndg.utils.config' for 'include' definitions. Each whitespace-separated string defined as include is treated as a path to another configuration file which will be passed to read(). Paths are interpreted relative to the config file currently being parsed.
  2. /etc/ndg.conf is read if available
  3. $HOME/.ndg/ndg.conf is read if available

For example in /etc/ndg.conf

[ndg.utils.config]
include = /etc/ndg/myconfig.conf
  ndg_extra.conf

would parse /etc/ndg/myconfig.conf and /etc/ndg_extra.conf.