模板化 - 从配置文件

时间:2016-07-07 14:36:16

标签: python configuration-files template-engine

票证脚本正在接受来自配置文件的输入。该配置文件包含一个电子邮件正文。如何最好地使客户能够在该电子邮件正文中引用静态变量列表?

我现在这样做的方法是训练用户允许哪些变量,并让变量被胡须“转义”,如下所示:

配置文件:

You have been assigned a ticket to fix {IP}. The host contains the following vulnerabilities:
{vulnerability}

The urgency is {priority}.

该脚本将接收此电子邮件正文并为其找到的每个服务器发送一封电子邮件。例如,有2个服务器,A和B.该脚本将发送以下电子邮件:

服务器A值:

  • IP = 192.168.1.2
  • 漏洞='端口443上的Heartbleed'
  • priority ='高'

服务器电子邮件输出:

You have been assigned a ticket to fix 192.168.1.2. The host contains the following vulnerabilities:
Heartbleed on port 443

The urgency is High.

服务器B值:

  • IP = 192.168.1.11
  • vulnerability ='OpenOffice 1.3'
  • priority ='低'

服务器B电子邮件输出:

You have been assigned a ticket to fix 192.168.1.11. The host contains the following vulnerabilities:
OpenOffice 1.3

The urgency is Low.

我问的问题,一般来说,如何让非Python用户能够自定义带有变量引用的字符串?

1 个答案:

答案 0 :(得分:0)

使用像pystache这样的小胡子模板库。

>>> import pystache
>>> renderer = pystache.Renderer()
>>> print renderer.render(parsed, {'who': 'Pops'})
Hey Pops!
>>> print renderer.render(parsed, {'who': 'you'})
Hey you!