[Python-Dev] ConfigParser with a single section

Fredrik Lundh fredrik@pythonware.com
Fri, 8 Nov 2002 00:40:25 +0100


Gustavo Niemeyer wrote:

> The most compeling reason I could see is:
> 
> >>> import ConfigParser
> >>> cfg = ConfigParser.ConfigParser(singlesection="shell")
> >>> cfg.read("/etc/sysconfig/network-scripts/ifcfg-lo")
> >>> cfg.get("shell", "ipaddr")
> '127.0.0.1'

don't forget that you can subclass stuff in Python:

import ConfigParser, StringIO

class MyConfigParser(ConfigParser.ConfigParser):
    def read(self, filename):
        try:
            text = open(filename).read()
        except IOError:
            pass
        else:
            file = StringIO.StringIO("[shell]\n" + text)
            self.readfp(file, filename)

cfg = MyConfigParser()
cfg.read("/etc/sysconfig/network-scripts/ifcfg-lo")
cfg.get("shell", "ipaddr")

</F>