.ini file parser (ConfigParser)

I

Ian Bicking

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top