--- configparser.py 2009-03-02 20:32:48.000000000 -0500 +++ configparser_patched.py 2009-03-03 10:05:22.000000000 -0500 @@ -59,6 +59,10 @@ provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. + __getitem__(reference) + reference should be section, option + return get(section, option) + getint(section, options) like get(), but convert value to an integer @@ -80,9 +84,17 @@ remove_option(section, option) remove the given option from the given section + __delitem__(reference) + reference should be section, option + return remove_option(section, option) + set(section, option, value) set the given option + __setitem__(reference, value) + reference should be section, option + return set(section, option, value) + write(fp) write the configuration state in .ini format """ @@ -219,6 +231,14 @@ self.lineno = lineno self.line = line +class InvalidConfigReferenceError(Error): + """Raised when __XXXitem__ is passed an invalid reference""" + def __init__(self, reference): + Error.__init__(self, + "Reference %r not in form ('section, option')" % + (reference, )) + self.reference = reference + class RawConfigParser: def __init__(self, defaults=None, dict_type=_default_dict): @@ -326,6 +346,13 @@ else: raise NoOptionError(option, section) + def __getitem__(self, reference): + try: + section, option = reference + except ValueError: + raise InvalidConfigReferenceError(reference) + return self.get(section, option) + def items(self, section): try: d2 = self._sections[section] @@ -383,6 +410,13 @@ raise NoSectionError(section) sectdict[self.optionxform(option)] = value + def __setitem__(self, reference, value): + try: + section, option = reference + except ValueError: + raise InvalidConfigReferenceError(reference) + return self.set(section, option, value) + def write(self, fp): """Write an .ini-format representation of the configuration state.""" if self._defaults: @@ -413,6 +447,13 @@ del sectdict[option] return existed + def __delitem__(self, reference): + try: + section, option = reference + except ValueError: + raise InvalidConfigReferenceError(reference) + return self.remove_option(section, option) + def remove_section(self, section): """Remove a file section.""" existed = section in self._sections