[Python-checkins] CVS: python/dist/src/Lib ConfigParser.py,1.19,1.20

Eric S. Raymond python-dev@python.org
Fri, 14 Jul 2000 07:28:24 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv7481

Modified Files:
	ConfigParser.py 
Log Message:
ConfigParser enhancements to edit existing configs, part 2


Index: ConfigParser.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/ConfigParser.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -r1.19 -r1.20
*** ConfigParser.py	2000/07/10 18:11:00	1.19
--- ConfigParser.py	2000/07/14 14:28:22	1.20
***************
*** 37,40 ****
--- 37,43 ----
          return whether the given section exists
  
+     has_option(section, option)
+         return whether the given option exists in the given section
+ 
      options(section)
          return list of configuration options for the named section
***************
*** 69,72 ****
--- 72,87 ----
          like get(), but convert value to a boolean (currently defined as 0 or
          1, only)
+ 
+     remove_section(section)
+ 	remove the given file section and all its options
+ 
+     remove_option(section, option)
+ 	remove the given option from the given section
+ 
+     set(section, option, value)
+         set the given option
+ 
+     write(fp)
+ 	write the configuration state in .ini format
  """
  
***************
*** 311,326 ****
          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
--- 326,363 ----
          if self.__defaults:
              fp.write("[DEFAULT]\n")
!             for (key, value) in self.__defaults.items():
!                 fp.write("%s = %s\n" % (key, value))
              fp.write("\n")
          for section in self.sections():
              fp.write("[" + section + "]\n")
              sectdict = self.__sections[section]
!             for (key, value) in sectdict.items():
                  if key == "__name__":
                      continue
!                 fp.write("%s = %s\n" % (key, value))
              fp.write("\n")
  
+     def remove_option(section, option):
+         """Remove an option."""
+         if not section or section == "DEFAULT":
+             sectdict = self.__defaults
+         else:
+             try:
+                 sectdict = self.__sections[section]
+             except KeyError:
+                 raise NoSectionError(section)
+         existed = sectdict.has_key(key)
+         if existed:
+             del sectdict[key]
+         return existed
+ 
+     def remove_section(section):
+         """Remove a file section."""
+         if self.__sections.has_key(section):
+             del self.__sections[section]
+             return 1
+         else:
+             return 0
+ 
      #
      # Regular expressions for parsing section headers and options.  Note a
***************
*** 394,398 ****
                      if mo:
                          optname, vi, optval = mo.group('option', 'vi', 'value')
-                         optname = string.lower(optname)
                          if vi in ('=', ':') and ';' in optval:
                              # ';' is a comment delimiter only if it follows
--- 431,434 ----