Professor is hosted by Hepforge, IPPP Durham

Basic Idea

My idea was that there exists one global object holding configuration values for all of professor's submodules. To make it possible to change this values during run time, it should read configuration files and command line arguments.

Example Usage

Module configuration is inited via the Config.initModule(modname, moddict) call. modname is the module's name, while moddict is a dictionary of (option name: value description) pairs. value description can have two forms:

  1. A length 4 tuple or list: The 4 fields are:
    1. the default value
    2. a conversion function if the type is not string
    3. a string to use as the command line option. It might also be True, then the command line option string is created from the option's name
    4. a help string
  2. A string if nothing of the above (type handling, command line) is needed.

Let's say we have a submodule "foo" and we have several settings of different types and usage:

  1. A string option, not controlled via command line.
  2. A string option which should be configurable via the command line.
  3. A boolean option, also configurable via command line.
  4. A float option, that should not be controlled on the command line (but it still can be changed via a config file).

To achieve this use the following example in the initialization of submodule foo.

# import the config object
from professor.tools.config import Config

# get a handle to the instance
conf = Config()

# initialize foo's configuration
conf.initModule('foo',
    {
    # 1.
    'hidden string opt': 'hidden default',
    # 2.
    'cmdline string opt': ('cmddefault', None, 'str-opt', 'this is help text),
    # 3.
    'cmdline bool opt': (False, conf.convBool, 'bool-opt', 'to switch it on use yes, on, true, 1'),
    # 4.
    'hidden float opt', (3.2, float, None, None)
    })

# get a handle to the logger for this module
logger = conf.getLogger('foo')

In the script we have to do the following to enable config file and command line parsing:

from professor.tools.config import Config
conf = Config()
# This parses the command line options.
# If a configuration file is given via the --config option it will be parsed
# automatically.
conf.parseCommandline()

Comments

  1. The type of a configuration value can be anything, as long as the conversion from string and back works, e.g. with integers:
    >>> int(str(1)) == 1
    True
    
  2. For boolean options you can use Config.convBool as conversion function. It converts every string that's lower version is one of "yes", "true", "on", "1" to True. The rest evaluates to False.
  3. You can use Config().write('path/to/file') to write the current content to the given file.

Logging

By default a module's logger sends its messages to its parent logger (normally the root logger in terms of the python logging module).

The logger's destination and level can be controlled via the logfiles and loglevel options:

logfiles comma separated list of destinations to log to
loglevel the level for logging

supported destinations are:

  • '-' : log to the parent logger (default)
  • 'stderr' : log to stderr
  • anything else will be treated as filename to log to

Configuration File Syntax

Right now the configuration file syntax is similar to Windows .ini files.

Example:

[global]
# send log messaged to parent logger
logfiles = -
loglevel = info

[interpolation]
logfiles = interpolation.log
loglevel = debug
method = quadratic

Multi line options are allowed, too. While parsing multi line options the following is performed:

  1. strip leading and trailing spaces, including the new line character
  2. strip trailing backslash
  3. append the remaining string to the option value.

Example:

opt1 = a first\
    example
opt2 = a second \
    example

opt1's value will be "a firstexample", opt2's "a second example"

Last modified 10 years ago Last modified on Mar 19, 2008, 10:32:33 AM