Calling a user-defined function from the configuration file in Python’s configparser module

Using Python and its configparser module I want to have an .ini file that results in a user-space function call. It doesn’t matter when the function is called, upon file read or parameter evaluation.

Example configuration file.

[section]
param = @to_upper_case( a_lower_case_string )

In my example I’d like the configuration file reader object to call the user defined function to_upper_case passing it the value a_lower_case_string. Of course to_upper_case would have to be known to the configparser before hand, and it would be defined in the user’s Python code. I arbitrarily chose the @ symbol to indicate a function, for this example.

I am already aware of the ${...} parameter reference feature available through the ExtendedInterpolation() object, but it does not seem to offer function callbacks.

Answer

Assuming a_lower_case_string is a literal string and not a variable,

from configparser import BasicInterpolation, ConfigParser


class Interpolation(BasicInterpolation):
    def before_get(self, parser, section: str, option: str, value: str, defaults) -> str:
        if value.startswith("@"):
            func = value.split("(", 1)
            rest = func[1].rsplit(")", 1)[0].strip()
            return parser.namespace[func[0].strip("@ ")](rest)
        return value

class Config(ConfigParser):
    def __init__(self, namespace, *args, **kwargs):
        self.namespace = namespace
        super().__init__(*args, **kwargs)


r = Config({"to_upper_case": str.upper}, interpolation=Interpolation())
r.read_string("""
[section]
param = @to_upper_case( a_lower_case_string )
""")
print(list(r["section"].items()))

This ends up in [('param', 'A_LOWER_CASE_STRING')].

Note: You must use the Config class and specify a namespace which contains the functions.