diff options
author | Guido van Rossum <guido@python.org> | 2001-10-04 19:58:46 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-10-04 19:58:46 (GMT) |
commit | fb06f75c5ac24c720aaf8f221efff35543d878b6 (patch) | |
tree | 8a25dcc2d81a2b943c7fe20b99fcf1a36e1bdb29 /Lib/ConfigParser.py | |
parent | 266410355f7faa7c98b29a16b9f50c56a9224f4b (diff) | |
download | cpython-fb06f75c5ac24c720aaf8f221efff35543d878b6.zip cpython-fb06f75c5ac24c720aaf8f221efff35543d878b6.tar.gz cpython-fb06f75c5ac24c720aaf8f221efff35543d878b6.tar.bz2 |
Apply modified SF patch 467580: ConfigParser.getboolean(): FALSE, TRUE.
This patch allows ConfigParser.getboolean() to interpret TRUE,
FALSE, YES, NO, ON and OFF instead just '0' and '1'.
While just allowing '0' and '1' sounds more correct users often
demand to use more descriptive directives in configuration
files. Instead of forcing every programmer do brew his own
solution a system should include the batteries for this.
[My modification to the patch is a slight rewording of the docstring
and use of lowercase instead of uppercase templates. The code is
still case sensitive. GvR.]
Diffstat (limited to 'Lib/ConfigParser.py')
-rw-r--r-- | Lib/ConfigParser.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py index fec847c..788c659 100644 --- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -66,8 +66,9 @@ ConfigParser -- responsible for for parsing a list of like get(), but convert value to a float getboolean(section, options) - like get(), but convert value to a boolean (currently defined as 0 or - 1, only) + like get(), but convert value to a boolean (currently case + insensitively defined as 0, false, no, off for 0, and 1, true, + yes, on for 1). Returns 0 or 1. remove_section(section) remove the given file section and all its options @@ -306,11 +307,12 @@ class ConfigParser: return self.__get(section, string.atof, option) def getboolean(self, section, option): - v = self.get(section, option) - val = int(v) - if val not in (0, 1): + 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 val + return states[v.lower()] def optionxform(self, optionstr): return optionstr.lower() |