# HG changeset patch # User Augie Fackler # Date 1283050235 18000 # Node ID a88a4720c2f08ee5a173b901f97afa3e994dedca # Parent dba2db7a7c28fdfe054fd689ba790530da521379 parsebool: create new function and use it for config parsing diff -r dba2db7a7c28 -r a88a4720c2f0 mercurial/ui.py --- a/mercurial/ui.py Sat Aug 28 21:49:53 2010 -0500 +++ b/mercurial/ui.py Sat Aug 28 21:50:35 2010 -0500 @@ -9,9 +9,6 @@ import errno, getpass, os, socket, sys, tempfile, traceback import config, util, error -_booleans = {'1': True, 'yes': True, 'true': True, 'on': True, - '0': False, 'no': False, 'false': False, 'off': False} - class ui(object): def __init__(self, src=None): self._buffers = [] @@ -149,10 +146,11 @@ return default if isinstance(v, bool): return v - if v.lower() not in _booleans: + b = util.parsebool(v) + if b is None: raise error.ConfigError(_("%s.%s not a boolean ('%s')") % (section, name, v)) - return _booleans[v.lower()] + return b def configlist(self, section, name, default=None, untrusted=False): """Return a list of comma/space separated strings""" diff -r dba2db7a7c28 -r a88a4720c2f0 mercurial/util.py --- a/mercurial/util.py Sat Aug 28 21:49:53 2010 -0500 +++ b/mercurial/util.py Sat Aug 28 21:50:35 2010 -0500 @@ -1435,3 +1435,13 @@ return socket.getservbyname(port) except socket.error: raise Abort(_("no port number associated with service '%s'") % port) + +_booleans = {'1': True, 'yes': True, 'true': True, 'on': True, + '0': False, 'no': False, 'false': False, 'off': False} + +def parsebool(s): + """Parse s into a boolean. + + If s is not a valid boolean, returns None. + """ + return _booleans.get(s.lower(), None)