Default Section Values in ConfigParser

Terry Hancock hancock at anansispaceworks.com
Tue Feb 28 22:13:39 EST 2006


On 28 Feb 2006 17:05:32 -0800
"mwt" <michaeltaft at gmail.com> wrote:
> I want to set default values for a ConfigParser. So far,
> its job is very small, so there is only one section
> heading, ['Main']. Reading the docs, I see that in order
> to set default values in a ConfigParser, you initialize it
> with a dictionary or defaults.

You know, I never noticed that, but there is another way.

I used a multi-line string constant with the same syntax
as the original file, and just fed that in as the first
source (this is particularly easy, so I think it must have
been intended).  Slightly snipped version of my code for
this:

default_cfg = StringIO("""\
[VARIMAGE]
V1_COMPATIBILITY: ON

[SECURITY]
MAX_OPERATORS: 0
""")

# Look for varimage.cfg in 3 possible locations:
config = ConfigParser.ConfigParser()
config.readfp(default_cfg)
config.read(['/etc/varimage.cfg',           
os.path.expandvars('${INSTANCE_HOME}/varimage.cfg'),        
   os.path.join(pkghome, 'varimage.cfg') ])


-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list