8

Logging.config.FileConfig に ini ファイルをロードするときに、configparser lib にある ExtendedInterpolation 機能を使用する方法を探しています。

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

したがって、次のようなiniファイルがあるとします。

[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

ご覧のとおり、${...} 表記を使用して別のセクションの値を参照することで、拡張補間構文に従っています。so のようにファイルを呼び出すとlogging.config.fileConfig(filepath)、モジュール内の評価は常に失敗します。特に、セクションのargsオプションの評価について[handler_dklogHandler]

これを回避する方法はありますか?ありがとう!

注: Python 3.2 を使用

4

1 に答える 1

2

ファイルに対して強制補間を使用し、結果を別の一時ファイルに保存することにしました。logconfig には一時ファイルを使用します。

関数は次のようになります。

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)
于 2013-02-01T14:14:07.320 に答える