Python Cheetah - 指定模板的名称/值对
我正在尝试模板化我的 Apache httpd 配置以部署到不同的环境,并且我想使用 Python 语言 Cheetah 应用程序来执行此操作。然而,我在使用命令行 cheetah 程序时遇到了困难,我相信这是我对 Cheetah 的误解以及缺乏文档的结合。
我的目标是拥有一个 httpd.conf 模板文件并替换环境特定定义文件中的变量。
httpd.tmpl:
Listen $HTTP_PORT
...
#if $ENABLE_HTTPS == true
<Virtual Host *:$HTTPS_PORT>
...
</VirtualHost>
#end if
生产.env:
HTTP_PORT=34120
HTTPS_PORT=34121
ENABLE_HTTPS=true
填充此 Cheetah 模板所需的命令行是什么?我使用过:
cheetah f --oext conf --debug httpd
但显然 prod.env
没有被读取为输入文件。将 #include
添加到我的模板文件顶部:
#include "prod.env"
并且没有找到我的名字:
Cheetah.NameMapper.NotFound: cannot find 'APACHE_PORT'
无论如何,这都不是理想的情况,因为我希望能够在模板文件上指定名称/值映射文件每次调用 cheetah 的命令行。
谢谢!
编辑:我知道我可以编写一个 python 脚本来执行文件读取,然后使用 Cheetah API 进行替换。但是,我正在寻找一种使用命令行来填充我的模板的方法。
已解决
感谢 @pyfunc 提供的文档链接,我现在了解如何实现此目的。主要问题是在 cheetah 命令行上提供 --env
,将当前环境变量传递给 cheetah。然而,必须首先导出这些环境变量。
I am trying to template-ize my Apache httpd configuration for deployment to different environments and I would like to use the Python language Cheetah application to do so. However, I am having difficulty with the command line cheetah
program and I believe its a combination of my misunderstanding Cheetah along with a lack of documentation.
My goal is to have a single httpd.conf template file and substitute variables from an environment specific definition file.
httpd.tmpl:
Listen $HTTP_PORT
...
#if $ENABLE_HTTPS == true
<Virtual Host *:$HTTPS_PORT>
...
</VirtualHost>
#end if
production.env:
HTTP_PORT=34120
HTTPS_PORT=34121
ENABLE_HTTPS=true
What is the command line needed to fill this Cheetah template? I've used:
cheetah f --oext conf --debug httpd
But obviously prod.env
is not read as an input file. Adding an #include
to the top of my template file:
#include "prod.env"
And none of my names are found:
Cheetah.NameMapper.NotFound: cannot find 'APACHE_PORT'
This is not the ideal situation anyway, because I want the ability to specify the name/value mapping file on the command line for each invocation of cheetah.
Thanks!
EDIT: I am aware that I can write a python script to perform the file reading and then substitute using the Cheetah API. However, I'm looking for a way to use the command line to fill my template.
SOLVED
Thanks to the documentation link provided by @pyfunc I now understand how to accomplish this. The main issue is to supply --env
on the cheetah command line which passes the current environment variables to cheetah. These environment variables must be exported first however.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cheetah 将 #include 文本的每个块包装在嵌套的 Template 对象内。
使用
同时
要包含不同的环境文件,您也将对其进行模板化。
请查看以下示例,对您有所帮助。
Cheetah wraps each chunk of #include text inside a nested Template object.
Use
also
To include different env files, you will have too templatize that too.
Please look at the following for examples that should help you.