ANN: New configuration module released

Sylvain Thenault sylvain.thenault at
Thu Nov 4 14:21:55 CET 2004

On Thu, 04 Nov 2004 11:00:32 +0000, Vinay Sajip wrote:

> Sylvain Thenault wrote:
>> the "configuration" module in logilab's common library is doing this (make
>> a bridge between optparse and ConfigParser). You give a kind of
>> configuration schema to a configuration object and then it can be
>> initialized from a configuration file and/or command line. Environment
>> variable are not considered yet, but I don't think it would be hard to add
>> this feature. The main problem is the lack of documentation, but if there
>> is some interest I can post an usage example.
> I'm interested to see how your bridging works.

well, the basic idea is a class providing a (simple) unified api to
register options and to read them from different sources, those
methods taking care of optparse / ConfigParser particularities 

A basic example showing main functionalities :

---- begin example
>>> import sys
>>> from logilab.common.configuration import Configuration
>>> options = [('dothis', {'type':'yn', 'default': True, 'metavar': '<y or n>'}),
...            ('value', {'type': 'string', 'metavar': '<string>'}),
...            ('multiple', {'type': 'csv', 'default': ('yop',),
...                          'metavar': '<comma separated values>',
...                          'help': 'you can also document the option'}),
...            ('number', {'type': 'int', 'default':2, 'metavar':'<int>'}),
...            ]
>>> config = Configuration(options=options, name='My config')
>>> print config['dothis']
>>> print config['value']
>>> print config['multiple']
>>> print config['number']
usage:  [options]

  -h, --help            show this help message and exit

  My config:
    --dothis=<y or n>
    --multiple=<comma separated values>
                        you can also document the option
>>> f = open('myconfig.ini', 'w')
>>> f.write('''[MY CONFIG]
... number = 3
... dothis = no
... multiple = 1,2,3
... ''')
>>> f.close()
>>> config.load_file_configuration('myconfig.ini')
>>> print config['dothis']
>>> print config['value']
>>> print config['multiple']
['1', '2', '3']
>>> print config['number']
>>> sys.argv = ['mon prog', '--value', 'bacon', '--multiple', '4,5,6', 'nonoptionargument']
>>> print config.load_command_line_configuration()
>>> print config['value']
>>> config.generate_config()


# you can also document the option


---- end example

> Using the config module I released, access to environment variables is
> very easy. Here's how:
> 1. Add the line
> os : `os`
> to your configuration (say, test.cfg).

I think the idea was to ask in the configuration file to specify a value
should be taken from an environment variable, but to get a value from an
environment variable and / or from the configuration file. 

> 2. Access it like this:
>>>> from config import Config
>>>> cfg = Config(file('test.cfg'))
>>>> cfg.os.environ['OS']
> 'Windows_NT'
> The point, of course, is that you can refer to environment variables
> from elsewhere in your configuration, e.g.
> temp_path: `os.environ['TEMP']`
> work_file_prefix: $temp_path + `os.sep`

I've one problem with this approach (other that the person writing the
configuration file has to know python to benefit from your module). Is
there something preventing things like :

work_file_prefix: `os.remove('/etc/password')`

Sylvain Thénault                               LOGILAB, Paris (France).

More information about the Python-list mailing list