ConfigParser and .ini files

Fred L. Drake, Jr. fdrake at acm.org
Fri Jan 21 12:42:05 EST 2000


super_banane at yahoo.com writes:
 > The ConfigParser class in the default Python libraries
 > does not support comments given on the value lines, separated
 > by a semicolon, such as:
 > 
 > [Section]
 > keyword = value ; put your comments here

  You're right, it doesn't.
  Does anyone know of a *real* specification for the .ini format?  I
didn't know this was allowed.

 > Adding one line of code in the ConfigParser class
 > adds support for this feature. Any chance of submitting
 > this for the next Python release?
 > 
 > file ConfigParser.py line 311, add:
 > optval = string.split(optval)[0]
 > (before optname=string.strip(optval))

  This doesn't do it either.  If I have:

[foo]
bar=baz bat

your patch would give me 'baz', not 'baz bat', so this seems broken.
  In general, I'm somewhat concerned that the syntax for ConfigParser
is overloaded; it tries to handle two different formats: one contains
RFC822 headers inside sections, and the other is the .ini format.
Both are subject to some pretty funky value interpolation.
These should really be separated, because what most people want is a
conventional .ini file parser.
  Anyway, I've attached a patch that allows ConfigParser to strip out
';' comments from .ini-style entries (using '=' instead of ':' as the
value separator); RFC822-style entries should *not* be affected.  I
think this is closer to what you're looking for.
  I can check this in if this is the right approach.  I'm still not
happy with the overloaded syntax, but this might be closer to getting
the syntaxes properly implemented.  People should tell me if this is
really the right thing to do, and if it breaks anything.


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives


Index: ConfigParser.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/ConfigParser.py,v
retrieving revision 1.14
diff -u -c -r1.14 ConfigParser.py
*** ConfigParser.py	1999/10/12 16:12:48	1.14
--- ConfigParser.py	2000/01/21 17:29:54
***************
*** 297,303 ****
          )
      OPTCRE = re.compile(
          r'(?P<option>[-.\w]+)'                # - . _ alphanum
!         r'[ \t]*[:=][ \t]*'                   # any number of space/tab,
                                                # followed by separator
                                                # (either : or =), followed
                                                # by any # space/tab
--- 297,303 ----
          )
      OPTCRE = re.compile(
          r'(?P<option>[-.\w]+)'                # - . _ alphanum
!         r'[ \t]*(?P<vi>[:=])[ \t]*'           # any number of space/tab,
                                                # followed by separator
                                                # (either : or =), followed
                                                # by any # space/tab
***************
*** 327,333 ****
              if string.strip(line) == '' or line[0] in '#;':
                  continue
              if string.lower(string.split(line)[0]) == 'rem' \
!                and line[0] == "r":      # no leading whitespace
                  continue
              # continuation line?
              if line[0] in ' \t' and cursect is not None and optname:
--- 327,333 ----
              if string.strip(line) == '' or line[0] in '#;':
                  continue
              if string.lower(string.split(line)[0]) == 'rem' \
!                and line[0] in "rR":      # no leading whitespace
                  continue
              # continuation line?
              if line[0] in ' \t' and cursect is not None and optname:
***************
*** 356,363 ****
                  else:
                      mo = self.OPTCRE.match(line)
                      if mo:
!                         optname, optval = mo.group('option', 'value')
                          optname = string.lower(optname)
                          optval = string.strip(optval)
                          # allow empty values
                          if optval == '""':
--- 356,365 ----
                  else:
                      mo = self.OPTCRE.match(line)
                      if mo:
!                         optname, vi, optval = mo.group('option', 'vi', 'value')
                          optname = string.lower(optname)
+                         if vi == '=' and ';' in optval:
+                             optval = optval[:string.find(optval, ';')]
                          optval = string.strip(optval)
                          # allow empty values
                          if optval == '""':




More information about the Python-list mailing list