equal
deleted
inserted
replaced
537 if b is None: |
537 if b is None: |
538 raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
538 raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
539 % (section, name, v)) |
539 % (section, name, v)) |
540 return b |
540 return b |
541 |
541 |
542 def configwith(self, convert, section, name, default=None, |
542 def configwith(self, convert, section, name, default=_unset, |
543 desc=None, untrusted=False): |
543 desc=None, untrusted=False): |
544 """parse a configuration element with a conversion function |
544 """parse a configuration element with a conversion function |
545 |
545 |
546 >>> u = ui(); s = 'foo' |
546 >>> u = ui(); s = 'foo' |
547 >>> u.setconfig(s, 'float1', '42') |
547 >>> u.setconfig(s, 'float1', '42') |
549 42.0 |
549 42.0 |
550 >>> u.setconfig(s, 'float2', '-4.25') |
550 >>> u.setconfig(s, 'float2', '-4.25') |
551 >>> u.configwith(float, s, 'float2') |
551 >>> u.configwith(float, s, 'float2') |
552 -4.25 |
552 -4.25 |
553 >>> u.configwith(float, s, 'unknown', 7) |
553 >>> u.configwith(float, s, 'unknown', 7) |
554 7 |
554 7.0 |
555 >>> u.setconfig(s, 'invalid', 'somevalue') |
555 >>> u.setconfig(s, 'invalid', 'somevalue') |
556 >>> u.configwith(float, s, 'invalid') |
556 >>> u.configwith(float, s, 'invalid') |
557 Traceback (most recent call last): |
557 Traceback (most recent call last): |
558 ... |
558 ... |
559 ConfigError: foo.invalid is not a valid float ('somevalue') |
559 ConfigError: foo.invalid is not a valid float ('somevalue') |
561 Traceback (most recent call last): |
561 Traceback (most recent call last): |
562 ... |
562 ... |
563 ConfigError: foo.invalid is not a valid womble ('somevalue') |
563 ConfigError: foo.invalid is not a valid womble ('somevalue') |
564 """ |
564 """ |
565 |
565 |
566 v = self.config(section, name, None, untrusted) |
566 v = self.config(section, name, default, untrusted) |
567 if v is None: |
567 if v is None: |
568 return default |
568 return v # do not attempt to convert None |
569 try: |
569 try: |
570 return convert(v) |
570 return convert(v) |
571 except (ValueError, error.ParseError): |
571 except (ValueError, error.ParseError): |
572 if desc is None: |
572 if desc is None: |
573 desc = convert.__name__ |
573 desc = convert.__name__ |