New configuration module released
Vinay Sajip
vinay_sajip at red-dove.com
Wed Nov 3 17:01:38 EST 2004
Dan,
> I'm posting some opinions I have on your module and on the topic in general
> in this newsgroup, instead of using the links you have on your web page
> (your email and a wiki) because I would like to see a wider discussion.
Fine.
> First, some small issues. The syntax for your configuration is still fairly
> close to the python syntax and it involves quite a bit of learning for both
> python users and non-python users. Your module would save me a little bit
> of work in parsing when loading a configuration (remember, I am loading
> configurations by importing them as a module) and a little more work than
> that in formatting when saving a configuration. That work is not very
> significant though and having my own mechanism also has its advantages.
One important benefit of having Python-like syntax is that it is easy to
create a configuration programmatically, using just dicts and lists.
This is something that you cite below as a big issue for you.
In terms of learning - I'm not sure that python developers would have
any real problem with the basic syntax. For end users, it's a case of
unfamiliarity rather than intrinsic difficulty, IMO.
> Now, here is the biggest issue I have and what I would be extremely
> interested in seeing in a configuration module. I don't see from your
> documentation how I could programmatically create a configuration from
> scratch. In my project I have a set of handlers and each handler has a
> special configuration. Each such special configuration is defined by a
> structure of metadata (with information like name, type, description) that I
> implemented myself. That way, my GUI tool can load a handler dynamically,
> read its configuration metadata, dynamically create a frame for the
> handler's configuration, and save the new configuration to a file.
There are many ways you could do what you want to do. Without knowing
the details of your application, I can't give particularly good advice.
However, consider the following program:
from config import Config
from cStringIO import StringIO
root = { }
messages = [ ]
root['total_period'] = 100
root['test'] = True
root['another_test'] = False
root['yet_another_test'] = None
root['messages'] = messages
root['message'] = 'Hello, world!'
messages.append({ 'message' : 'Welcome', 'name' : 'Harry' })
messages.append({ 'message' : 'Welkom', 'name' : 'Ruud' })
messages.append({ 'message' : 'Bienvenue', 'name' : 'Yves' })
stream = StringIO()
print >> stream, root
value = stream.getvalue()
stream.close()
stream = StringIO(value[1:-2])
cfg = Config(stream)
cfg.save(file('test.txt', 'w'))
This shows how easy it is to programmatically construct a simple
configuration from scratch.
It would be easy for your configuration to hold a separate tree in the
configuration with the metadata. For example, you could construct a GUI
frame given a set of fields:
fields: [ title, firstname, surname, age, jobtitle ]
metadata: {
title : { description: 'Salutation', type: choice,
values: [ [1, 'Mr.'], [2, 'Mrs.']] }
firstname : { description: 'First name', type: text,
max_length: 20 }
surname : { description: 'Last name', type: text, max_length: 20 }
age : { description: 'Age', type: int }
jobtitle: { description: 'Job title', type: text }
}
Then you can easily loop through the fields.
from config import Config
cfg = Config(file('metadata.cfg'))
for fld in cfg.fields:
metadata = cfg.get('metadata.' + fld)
print "name = %r, type = %r, description = %r" %
(fld, metadata.type, metadata.description)
with the result
name = 'title', type = 'choice', description = 'Salutation'
name = 'firstname', type = 'text', description = 'First name'
name = 'surname', type = 'text', description = 'Last name'
name = 'age', type = 'int', description = 'Age'
name = 'jobtitle', type = 'text', description = 'Job title'
I hope the above allows you to see the potential that the config module
has to meet your needs.
Best wishes,
Vinay Sajip
More information about the Python-list
mailing list