This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ConfigParser calls Interpolation.before_read after reading
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Anaphory
Priority: normal Keywords:

Created on 2017-01-20 11:50 by Anaphory, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
earlyinterpolation.py Anaphory, 2017-01-20 11:50 Python module: A subclass of `BasicInterpolation` and a subclass of `ConfigParser` which in concert allow recursive interpolation.
Messages (1)
msg285902 - (view) Author: Gereon Kaiping (Anaphory) Date: 2017-01-20 11:50
In its current implementation, `ConfigParser` calls its Interpolation's `before_read` method in the very last step of `_read`, when all properties have already been overwritten by their new uninterpolated values.

I am developing a program with modular config files: It is possible to supply several configuration files on the command line, and they are all fed through the `.read` method.

Now it would be amazing to use `read` time interpolation instead of `get` time interpolation to construct things like

outputfilename = %(outputfilename)s_extension_from_this_ini_module

By looking at the `Interpolation` class, it seems that behaviour like this should be supported by supplying a `before_read` as follows.

    def before_read(self, parser, section, option, value):
        L = []
        interpolations = parser[section]
        self._interpolate_some(
            parser, option, L, value, section, interpolations, 1)
        return ''.join(L)

However, this is not possible, because `before_read` is only called *after* all values in the config file have been read and all old values in the ConfigParser object have been overridden.

The attached file contains a subclass of `BasicInterpolation` and a subclass of `ConfigParser` which in concert allow me to write recursive property definitions as given above.

The downside of this change is that (a) interpolation values can't be defined *after* they are used any more, and (b) it is not possible to hack this parser to accept multi-line option names as interpolations. (To me personally, both of these don't sound like useful features to begin with.)
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73519
2017-01-25 13:32:16Anaphorysettype: behavior
components: + Library (Lib)
versions: + Python 3.5, Python 3.6
2017-01-20 11:50:36Anaphorycreate