Mercurial > public > mercurial-scm > hg
comparison mercurial/ui.py @ 30926:120682fce099
ui: add a configwith method
This is a long-overdue generalization of the pattern in configint
and configbool.
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Sun, 12 Feb 2017 21:40:46 -0800 |
parents | 82f1ef8b4477 |
children | 8fa3ab6221b9 |
comparison
equal
deleted
inserted
replaced
30925:82f1ef8b4477 | 30926:120682fce099 |
---|---|
399 b = util.parsebool(v) | 399 b = util.parsebool(v) |
400 if b is None: | 400 if b is None: |
401 raise error.ConfigError(_("%s.%s is not a boolean ('%s')") | 401 raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
402 % (section, name, v)) | 402 % (section, name, v)) |
403 return b | 403 return b |
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)) | |
404 | 439 |
405 def configint(self, section, name, default=None, untrusted=False): | 440 def configint(self, section, name, default=None, untrusted=False): |
406 """parse a configuration element as an integer | 441 """parse a configuration element as an integer |
407 | 442 |
408 >>> u = ui(); s = 'foo' | 443 >>> u = ui(); s = 'foo' |