Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6506e7

Browse files
committed
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.
1 parent 9f253dc commit e6506e7

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Lib/ConfigParser.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,14 @@ def read(self, filenames):
173173
except IOError:
174174
pass
175175

176-
def get(self, section, option, raw=0):
176+
def get(self, section, option, raw=0, vars=None):
177177
"""Get an option value for a given section.
178178
179179
All % interpolations are expanded in the return values, based
180180
on the defaults passed into the constructor, unless the optional
181-
argument `raw' is true.
181+
argument `raw' is true. Additional substitutions may be provided
182+
using the vars keyword argument, which override any pre-existing
183+
defaults.
182184
183185
The section DEFAULT is special.
184186
"""
@@ -191,6 +193,9 @@ def get(self, section, option, raw=0):
191193
raise NoSectionError(section)
192194
d = self.__defaults.copy()
193195
d.update(sectdict)
196+
# Update with the entry specific variables
197+
if vars:
198+
d.update(vars)
194199
option = string.lower(option)
195200
try:
196201
rawval = d[option]
@@ -199,11 +204,17 @@ def get(self, section, option, raw=0):
199204
# do the string interpolation
200205
if raw:
201206
return rawval
202-
try:
203-
return rawval % d
204-
except KeyError, key:
205-
raise InterpolationError(key, option, section, rawval)
206207

208+
value = rawval # Make it a pretty variable name
209+
while 1: # Loop through this until it's done
210+
if not string.find(value, "%("):
211+
try:
212+
value = value % d
213+
except KeyError, key:
214+
raise InterpolationError(key, option, section, rawval)
215+
else:
216+
return value
217+
207218
def __get(self, section, conv, option):
208219
return conv(self.get(section, option))
209220

0 commit comments

Comments
 (0)