pythonbuildout

Can we have a variable in the option part of Buildout's config file?


Python's Buildout configuration file allows us to avoid repetition of values by allowing a special syntax known as variable substitution which is of the form ${SECTION:OPTION}

This is an example which allows us to avoid repeating the word experiment:

[context]
name = experiment

[db]
server = ${context:name}

Is it possible to use substitution for the option itself?

For example:

[soures]
${context:name} = https://git.com/${context:name}.git

Solution

  • No, variable expansion in keys is not supported; the feature is actually named value substitions, to make it clear the syntax only applies to values:

    When supplying values in a configuration, you can include values from other options using the syntax:

    ${SECTION:OPTION}
    

    The options syntax also explicitly excludes the characters needed to make substitutions possible:

    Options are specified with an option name followed by an equal sign and a value:

    parts = py 
    

    Option names may have any characters other than whitespace, square braces, curly braces, equal signs, or colons

    with an exception noted for the short-hand <part-dependencies> syntax.

    So, in the end, when variable substitutions are applied in buildout, the code looks for the ${ part of the syntax in the values only:

    # force substitutions
    for k, v in sorted(self._raw.items()):
        if '${' in v:
            self._dosub(k, v)