Graham Dumpleton Graham Dumpleton - 1 year ago 73
Python Question

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:

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:

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

In the template input file then have:


instead of:


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:


or perhaps even:


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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download