防止构建时发生变量替换

发布于 2024-07-27 19:46:38 字数 1103 浏览 7 评论 0原文

是否有一种简单的方法可以在 buildout 配置中转义用于变量替换的魔术字符,使得字符串为留下一个。 换句话说,我说:

[part]
attribute = ${variable}

我实际上并不希望它扩展 ${variable} 而是将其保留为文字值。

实际上,我遇到的具体问题不是在构建配置文件本身中,而是在由配方“collective.recipe.template”处理的模板文件中。 这使用配置文件中使用的构建中相同的变量替换引擎。 问题是,我想用作模板的文件已经使用“${variable}”语法来实现其自身目的,并与最终使用该文件的应用程序配置系统结合使用。

我发现解决这个问题的唯一方法是使用类似的东西:

[server-xml]
recipe = collective.recipe.template
input = templates/server.xml.in
output = ${product:build-directory}/conf/server.xml
dollar = $

在模板输入文件中然后有:

${dollar}{variable}

而不是:

${variable}

它已经有。

这样做的目的是使用模板针对该部分查找“dollar”属性并将其替换为“$”。

而不是必须这样做,而是希望人们能够做到:

\${variable}

或者甚至:

$${variable}

并消除必须有一个虚拟属性来欺骗它做我想做的事情的需要。

查看构建的源代码,它匹配变量替换的方式似乎没有提供转义机制。

如果确实没有办法,那么也许有人知道另一种用于构建的模板配方,它可以进行变量扩展,但为它指示变量的任何方式提供了一种转义机制,这样就可以避免在变量之间可能发生冲突的问题。模板系统扩展机制和正在模板化的文件中的文字数据。

Is there a simple way of escaping the magic characters used for variable substitution in a buildout configuration, such that the string is left alone. In other words, where I say:

[part]
attribute = ${variable}

I don't actually want it to expand ${variable} but leave it as the literal value.

In practice the specific problem I am encountering is not in the buildout configuration file itself, but in a template file processed by the recipe 'collective.recipe.template'. This uses the same variable substitution engine from buildout that is used in the configuration files. Problem is that the file I want to use as a template already uses '${variable}' syntax for its own purposes in conjunction with the application configuration system which ultimately consumes the file.

The only way I have found to get around the problem is to use something like:

[server-xml]
recipe = collective.recipe.template
input = templates/server.xml.in
output = ${product:build-directory}/conf/server.xml
dollar = $

In the template input file then have:

${dollar}{variable}

instead of:

${variable}

that it already had.

What this is doing is cause a lookup of 'dollar' attribute against the section using the template and replace it with '$'.

Rather than have to do that, was sort of hoping that one could do:

\${variable}

or perhaps even:

${variable}

and eliminate the need to have to have a dummy attribute to trick it into doing what I want.

Looking at the source code for buildout, the way it matches variable substitution doesn't seem to provide an escape mechanism.

If there is indeed no way, then perhaps someone knows of an alternate templating recipe for buildout that can do variable expansion, but provides an escape mechanism for whatever way it indicates variables, such that one can avoid problems where there may be a clash between the templating systems expansion mechanism and literal data in the file being templated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-08-03 19:46:38

恐怕您对构建变量替换代码(collective.recipe.template 所依赖的)的分析是正确的。 没有用于转义 ${section:variable} 变量替换的语法,并且提供 ${dollar} 替换的解决方案是我能想到的最佳解决方法。

当然,您也可以向 zc.buildout 团队提出一个补丁,以添加对转义变量替换语法的支持。 :-)

I am afraid your analysis of the buildout variable substitution code (which collective.recipe.template relies on) is correct. There is no syntax for escaping a ${section:variable} variable substitution and your solution of providing a ${dollar} substitution is the best workaround I can think of.

You could of course also propose a patch to the zc.buildout team to add support for escaping the variable substitution syntax. :-)

罗罗贝儿 2024-08-03 19:46:38

从 Collective.recipe.template 版本 1.7 开始,您可以使用 genshi 文本模板,但从版本 1.8 开始,由于进行了一些修复,它很有用。

recipe = collective.recipe.template[genshi]:genshi
...
mymessage = Hello

因此输入文件看起来像

The message in ${:mymessage} is: ${options['mymessage']}

genshi 允许转义美元,请参阅 http://genshi.edgewall。 org/wiki/Documentation/templates.html#escaping

有关如何将配方与 genshi 一起使用的更多详细信息,请访问 http://pypi.python.org/pypi/collective.recipe.template#genshi-text-templates

since version 1.7 of collective.recipe.template you can use genshi text templates, but since version 1.8 its useful because of some fixes made.

recipe = collective.recipe.template[genshi]:genshi
...
mymessage = Hello

so the input-file it looks like

The message in ${:mymessage} is: ${options['mymessage']}

genshi allows escaping of the dollar, see http://genshi.edgewall.org/wiki/Documentation/templates.html#escaping

More details of how to use teh recipe with genshi at http://pypi.python.org/pypi/collective.recipe.template#genshi-text-templates

俯瞰星空 2024-08-03 19:46:38

${ 之间插入空替换应该可以防止构建将生成的文本评估为构建替换。

buildout.cfg:

[server-xml]
recipe = collective.recipe.template
input = server.xml.in
output = server.xml
_ =

server.xml.in:

do no substitution ${_}{myvar} blah

server.xml:

do no substitution ${myvar} blah

Inserting an empty substitution between the $ and the { should prevent buildout from evaluating the resulting text as a buildout substitution.

buildout.cfg:

[server-xml]
recipe = collective.recipe.template
input = server.xml.in
output = server.xml
_ =

server.xml.in:

do no substitution ${_}{myvar} blah

server.xml:

do no substitution ${myvar} blah
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文