ANN: New configuration module released
Sylvain Thenault
sylvain.thenault at nospam.logilab.fr
Thu Nov 4 08:21:55 EST 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']
True
>>> print config['value']
None
>>> print config['multiple']
('yop',)
>>> print config['number']
2
>>>
>>> config.help()
usage: [options]
options:
-h, --help show this help message and exit
My config:
--dothis=<y or n>
--value=<string>
--multiple=<comma separated values>
you can also document the option
--number=<int>
>>>
>>> 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']
0
>>> print config['value']
None
>>> print config['multiple']
['1', '2', '3']
>>> print config['number']
3
>>>
>>> sys.argv = ['mon prog', '--value', 'bacon', '--multiple', '4,5,6', 'nonoptionargument']
>>> print config.load_command_line_configuration()
['nonoptionargument']
>>> print config['value']
bacon
>>>
>>> config.generate_config()
[MY CONFIG]
dothis=no
value='bacon'
# you can also document the option
multiple=4,5,6
number=3
---- 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).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
More information about the Python-list
mailing list