comparison configuration/configuration.py @ 115:56db0b2b90af

fix casting
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 04 Jul 2012 09:54:37 -0700
parents d1911d9b5b19
children 9d19ed8fd883
comparison
equal deleted inserted replaced
114:d1911d9b5b19 115:56db0b2b90af
311 # ensure options are of the right type (if specified) 311 # ensure options are of the right type (if specified)
312 for key, value in config.items(): 312 for key, value in config.items():
313 _type = self.option_dict[key].get('type') 313 _type = self.option_dict[key].get('type')
314 if _type is None and 'default' in self.option_dict[key]: 314 if _type is None and 'default' in self.option_dict[key]:
315 _type = type(self.option_dict[key]['default']) 315 _type = type(self.option_dict[key]['default'])
316 if _type is not None and not isinstance(value, _type): 316 if _type is not None:
317 tocast = True
317 try: 318 try:
318 config[key] = _type(value) 319 if isinstance(value, _type):
319 except BaseException, e: 320 tocast = False
320 raise TypeCastException("Could not coerce %s, %s, to type %s: %s" % (key, value, _type.__name__, e)) 321 except TypeError:
322 # type is a type-casting function, not a proper type
323 pass
324 if tocast:
325 try:
326 config[key] = _type(value)
327 except BaseException, e:
328 raise TypeCastException("Could not coerce %s, %s, to type %s: %s" % (key, value, _type.__name__, e))
321 329
322 return config 330 return config
323 331
324 def validate(self): 332 def validate(self):
325 """validate resultant configuration""" 333 """validate resultant configuration"""