template files in python
        Posted  
        
            by saminny
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by saminny
        
        
        
        Published on 2010-05-18T22:18:15Z
        Indexed on 
            2010/05/18
            22:20 UTC
        
        
        Read the original article
        Hit count: 324
        
python
I am trying to use python for translating a set of templates to a set of configuration files based on values taken from a main configuration file. However, I am having certain issues. Consider the following example of a template file.
file1.cfg.template
%(CLIENT1)s %(HOST1)s  %(PORT1)d  C  %(COMPID1)s
%(CLIENT2)s %(HOST2)s  %(PORT2)d  C  %(COMPID2)s
This file contains an entry for each client. There are hundreds of config files like this and I don't want to have logic for each type of config file. Python should do the replacements and generate config files automatically given a set of global values read from a main xml config file. However, in the above example, if CLIENT2 does not exist, how do I delete that line? I expect Python would generate the config file using the following simple code:
os.open("file1.cfg.template").read() & myhash
where myhash is hash of all configuration parameters which may not contain CLIENT2 at all. In the case it does not contain CLIENT2, I want that line to disappear from the file. Is it possible to insert some 'IF' block in the file and have python evaluate it?
Thanks for your help. Any suggestions most welcome.
© Stack Overflow or respective owner