@@ -277,6 +277,30 @@ def valid_ident(s):
277277 return True
278278
279279
280+ class ConvertingMixin (object ):
281+ """For ConvertingXXX's, this mixin class provides common functions"""
282+
283+ def convert_with_key (self , key , value , replace = True ):
284+ result = self .configurator .convert (value )
285+ #If the converted value is different, save for next time
286+ if value is not result :
287+ if replace :
288+ self [key ] = result
289+ if type (result ) in (ConvertingDict , ConvertingList ,
290+ ConvertingTuple ):
291+ result .parent = self
292+ result .key = key
293+ return result
294+
295+ def convert (self , value ):
296+ result = self .configurator .convert (value )
297+ if value is not result :
298+ if type (result ) in (ConvertingDict , ConvertingList ,
299+ ConvertingTuple ):
300+ result .parent = self
301+ return result
302+
303+
280304# The ConvertingXXX classes are wrappers around standard Python containers,
281305# and they serve to convert any suitable values in the container. The
282306# conversion converts base dicts, lists and tuples to their wrapped
@@ -286,77 +310,37 @@ def valid_ident(s):
286310# Each wrapper should have a configurator attribute holding the actual
287311# configurator to use for conversion.
288312
289- class ConvertingDict (dict ):
313+ class ConvertingDict (dict , ConvertingMixin ):
290314 """A converting dictionary wrapper."""
291315
292316 def __getitem__ (self , key ):
293317 value = dict .__getitem__ (self , key )
294- result = self .configurator .convert (value )
295- #If the converted value is different, save for next time
296- if value is not result :
297- self [key ] = result
298- if type (result ) in (ConvertingDict , ConvertingList ,
299- ConvertingTuple ):
300- result .parent = self
301- result .key = key
302- return result
318+ return self .convert_with_key (key , value )
303319
304320 def get (self , key , default = None ):
305321 value = dict .get (self , key , default )
306- result = self .configurator .convert (value )
307- #If the converted value is different, save for next time
308- if value is not result :
309- self [key ] = result
310- if type (result ) in (ConvertingDict , ConvertingList ,
311- ConvertingTuple ):
312- result .parent = self
313- result .key = key
314- return result
322+ return self .convert_with_key (key , value )
315323
316324 def pop (self , key , default = None ):
317325 value = dict .pop (self , key , default )
318- result = self .configurator .convert (value )
319- if value is not result :
320- if type (result ) in (ConvertingDict , ConvertingList ,
321- ConvertingTuple ):
322- result .parent = self
323- result .key = key
324- return result
326+ return self .convert_with_key (key , value , replace = False )
325327
326- class ConvertingList (list ):
328+ class ConvertingList (list , ConvertingMixin ):
327329 """A converting list wrapper."""
328330 def __getitem__ (self , key ):
329331 value = list .__getitem__ (self , key )
330- result = self .configurator .convert (value )
331- #If the converted value is different, save for next time
332- if value is not result :
333- self [key ] = result
334- if type (result ) in (ConvertingDict , ConvertingList ,
335- ConvertingTuple ):
336- result .parent = self
337- result .key = key
338- return result
332+ return self .convert_with_key (key , value )
339333
340334 def pop (self , idx = - 1 ):
341335 value = list .pop (self , idx )
342- result = self .configurator .convert (value )
343- if value is not result :
344- if type (result ) in (ConvertingDict , ConvertingList ,
345- ConvertingTuple ):
346- result .parent = self
347- return result
336+ return self .convert (value )
348337
349- class ConvertingTuple (tuple ):
338+ class ConvertingTuple (tuple , ConvertingMixin ):
350339 """A converting tuple wrapper."""
351340 def __getitem__ (self , key ):
352341 value = tuple .__getitem__ (self , key )
353- result = self .configurator .convert (value )
354- if value is not result :
355- if type (result ) in (ConvertingDict , ConvertingList ,
356- ConvertingTuple ):
357- result .parent = self
358- result .key = key
359- return result
342+ # Can't replace a tuple entry.
343+ return self .convert_with_key (key , value , replace = False )
360344
361345class BaseConfigurator (object ):
362346 """
0 commit comments