summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-01-26 19:29:25 (GMT)
committerGuido van Rossum <guido@python.org>1999-01-26 19:29:25 (GMT)
commite6506e753bd3c1fc5c32271d30db46fb41f8486f (patch)
tree49dd456491f6b737e25b360ec38d41be87e04a94 /Lib
parent9f253dc3ffb09c7a3163503453eddab060acec26 (diff)
downloadcpython-e6506e753bd3c1fc5c32271d30db46fb41f8486f.zip
cpython-e6506e753bd3c1fc5c32271d30db46fb41f8486f.tar.gz
cpython-e6506e753bd3c1fc5c32271d30db46fb41f8486f.tar.bz2
Patch by Chris Petrilli (not really tested since I don't know this
module myself) to accept an option keyword argument (vars) that is substituted on top of the defaults that were setup in __init__. The patch also fixes the problem where you can't have recusive references inside your configuration file.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ConfigParser.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py
index 89e2d85..6d8f956 100644
--- a/Lib/ConfigParser.py
+++ b/Lib/ConfigParser.py
@@ -173,12 +173,14 @@ class ConfigParser:
except IOError:
pass
- def get(self, section, option, raw=0):
+ def get(self, section, option, raw=0, vars=None):
"""Get an option value for a given section.
All % interpolations are expanded in the return values, based
on the defaults passed into the constructor, unless the optional
- argument `raw' is true.
+ argument `raw' is true. Additional substitutions may be provided
+ using the vars keyword argument, which override any pre-existing
+ defaults.
The section DEFAULT is special.
"""
@@ -191,6 +193,9 @@ class ConfigParser:
raise NoSectionError(section)
d = self.__defaults.copy()
d.update(sectdict)
+ # Update with the entry specific variables
+ if vars:
+ d.update(vars)
option = string.lower(option)
try:
rawval = d[option]
@@ -199,11 +204,17 @@ class ConfigParser:
# do the string interpolation
if raw:
return rawval
- try:
- return rawval % d
- except KeyError, key:
- raise InterpolationError(key, option, section, rawval)
+ value = rawval # Make it a pretty variable name
+ while 1: # Loop through this until it's done
+ if not string.find(value, "%("):
+ try:
+ value = value % d
+ except KeyError, key:
+ raise InterpolationError(key, option, section, rawval)
+ else:
+ return value
+
def __get(self, section, conv, option):
return conv(self.get(section, option))