Domanda

I'm a big proponent of DRY and would like to split my CFG file into a common section and env-specific sections, and have the env-sections "inherit" from the common section, like so:

[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'),
     ]

Right now, I'm getting the following error:

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'),
]

If I do repeat myself, and add basedir and pattern to [dev-test] all is good:

[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'),
     ]

Is there a way to use a key/value from one section in another (even tried generic.basedir and generic:basedir)?

Thank you.

È stato utile?

Soluzione

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. :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top