Default Section Values in ConfigParser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mwt

    Default Section Values in ConfigParser

    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. However, I'm not quite
    sure of the syntax to add the section headings in to the dictionary of
    defaults. For now, I'm doing it like this:

    default_values = {'username' : 'put username here',
    'teamnumber': 'team number here',
    'update_interva l' : 'update interval'}
    self.INI = ConfigParser.Co nfigParser(defa ult_values)
    self.INI.add_se ction('Main')

    This works, but clearly won't last beyond the adding of a second
    section. What is the correct way to initialize it with a full
    dictionary of defaults, including section names?

    Thanks.

  • Terry Hancock

    #2
    Re: Default Section Values in ConfigParser

    On 28 Feb 2006 17:05:32 -0800
    "mwt" <michaeltaft@gm ail.com> wrote:[color=blue]
    > 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.[/color]

    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_COMPATIBILIT Y: ON

    [SECURITY]
    MAX_OPERATORS: 0
    """)

    # Look for varimage.cfg in 3 possible locations:
    config = ConfigParser.Co nfigParser()
    config.readfp(d efault_cfg)
    config.read(['/etc/varimage.cfg',
    os.path.expandv ars('${INSTANCE _HOME}/varimage.cfg'),
    os.path.join(pk ghome, 'varimage.cfg') ])


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

    Comment

    • mwt

      #3
      Re: Default Section Values in ConfigParser

      Thanks, Terry. That's an interesting way to go about it.

      Comment

      • Fuzzyman

        #4
        Re: Default Section Values in ConfigParser


        mwt wrote:[color=blue]
        > 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. However, I'm not quite
        > sure of the syntax to add the section headings in to the dictionary of
        > defaults. For now, I'm doing it like this:
        >
        > default_values = {'username' : 'put username here',
        > 'teamnumber': 'team number here',
        > 'update_interva l' : 'update interval'}
        > self.INI = ConfigParser.Co nfigParser(defa ult_values)
        > self.INI.add_se ction('Main')
        >
        > This works, but clearly won't last beyond the adding of a second
        > section. What is the correct way to initialize it with a full
        > dictionary of defaults, including section names?
        >[/color]

        An alternative approach is to use `ConfigObj
        <http://www.voidspace.o rg.uk/python/configobj.html> `_. It has two ways
        of specifying default values.

        The first way is to provide a configspec. This is a schema (that looks
        very like a config file itself) that specifies the type (and paramater
        bounds if you want) for each member. It can also include a default
        value.

        Simpler (although without the benefit of validation and type
        conversion) is to use the ``merge`` method which is a recursive update.
        (Although for config files that are a maximum of one section deep, the
        dictionary method ``update`` will do the same job).

        default_values = {'username' : 'put username here',
        'teamnumber': 'team number here',
        'update_interva l' : 'update interval'}
        user_values = ConfigObj(filen ame)
        cfg = ConfigObj(defau lt_values)
        cfg.merge(user_ values)

        Note that for a config file with only a few values in it, ConfigObj
        doesn't force you to use a section if it's not needed. To put your
        default values into a 'Main' section you would actually do :

        default_values = { 'Main': {'username' : 'put username here',
        'teamnumber': 'team number here',
        'update_interva l' : 'update interval'}
        }
        #
        user_values = ConfigObj(filen ame)
        cfg = ConfigObj(defau lt_values)
        cfg.merge(user_ values)

        All the best,

        Fuzzyman


        [color=blue]
        > Thanks.[/color]

        Comment

        Working...