I am looking for a way to use the ExtendedInterpolation functionality found in the configparser lib when loading in a ini file to Logging.config.FileConfig.
http://docs.python.org/3/library/configparser#configparser.ExtendedInterpolation
So if I have a 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 am following the extended interpolation syntax by using the ${...} notation to reference a value in a different section. When calling the file like so logging.config.fileConfig(filepath)
, the eval'ing within the module always fails. In paticular on the eval'ing of the args option in the [handler_dklogHandler]
section.
Is there a way to get around this? Thanks!
Note: Using Python 3.2
Decided to use force the interpolation over the file and save the result to another temp file. I use the temp file for the logconfig.
The function looks like this:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With