3

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.

0

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

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.
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.
1

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.

Comments

1

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

Comments

1

You need some template engine, look at string.Template

1 Comment

you should avoid generating XML by plain string concatenation because you can easily run into encoding problems. after all you generated a huge XML file an can't parse it because the encoding is broken - happened more than once to me... since that i only use xml.minidom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.