Using ExtendedInterpolation with Logging.Config.FileConfig

I am looking for a way to use the ExtendedInterpolation functionality found in the configparser lib file when loading in the ini file in Logging.config.FileConfig.

http://docs.python.org/3/library/configparser#configparser.ExtendedInterpolation

So, if I have an ini file that looks like this:

[logSettings] eventlogs=application logfilepath=C:\Programs\dk_test\results\dklog_009.log levelvalue=10 [formatters] keys=dkeventFmt,dklogFmt [handlers] keys=dklogHandler [handler_dklogHandler] class=FileHandler level=${logSettings:levelvalue} formatter=dklogFmt args=(${logSettings:logfilepath}, 'w') [logger_dklog] level=${logSettings:levelvalue} handlers=dklogHandler 

As you can see, I follow the syntax of extended interpolation using the notation $ {...} to refer to the value in another section. When calling such a logging.config.fileConfig(filepath) file, eval'ing inside the module always fails. In paticular on eval'ing the args option in the [handler_dklogHandler] section.

Is there any way around this? Thanks!

Note: Using Python 3.2

+4
source share
1 answer

It was decided to use the power of file interpolation and save the result in another temporary file. I am using the temp file for logconfig.

The function is as follows:

 tmpConfigDict = {} tmpConfig = ConfigParser(allow_no_value = True, interpolation = ExtendedInterpolation()) for path in configPaths: tmpConfig.read(path) #Iterate over options and use "get()" to execute the Interpolation for sec in tmpConfig.sections(): tmpConfigDict[sec] = {} for opt, _ in tmpConfig[sec].items(): tmpConfigDict[sec][opt] = cleanValue(tmpConfig.get(sec, opt)) #Finished getting values. Write the dict to the configparser tmpConfig.read_dict(tmpConfigDict) #Open the file handle and close it when done with open(pathToTmpFile, 'w') as fp: tmpConfig.write(fp, space_around_delimiters = False) 
+1
source

All Articles