2424
2525 methods:
2626
27- __init__(defaults=None) -- create the parser and specify a
28- dictionary of intrinsic defaults. The
29- keys must be strings, the values must
30- be appropriate for %()s string
31- interpolation. Note that `__name__' is
32- always an intrinsic default; it's value
33- is the section's name.
27+ __init__(defaults=None)
28+ create the parser and specify a dictionary of intrinsic defaults. The
29+ keys must be strings, the values must be appropriate for %()s string
30+ interpolation. Note that `__name__' is always an intrinsic default;
31+ it's value is the section's name.
3432
35- sections() -- return all the configuration section names, sans DEFAULT
33+ sections()
34+ return all the configuration section names, sans DEFAULT
3635
37- options(section) -- return list of configuration options for the named
38- section
36+ options(section)
37+ return list of configuration options for the named section
3938
40- read(*filenames) -- read and parse the list of named configuration files
39+ read(filenames)
40+ read and parse the list of named configuration files
4141
42- get(section, option, raw=0) -- return a string value for the named
43- option. All % interpolations are
44- expanded in the return values, based on
45- the defaults passed into the constructor
46- and the DEFAULT section.
42+ get(section, option, raw=0, vars=None)
43+ return a string value for the named option. All % interpolations are
44+ expanded in the return values, based on the defaults passed into the
45+ constructor and the DEFAULT section. Additional substitutions may be
46+ provided using the `vars' argument, which must be a dictionary whose
47+ contents override any pre-existing defaults.
4748
48- getint(section, options) -- like get(), but convert value to an integer
49+ getint(section, options)
50+ like get(), but convert value to an integer
4951
50- getfloat(section, options) -- like get(), but convert value to a float
52+ getfloat(section, options)
53+ like get(), but convert value to a float
5154
52- getboolean(section, options) -- like get(), but convert value to
53- a boolean (currently defined as 0
54- or 1, only)
55+ getboolean(section, options)
56+ like get(), but convert value to a boolean (currently defined as 0 or
57+ 1, only)
5558"""
5659
5760import sys
@@ -173,12 +176,14 @@ def read(self, filenames):
173176 except IOError :
174177 pass
175178
176- def get (self , section , option , raw = 0 ):
179+ def get (self , section , option , raw = 0 , vars = None ):
177180 """Get an option value for a given section.
178181
179- All % interpolations are expanded in the return values, based
180- on the defaults passed into the constructor, unless the optional
181- argument `raw' is true.
182+ All % interpolations are expanded in the return values, based on the
183+ defaults passed into the constructor, unless the optional argument
184+ `raw' is true. Additional substitutions may be provided using the
185+ `vars' argument, which must be a dictionary whose contents overrides
186+ any pre-existing defaults.
182187
183188 The section DEFAULT is special.
184189 """
@@ -191,6 +196,9 @@ def get(self, section, option, raw=0):
191196 raise NoSectionError (section )
192197 d = self .__defaults .copy ()
193198 d .update (sectdict )
199+ # Update with the entry specific variables
200+ if vars :
201+ d .update (vars )
194202 option = string .lower (option )
195203 try :
196204 rawval = d [option ]
@@ -199,11 +207,17 @@ def get(self, section, option, raw=0):
199207 # do the string interpolation
200208 if raw :
201209 return rawval
202- try :
203- return rawval % d
204- except KeyError , key :
205- raise InterpolationError (key , option , section , rawval )
206210
211+ value = rawval # Make it a pretty variable name
212+ while 1 : # Loop through this until it's done
213+ if not string .find (value , "%(" ):
214+ try :
215+ value = value % d
216+ except KeyError , key :
217+ raise InterpolationError (key , option , section , rawval )
218+ else :
219+ return value
220+
207221 def __get (self , section , conv , option ):
208222 return conv (self .get (section , option ))
209223
0 commit comments