Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing variable substitutions from occurring with buildout

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.

like image 228
Graham Dumpleton Avatar asked Jul 16 '09 03:07

Graham Dumpleton


3 Answers

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. :-)

like image 56
Martijn Pieters Avatar answered Oct 21 '22 19:10

Martijn Pieters


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

like image 5
jensens Avatar answered Oct 21 '22 17:10

jensens


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
like image 3
Adam Terrey Avatar answered Oct 21 '22 18:10

Adam Terrey