1

我是 DRY 的忠实拥护者,希望将我的CFG文件拆分为公共部分和特定于环境的部分,并让环境部分从公共部分“继承”,如下所示:

[generic]
basedir = ~/computational-services/jobs
pattern = Job*
usage-jar = ../lib/usage-logging-client.jar

[dev-test]
age = 7
dirs = [
     ('%(basedir)s/rundir01', %(age)s, '%(pattern)s', 'cleanupRunDir01'),
     ('%(basedir)s/linuxNonimmediate', %(age)s, '%(pattern)s', 'cleanupLinuxNonimmediate01'),
     ]

[prod]
age = 14
dirs = [
     ('%(basedir)s/rundir01', %(age)s, '%(pattern)s', 'cleanupRunDir01'),
     ]

现在,我收到以下错误:

ConfigParser.InterpolationMissingOptionError: Bad value substitution:
    section: [dev-test]
    option : dirs
    key    : basedir
    rawval : [
('%(basedir)s/rundir01', %(age)s, '%(pattern)s', 'cleanupRunDir01'),
('%(basedir)s/linuxNonimmediate', %(age)s, '%(pattern)s', 'cleanupLinuxNonimmediate01'),
]

如果我确实重复自己,并添加basedir一切pattern[dev-test]很好:

[dev-test]
age = 7
basedir = ~/computational-services/jobs
pattern = Job*
dirs = [
     ('%(basedir)s/rundir01', %(age)s, '%(pattern)s', 'cleanupRunDir01'),
     ('%(basedir)s/linuxNonimmediate', %(age)s, '%(pattern)s', 'cleanupLinuxNonimmediate01'),
     ]

有没有办法在另一个部分中使用一个键/值(甚至尝试过generic.basedirand generic:basedir)?

谢谢你。

4

1 回答 1

2

Ah, sorry, just checked the documentation again and read about DEFAULT:

[DEFAULT]
basedir = ~/computational-services/jobs
pattern = Job*
usage-jar = ../lib/usage-logging-client.jar

[dev-test]
age = 7
dirs = [
     ('%(basedir)s/rundir01', %(age)s, '%(pattern)s', 'cleanupRunDir01'),
     ('%(basedir)s/linuxNonimmediate', %(age)s, '%(pattern)s', 'cleanupLinuxNonimmediate01'),
     ]

Should spend as much time searching the documentation as checking SO. :)

于 2013-07-12T16:38:41.443 回答