from ConfigParser import SafeConfigParser class SimpleConfigParser(SafeConfigParser, object): """SafeConfigParser that auto-loads files This class also supports a more terse form of get that uses the default section which can be passed to the constructor. """ def __init__(self, filename, default_section=None): super(SimpleConfigParser, self).__init__() self.default_section = default_section with open(filename) as fp: self.readfp(fp) def get(self, section, option=None): """Get that allows skipping section This can be called with one or two arguments. The two argument form is the same as ConfigParser. The one argument form allows skipping the section if the class has a default_section set. """ default_get = super(SimpleConfigParser, self).get if not option and self.default_section: return default_get(self.default_section, section) else: return default_get(section, option)