[Python-checkins] CVS: python/dist/src/Lib ConfigParser.py,1.18,1.19
Eric S. Raymond
python-dev@python.org
Mon, 10 Jul 2000 11:11:02 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv18589/dist/src/Lib
Modified Files:
ConfigParser.py
Log Message:
Give ConfigParser the capability to set as well as read options, and to write
a representation of the configuration state in .ini format that can be read
back in by a future read() call. Thus this class is now a back end
for .ini editors as well as parsers.
This patch is complete and tested, but exposes a bug in the ConfigParser
implementation which I have not yet fixed. Because case information is
discarded during parsing, the output of write() has its case smashed.
I wrote this for a SourceForge interface script called forgetool.
Documentation for the new entry points included.
Index: ConfigParser.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/ConfigParser.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** ConfigParser.py 2000/05/09 14:46:40 1.18
--- ConfigParser.py 2000/07/10 18:11:00 1.19
***************
*** 287,290 ****
--- 287,326 ----
return string.lower(optionstr)
+ def has_option(self, section, option):
+ """Check for the existence of a given option in a given section."""
+ if not section or section == "DEFAULT":
+ return self.__defaults.has_key(option)
+ elif not self.has_section(section):
+ return 0
+ else:
+ return self.__sections[section].has_key(option)
+
+ def set(self, section, option, value):
+ """Set an option."""
+ if not section or section == "DEFAULT":
+ sectdict = self.__defaults
+ else:
+ try:
+ sectdict = self.__sections[section]
+ except KeyError:
+ raise NoSectionError(section)
+ sectdict[option] = value
+
+ def write(self, fp):
+ """Write an .ini-format representation of the configuration state."""
+ if self.__defaults:
+ fp.write("[DEFAULT]\n")
+ for key in self.__defaults.keys():
+ fp.write(key + " = " + self.__defaults[key] + "\n")
+ fp.write("\n")
+ for section in self.sections():
+ fp.write("[" + section + "]\n")
+ sectdict = self.__sections[section]
+ for key in sectdict.keys():
+ if key == "__name__":
+ continue
+ fp.write(key + " = " + str(sectdict[key]) + "\n")
+ fp.write("\n")
+
#
# Regular expressions for parsing section headers and options. Note a