1

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 something like this:

os.open("file1.cfg.template").read() % myhash

where myhash is hash of configuration parameters from the main config file 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.

user236215
  • 7,278
  • 23
  • 59
  • 87
  • if anyone could give an example in python or a third party framework how to achieve this exact behavior, that would be fantastic. – user236215 May 18 '10 at 22:42

3 Answers3

3

Sounds like you may have outgrown your originally simple home-grown templating solution. Maybe you should move to something like Jinja? It might be less of a headache to simply implement a third-party solution than it would be to create/continue to maintain your own solution.

Other options:

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • I found people have a lot of good things to say about Jinja from the link from my own answer. Do you personally recommend it over other template language? Thanks for tipping us off. – Wai Yip Tung May 18 '10 at 22:53
0

Maybe you can use a standalone Django template.

How do I use Django templates without the rest of Django? - Stack Overflow

Community
  • 1
  • 1
Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47
  • can you give an example how can I achieve the above behavior with Django? that is having the template code in the file, reading it, evaluating it, with IF blocks to ignore the second line. – user236215 May 18 '10 at 22:40
  • You can find example in Chapter 4: The Django template system http://www.djangobook.com/en/beta/chapter04/ . Check for the {% if %} tag. – Wai Yip Tung May 18 '10 at 22:50
0

Given that the files already exist, I would set default values for things like CLIENT2 (assuming you know ahead of time all possible keys). You can probably set the default value to something unusual so you can do

config = os.open("file1.cfg.template").read() % myhash
config = [l for l in config.split('\n') if <l does not have unusual text>].join('\n')

I agree with others that in the long term a more robust template would be better.

Kathy Van Stone
  • 25,531
  • 3
  • 32
  • 40
  • Is there a way to default template variables in a string if they do not appear in myhash. Otherwise, setting defaults is going to very hard. – user236215 May 18 '10 at 22:38
  • I found a solution. I will evaluate each line separately with exception handling. Thanks. – user236215 May 18 '10 at 22:57