]> granicus.if.org Git - python/commitdiff
Give ConfigParser the capability to set as well as read options, and to write
authorEric S. Raymond <esr@thyrsus.com>
Mon, 10 Jul 2000 18:11:00 +0000 (18:11 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 10 Jul 2000 18:11:00 +0000 (18:11 +0000)
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.

Doc/lib/libcfgparser.tex
Lib/ConfigParser.py

index 2f3536b9c7232c03e507d80a690611de3eb1e68b..32851f5ef07b0cb495f92fc0d18dc3e2b22576a0 100644 (file)
@@ -5,6 +5,7 @@
 \modulesynopsis{Configuration file parser.}
 \moduleauthor{Ken Manheimer}{klm@digicool.com}
 \moduleauthor{Barry Warsaw}{bwarsaw@python.org}
+\moduleauthor{Eric S. Raymond}{esr@thyrsus.com}
 \sectionauthor{Christopher G. Petrilli}{petrilli@amber.org}
 
 This module defines the class \class{ConfigParser}.
@@ -113,6 +114,11 @@ configuration. The \code{DEFAULT} section is not acknowledged.
 Returns a list of options available in the specified \var{section}.
 \end{methoddesc}
 
+\begin{methoddesc}{has_option}{section, option}
+If the given section exists, and contains the given option. return 1;
+otherwise return 0. (New in 1.6)
+\end{methoddesc}
+
 \begin{methoddesc}{read}{filenames}
 Read and parse a list of filenames.  If \var{filenames} is a string or
 Unicode string, it is treated as a single filename.
@@ -148,3 +154,14 @@ A convenience method which coerces the \var{option} in the specified
 for the option are \samp{0} and \samp{1}, any others will raise
 \exception{ValueError}.
 \end{methoddesc}
+
+\begin{methoddesc}{set}{section, option, value}
+If the given section exists, set the given option to the specified value;
+otherwise raise \exception{NoSectionError}. (New in 1.6)
+\end{methoddesc}
+
+\begin{methoddesc}{write}{fileobect}
+Write a representation of the configuration to the specified file
+object.  This representation can be parsed by a future \method{read()}
+call. (New in 1.6)
+\end{methoddesc}
index 38a85f4520bf73bd1d4ffd866ac31da11fe2ac5a..878b7900d399ef1185fa8b2f80db274a18ee1531 100644 (file)
@@ -286,6 +286,42 @@ class ConfigParser:
     def optionxform(self, optionstr):
         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
     # slight semantic change from the previous version, because of the use