Patch suggested (and partially provided) by Lars Damerow: instead of

always lowercasing the option name, call a method optionxform() which
can be overridden.  Also make the regexps SECTRE and OPTRE non-private
variables so they can also be overridden.
This commit is contained in:
Guido van Rossum 1999-06-17 18:41:42 +00:00
parent e019789962
commit 9e480adf9b
1 changed files with 8 additions and 5 deletions

View File

@ -199,7 +199,7 @@ class ConfigParser:
# Update with the entry specific variables # Update with the entry specific variables
if vars: if vars:
d.update(vars) d.update(vars)
option = string.lower(option) option = self.optionxform(option)
try: try:
rawval = d[option] rawval = d[option]
except KeyError: except KeyError:
@ -236,16 +236,19 @@ class ConfigParser:
raise ValueError, 'Not a boolean: %s' % v raise ValueError, 'Not a boolean: %s' % v
return val return val
def optionxform(self, optionstr):
return string.lower(optionstr)
# #
# Regular expressions for parsing section headers and options. Note a # Regular expressions for parsing section headers and options. Note a
# slight semantic change from the previous version, because of the use # slight semantic change from the previous version, because of the use
# 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]*[:=][ \t]*' # any number of space/tab,
# followed by separator # followed by separator
@ -287,7 +290,7 @@ class ConfigParser:
# a section header or option header? # a section header or option header?
else: else:
# is it a section header? # is it a section header?
mo = self.__SECTCRE.match(line) mo = self.SECTCRE.match(line)
if mo: if mo:
sectname = mo.group('header') sectname = mo.group('header')
if self.__sections.has_key(sectname): if self.__sections.has_key(sectname):
@ -304,7 +307,7 @@ class ConfigParser:
raise MissingSectionHeaderError(fp.name, lineno, `line`) raise MissingSectionHeaderError(fp.name, lineno, `line`)
# an option line? # an option line?
else: else:
mo = self.__OPTCRE.match(line) mo = self.OPTCRE.match(line)
if mo: if mo:
optname, optval = mo.group('option', 'value') optname, optval = mo.group('option', 'value')
optname = string.lower(optname) optname = string.lower(optname)