[Python-checkins] python/dist/src/Lib ConfigParser.py,1.38.10.2,1.38.10.3

fdrake@users.sourceforge.net fdrake@users.sourceforge.net
Thu, 26 Sep 2002 12:37:38 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv24246/Lib

Modified Files:
      Tag: release22-maint
	ConfigParser.py 
Log Message:
Previous fix depended on the "code cleanup and general bug fix patch" already
being applied.  This is portion of that patch that does not add new
functionality.


Index: ConfigParser.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/ConfigParser.py,v
retrieving revision 1.38.10.2
retrieving revision 1.38.10.3
diff -C2 -d -r1.38.10.2 -r1.38.10.3
*** ConfigParser.py	26 Sep 2002 19:23:31 -0000	1.38.10.2
--- ConfigParser.py	26 Sep 2002 19:37:36 -0000	1.38.10.3
***************
*** 84,88 ****
  """
  
- import string, types
  import re
  
--- 84,87 ----
***************
*** 90,94 ****
             "InterpolationError","InterpolationDepthError","ParsingError",
             "MissingSectionHeaderError","ConfigParser",
!            "MAX_INTERPOLATION_DEPTH"]
  
  DEFAULTSECT = "DEFAULT"
--- 89,93 ----
             "InterpolationError","InterpolationDepthError","ParsingError",
             "MissingSectionHeaderError","ConfigParser",
!            "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
  
  DEFAULTSECT = "DEFAULT"
***************
*** 192,196 ****
          already exists.
          """
!         if self.__sections.has_key(section):
              raise DuplicateSectionError(section)
          self.__sections[section] = {}
--- 191,195 ----
          already exists.
          """
!         if section in self.__sections:
              raise DuplicateSectionError(section)
          self.__sections[section] = {}
***************
*** 201,205 ****
          The DEFAULT section is not acknowledged.
          """
!         return section in self.sections()
  
      def options(self, section):
--- 200,204 ----
          The DEFAULT section is not acknowledged.
          """
!         return section in self.__sections
  
      def options(self, section):
***************
*** 210,214 ****
              raise NoSectionError(section)
          opts.update(self.__defaults)
!         if opts.has_key('__name__'):
              del opts['__name__']
          return opts.keys()
--- 209,213 ----
              raise NoSectionError(section)
          opts.update(self.__defaults)
!         if '__name__' in opts:
              del opts['__name__']
          return opts.keys()
***************
*** 224,228 ****
          filename may also be given.
          """
!         if type(filenames) in types.StringTypes:
              filenames = [filenames]
          for filename in filenames:
--- 223,227 ----
          filename may also be given.
          """
!         if isinstance(filenames, basestring):
              filenames = [filenames]
          for filename in filenames:
***************
*** 261,298 ****
          The section DEFAULT is special.
          """
          try:
!             sectdict = self.__sections[section].copy()
          except KeyError:
!             if section == DEFAULTSECT:
!                 sectdict = {}
!             else:
                  raise NoSectionError(section)
-         d = self.__defaults.copy()
-         d.update(sectdict)
          # Update with the entry specific variables
!         if vars:
              d.update(vars)
          option = self.optionxform(option)
          try:
!             rawval = d[option]
          except KeyError:
              raise NoOptionError(option, section)
  
          if raw:
!             return rawval
  
          # do the string interpolation
!         value = rawval                  # Make it a pretty variable name
!         depth = 0
!         while depth < 10:               # Loop through this until it's done
!             depth = depth + 1
!             if value.find("%(") >= 0:
                  try:
!                     value = value % d
                  except KeyError, key:
                      raise InterpolationError(key, option, section, rawval)
              else:
                  break
!         if value.find("%(") >= 0:
              raise InterpolationDepthError(option, section, rawval)
          return value
--- 260,296 ----
          The section DEFAULT is special.
          """
+         d = self.__defaults.copy()
          try:
!             d.update(self.__sections[section])
          except KeyError:
!             if section != DEFAULTSECT:
                  raise NoSectionError(section)
          # Update with the entry specific variables
!         if vars is not None:
              d.update(vars)
          option = self.optionxform(option)
          try:
!             value = d[option]
          except KeyError:
              raise NoOptionError(option, section)
  
          if raw:
!             return value
!         return self._interpolate(section, option, value, d)
  
+     def _interpolate(self, section, option, rawval, vars):
          # do the string interpolation
!         value = rawval
!         depth = MAX_INTERPOLATION_DEPTH 
!         while depth:                    # Loop through this until it's done
!             depth -= 1
!             if value.find("%(") != -1:
                  try:
!                     value = value % vars
                  except KeyError, key:
                      raise InterpolationError(key, option, section, rawval)
              else:
                  break
!         if value.find("%(") != -1:
              raise InterpolationDepthError(option, section, rawval)
          return value
***************
*** 302,317 ****
  
      def getint(self, section, option):
!         return self.__get(section, string.atoi, option)
  
      def getfloat(self, section, option):
!         return self.__get(section, string.atof, option)
  
      def getboolean(self, section, option):
-         states = {'1': 1, 'yes': 1, 'true': 1, 'on': 1,
-                   '0': 0, 'no': 0, 'false': 0, 'off': 0}
          v = self.get(section, option)
!         if not states.has_key(v.lower()):
              raise ValueError, 'Not a boolean: %s' % v
!         return states[v.lower()]
  
      def optionxform(self, optionstr):
--- 300,316 ----
  
      def getint(self, section, option):
!         return self.__get(section, int, option)
  
      def getfloat(self, section, option):
!         return self.__get(section, float, option)
! 
!     _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
!                        '0': False, 'no': False, 'false': False, 'off': False}
  
      def getboolean(self, section, option):
          v = self.get(section, option)
!         if v.lower() not in self._boolean_states:
              raise ValueError, 'Not a boolean: %s' % v
!         return self._boolean_states[v.lower()]
  
      def optionxform(self, optionstr):
***************
*** 320,335 ****
      def has_option(self, section, option):
          """Check for the existence of a given option in a given section."""
!         if not section or section == "DEFAULT":
              option = self.optionxform(option)
!             return self.__defaults.has_key(option)
!         elif not self.has_section(section):
              return 0
          else:
              option = self.optionxform(option)
!             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:
--- 319,335 ----
      def has_option(self, section, option):
          """Check for the existence of a given option in a given section."""
!         if not section or section == DEFAULTSECT:
              option = self.optionxform(option)
!             return option in self.__defaults
!         elif section not in self.__sections:
              return 0
          else:
              option = self.optionxform(option)
!             return (option in self.__sections[section]
!                     or option in self.__defaults)
  
      def set(self, section, option, value):
          """Set an option."""
!         if not section or section == DEFAULTSECT:
              sectdict = self.__defaults
          else:
***************
*** 338,363 ****
              except KeyError:
                  raise NoSectionError(section)
!         option = self.optionxform(option)
!         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, value) in self.__defaults.items():
                  fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
              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, str(value).replace('\n', '\n\t')))
              fp.write("\n")
  
      def remove_option(self, section, option):
          """Remove an option."""
!         if not section or section == "DEFAULT":
              sectdict = self.__defaults
          else:
--- 338,361 ----
              except KeyError:
                  raise NoSectionError(section)
!         sectdict[self.optionxform(option)] = value
  
      def write(self, fp):
          """Write an .ini-format representation of the configuration state."""
          if self.__defaults:
!             fp.write("[%s]\n" % DEFAULTSECT)
              for (key, value) in self.__defaults.items():
                  fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
              fp.write("\n")
!         for section in self.__sections:
!             fp.write("[%s]\n" % section)
!             for (key, value) in self.__sections[section].items():
!                 if key != "__name__":
!                     fp.write("%s = %s\n" %
!                              (key, str(value).replace('\n', '\n\t')))
              fp.write("\n")
  
      def remove_option(self, section, option):
          """Remove an option."""
!         if not section or section == DEFAULTSECT:
              sectdict = self.__defaults
          else:
***************
*** 367,371 ****
                  raise NoSectionError(section)
          option = self.optionxform(option)
!         existed = sectdict.has_key(option)
          if existed:
              del sectdict[option]
--- 365,369 ----
                  raise NoSectionError(section)
          option = self.optionxform(option)
!         existed = option in sectdict
          if existed:
              del sectdict[option]
***************
*** 374,387 ****
      def remove_section(self, 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
!     # slight semantic change from the previous version, because of the use
!     # of \w, _ is allowed in section header names.
      SECTCRE = re.compile(
          r'\['                                 # [
--- 372,383 ----
      def remove_section(self, section):
          """Remove a file section."""
!         existed = section in self.__sections
!         if existed:
              del self.__sections[section]
!         return existed
  
      #
!     # Regular expressions for parsing section headers and options.
!     #
      SECTCRE = re.compile(
          r'\['                                 # [
***************
*** 390,395 ****
          )
      OPTCRE = re.compile(
!         r'(?P<option>[]\-[\w_.*,(){}]+)'      # a lot of stuff found by IvL
!         r'[ \t]*(?P<vi>[:=])[ \t]*'           # any number of space/tab,
                                                # followed by separator
                                                # (either : or =), followed
--- 386,391 ----
          )
      OPTCRE = re.compile(
!         r'(?P<option>[^:=\s]+)'               # very permissive!
!         r'\s*(?P<vi>[:=])\s*'                 # any number of space/tab,
                                                # followed by separator
                                                # (either : or =), followed
***************
*** 420,432 ****
              if line.strip() == '' or line[0] in '#;':
                  continue
!             if line.split()[0].lower() == 'rem' \
!                and line[0] in "rR":      # no leading whitespace
                  continue
              # continuation line?
!             if line[0] in ' \t' and cursect is not None and optname:
                  value = line.strip()
                  if value:
!                     k = self.optionxform(optname)
!                     cursect[k] = "%s\n%s" % (cursect[k], value)
              # a section header or option header?
              else:
--- 416,426 ----
              if line.strip() == '' or line[0] in '#;':
                  continue
!             if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":      # no leading whitespace
                  continue
              # continuation line?
!             if line[0].isspace() and cursect is not None and optname:
                  value = line.strip()
                  if value:
!                     cursect[optname] = "%s\n%s" % (cursect[optname], value)
              # a section header or option header?
              else:
***************
*** 435,439 ****
                  if mo:
                      sectname = mo.group('header')
!                     if self.__sections.has_key(sectname):
                          cursect = self.__sections[sectname]
                      elif sectname == DEFAULTSECT:
--- 429,433 ----
                  if mo:
                      sectname = mo.group('header')
!                     if sectname in self.__sections:
                          cursect = self.__sections[sectname]
                      elif sectname == DEFAULTSECT:
***************
*** 456,460 ****
                              # a spacing character
                              pos = optval.find(';')
!                             if pos and optval[pos-1] in string.whitespace:
                                  optval = optval[:pos]
                          optval = optval.strip()
--- 450,454 ----
                              # a spacing character
                              pos = optval.find(';')
!                             if pos != -1 and optval[pos-1].isspace():
                                  optval = optval[:pos]
                          optval = optval.strip()
***************
*** 462,466 ****
                          if optval == '""':
                              optval = ''
!                         cursect[self.optionxform(optname)] = optval
                      else:
                          # a non-fatal parsing error occurred.  set up the
--- 456,461 ----
                          if optval == '""':
                              optval = ''
!                         optname = self.optionxform(optname)
!                         cursect[optname] = optval
                      else:
                          # a non-fatal parsing error occurred.  set up the