views:

83

answers:

4

I want to use Python to make a configuration file generator. My roughly idea is feeding input with template files and some XML files with the real settings. Then use the program to generate the real configuration files.

Example:

[template file]
server_IP = %serverip%
server_name = %servername%


[XML file]
<serverinfo>
<server ip="x.x.x.x" name="host1" />
<server ip="x.x.x.x" name="host2" />
</serverinfo>

and then get output configuration file like this

[server.ini]

[server1]
server_IP = x.x.x.x
server_name = host1

[server2]
server_IP = x.x.x.x
server_name = host2

I got several questions:

  • Is there any open source configuration generator program? (what could be the keyword), I wonder if there's anything can be added/modified in the design.

  • Does Python have good XML parser module?

  • Is it good idea to use XML file to save the original settings? I've been thinking to use Excel since it's more intuitive to maintain, but harder for program to parse. Not sure how people deal with this.

Hope the community can give me some suggestions. Thanks a lot!

EDIT: In scenario that there are dozens of these output ini files. I am concerning 2 things.

  1. there are dozens of ip/hostname and related informations, that may requires to be managed by human, so XML format would be a bit inconvenient. What could be the most convenient structure to manage those information? (Excel would be a handy tool to batch modify and look up info)

  2. In case of need to add some extra line into ini files, I need a efficient way by just modify the template file and add extra info into the source file (may be the Excel file or whatever), then whole bunches of ini files can be generated quickly.

+1  A: 

It's terrific to use xml for any configuration file. Best choice in any interpreted language is to use code file and simply exec or import it. Pickle is also good, but not human readable.

lionbest
A: 

I've used the minidom module. That would probably work for you.

Fred Larson
+2  A: 

I don't know if there are any open source configuration generators.

Python has several xml parser modules, the newest (and perhaps most pythonic) of which is ElementTree. You can find additional documentation for it at the developer's site.

I recommend avoiding xml in your configuration files if possible. A simple flat file full of name/value pairs is much easier for humans to work with. If you need a level or two of structure, ini-style files ought to do nicely, and python comes with a built-in parser for them.

Forest
Thanks for the suggestion. But I need to generate dozens of ini files mentioned in my question. I would like to have the ini save as a template. And my real question is how to save those dozens of information? What kind of structure would be most convenient for human to manage the configuration, but also easier to generate once there's a change that needs to apply on all ini files. Please see my revised question. Thanks.
Stan
To clarify, I am suggesting that you use a flat name/value text file or ini file as your *source* file, and use the dozens of data within it to fill your template files.
Forest
+2  A: 

I recommend using excellent ConfigObj library by Michael Foord. It can read/write configuration files, even with nested sections.

andreypopp