.ini file parser (ConfigParser)

Discussion in 'Python' started by Ian Bicking, Oct 22, 2004.

  1. Ian Bicking

    Ian Bicking Guest

    I've written a simple parser for .ini files; it's not a replacement for
    ConfigParser, but you could build such a module ontop of it fairly
    easily, or you could build something either more or less complicated.

    svn://colorstudy.com/home/ianb/config/
    http://colorstudy.com/cgi-bin/viewcvs.cgi/home/ianb/config/

    For instance, here's the code to parse an ini file into a nested
    dictionary, like {'section': {'var1': ['value1']}}:


    class BasicParser(INIParser):

    def __init__(self):
    self.data = {}

    def assignment(self, name, content):
    if not self.section:
    self.parse_error(
    'Assignments can only occur inside sections; no '
    'section has been defined yet')
    section = self.data.setdefault(self.section, {})
    section.setdefault(name, []).append(content)

    usage:

    p = BasicParser()
    p.feed('config.ini')
    data = p.data

    .... though generally the parser should be a non-public implementation
    detail in a config library. Anyway, not a lot of code, but I think it's
    a good piece for building different config parsers. both simple and complex.

    --
    Ian Bicking / / http://blog.ianbicking.org
    Ian Bicking, Oct 22, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sphinx

    ini file

    Sphinx, Feb 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,360
    Gerry O'Brien [MVP]
    Feb 25, 2004
  2. chapman

    Update INI file problem

    chapman, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    426
    chapman
    Jul 31, 2003
  3. SeSe
    Replies:
    7
    Views:
    439
    Giovanni Bajo
    Feb 13, 2005
  4. Replies:
    3
    Views:
    418
  5. Tim Chase
    Replies:
    0
    Views:
    364
    Tim Chase
    Dec 3, 2008
Loading...

Share This Page