(Finally!) Changes related to the ConfigParser/INI-file topics

discussed on c.l.py last January.  Specifically:
  - more characters allowed in section & option names
  - if '=' is used to separate the option & value, the value can be
    followed by a comment of the form '\s;'
This commit is contained in:
Fred Drake 2000-02-28 20:59:03 +00:00
parent 048e6103f2
commit c517b9b406
1 changed files with 11 additions and 5 deletions

View File

@ -292,12 +292,12 @@ class ConfigParser:
# of \w, _ is allowed in section header names. # of \w, _ is allowed in section header names.
SECTCRE = re.compile( SECTCRE = re.compile(
r'\[' # [ r'\[' # [
r'(?P<header>[-\w]+)' # `-', `_' or any alphanum r'(?P<header>[-\w_.*,(){}]+)' # `-', `_' or any alphanum
r'\]' # ] r'\]' # ]
) )
OPTCRE = re.compile( OPTCRE = re.compile(
r'(?P<option>[-.\w]+)' # - . _ alphanum r'(?P<option>[-\w_.*,(){}]+)' # - . _ alphanum
r'[ \t]*[:=][ \t]*' # any number of space/tab, r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
# followed by separator # followed by separator
# (either : or =), followed # (either : or =), followed
# by any # space/tab # by any # space/tab
@ -327,7 +327,7 @@ class ConfigParser:
if string.strip(line) == '' or line[0] in '#;': if string.strip(line) == '' or line[0] in '#;':
continue continue
if string.lower(string.split(line)[0]) == 'rem' \ if string.lower(string.split(line)[0]) == 'rem' \
and line[0] == "r": # no leading whitespace and line[0] in "rR": # no leading whitespace
continue continue
# continuation line? # continuation line?
if line[0] in ' \t' and cursect is not None and optname: if line[0] in ' \t' and cursect is not None and optname:
@ -356,8 +356,14 @@ class ConfigParser:
else: else:
mo = self.OPTCRE.match(line) mo = self.OPTCRE.match(line)
if mo: if mo:
optname, optval = mo.group('option', 'value') optname, vi, optval = mo.group('option', 'vi', 'value')
optname = string.lower(optname) optname = string.lower(optname)
if vi == '=' and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = string.find(optval, ';')
if pos and optval[pos-1] in string.whitespace:
optval = optval[:pos]
optval = string.strip(optval) optval = string.strip(optval)
# allow empty values # allow empty values
if optval == '""': if optval == '""':