##// END OF EJS Templates
ui: add a configwith method...
Bryan O'Sullivan -
r30926:120682fc default
parent child Browse files
Show More
@@ -402,6 +402,41 b' class ui(object):'
402 402 % (section, name, v))
403 403 return b
404 404
405 def configwith(self, convert, section, name, default=None,
406 desc=None, untrusted=False):
407 """parse a configuration element with a conversion function
408
409 >>> u = ui(); s = 'foo'
410 >>> u.setconfig(s, 'float1', '42')
411 >>> u.configwith(float, s, 'float1')
412 42.0
413 >>> u.setconfig(s, 'float2', '-4.2')
414 >>> u.configwith(float, s, 'float2')
415 -4.2
416 >>> u.configwith(float, s, 'unknown', 7)
417 7
418 >>> u.setconfig(s, 'invalid', 'somevalue')
419 >>> u.configwith(float, s, 'invalid')
420 Traceback (most recent call last):
421 ...
422 ConfigError: foo.invalid is not a valid float ('somevalue')
423 >>> u.configwith(float, s, 'invalid', desc='womble')
424 Traceback (most recent call last):
425 ...
426 ConfigError: foo.invalid is not a valid womble ('somevalue')
427 """
428
429 v = self.config(section, name, None, untrusted)
430 if v is None:
431 return default
432 try:
433 return convert(v)
434 except ValueError:
435 if desc is None:
436 desc = convert.__name__
437 raise error.ConfigError(_("%s.%s is not a valid %s ('%s')")
438 % (section, name, desc, v))
439
405 440 def configint(self, section, name, default=None, untrusted=False):
406 441 """parse a configuration element as an integer
407 442
General Comments 0
You need to be logged in to leave comments. Login now