Approach for parsing file and creating dynamic data structure for use by another program

Posted by user275633 on Stack Overflow See other posts from Stack Overflow or by user275633
Published on 2011-03-17T20:02:13Z Indexed on 2011/03/18 8:09 UTC
Read the original article Hit count: 179

Filed under:

All,

Background: I have a customer who has some build scripts for their datacenter based on python that I've inherited. I did not work on the original design so I'm sort of limited to some degree on what I can and can't change. That said, my customer has a properties file that they use in their datacenter. Some of the values are used to build their servers and unfortunately they have other applications that also use these values so I cannot change them to make it easier for me.

What I want to do is make the scripts more dynamic to distribute more hosts so that I don't have to keep updating the scripts in the future and can just add more hosts to the property file. Unfortunately I can't change the current property file and have to work with it.

The property file looks something like this:

projectName.ClusterNameServer1.sslport=443

projectName.ClusterNameServer1.port=80

projectName.ClusterNameServer1.host=myHostA

projectName.ClusterNameServer2.sslport=443

projectName.ClusterNameServer2.port=80

projectName.ClusterNameServer2.host=myHostB

In their deployment scripts they basically have alot of if projectName.ClusterNameServerX where X is some number of entries defined and then do something, e.g.:

if projectName.ClusterNameServer1.host != "" do X

if projectName.ClusterNameServer2.host != "" do X

if projectName.ClusterNameServer3.host != "" do X

Then when they add another host (say Serve4) they've added another if statement.

Question: What I would like to do is make the scripts more dynamic and parse the properties file and put what I need into some data structure to pass to the deployment scripts and then just iterate over the structure and do my deployment that way so I don't have to constantly add a bunch of if some host# do something. I'm just curious to feed some suggestions as to what others would do to parse the file and what sort of data structure would they use and how they would group things together by ClusterNameServer# or something else.

Thanks

© Stack Overflow or respective owner

Related posts about python