Issue #908 resolved

CherryPy config doesn't support variable substitution

Anonymous created an issue

CP3 config parser doesn't support variable substitutions (interpolation). I'm trying to do this: {{{ [DEFAULT] dir = "/some/dir"

[my] my.dir = "%(dir)s/my/dir" }}}

The parser raises InterpolationError. I have fixed the problem with the attached patch. I hope this would be fixed in next release...

Reported by

Comments (10)

  1. Jason R. Coombs

    With klarson's help, we've found that the existing implementation is working as intended. The syntax you proposed for the config is slightly different than CherryPy expects. CherryPy does textual substitution before running through unrepr. To accomplish what you have suggested in the, use the following syntax:

            dir = "/some/dir"
            my.dir = %(dir)s + "/sub"
            my.dir = %(dir)s + "/my/dir"
            my.dir2 = %(my.dir)s + '/dir2'

    Please confirm this fix will work for you.

  2. Anonymous

    The workaround you suggested works, but clearly it's not pythonic. Additionally, I can't update substitution vars from python code (second case of my test case).

  3. Anonymous

    We really need CherryPy3 to support the second case. We are going to implement plugin system for our application where each plugins are supposed to have it's own config file, where we need to provide some substitution variables to be available to the plugins.

    As `cherrypy.config.defaults` are meant for adding those defaults to each sections, I think, it would better to use separate map (see my patch) to store substitution vars which are just intended to be used as substitution vars.

  4. Robert Brewer

    You can use anything you can import. So instead of:

    my.dir = %(dir)s + "/my/dir"
    my.dir2 = %(my.dir)s + "/dir2"


    my.dir = myapp.dir + "/my/dir"
    my.dir2 = myapp.submod.dir2 + "/dir2"

    For the first line, the parser will attempt to "import myapp" and then find a variable named "dir" in it, and use that. For the second, the parser will attempt to import "myapp", then see if there's a "submod" object in it, then see if "submod" has a "dir2" attribute.

  5. Log in to comment