merge with crew
authorMatt Mackall <mpm@selenic.com>
Tue, 03 May 2011 22:04:11 -0500
changeset 14180 1123bbba278d
parent 14179 64481eee6215 (current diff)
parent 14171 fa2b596db182 (diff)
child 14181 bf951d58b917
merge with crew
--- a/mercurial/ui.py	Tue May 03 21:53:13 2011 -0500
+++ b/mercurial/ui.py	Tue May 03 22:04:11 2011 -0500
@@ -164,6 +164,26 @@
         return v
 
     def configbool(self, section, name, default=False, untrusted=False):
+        """parse a configuration element as a boolean
+
+        >>> u = ui(); s = 'foo'
+        >>> u.setconfig(s, 'true', 'yes')
+        >>> u.configbool(s, 'true')
+        True
+        >>> u.setconfig(s, 'false', 'no')
+        >>> u.configbool(s, 'false')
+        False
+        >>> u.configbool(s, 'unknown')
+        False
+        >>> u.configbool(s, 'unknown', True)
+        True
+        >>> u.setconfig(s, 'invalid', 'somevalue')
+        >>> u.configbool(s, 'invalid')
+        Traceback (most recent call last):
+            ...
+        ConfigError: foo.invalid is not a boolean ('somevalue')
+        """
+
         v = self.config(section, name, None, untrusted)
         if v is None:
             return default
@@ -171,12 +191,47 @@
             return v
         b = util.parsebool(v)
         if b is None:
-            raise error.ConfigError(_("%s.%s not a boolean ('%s')")
+            raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
                                     % (section, name, v))
         return b
 
+    def configint(self, section, name, default=None, untrusted=False):
+        """parse a configuration element as an integer
+
+        >>> u = ui(); s = 'foo'
+        >>> u.setconfig(s, 'int1', '42')
+        >>> u.configint(s, 'int1')
+        42
+        >>> u.setconfig(s, 'int2', '-42')
+        >>> u.configint(s, 'int2')
+        -42
+        >>> u.configint(s, 'unknown', 7)
+        7
+        >>> u.setconfig(s, 'invalid', 'somevalue')
+        >>> u.configint(s, 'invalid')
+        Traceback (most recent call last):
+            ...
+        ConfigError: foo.invalid is not an integer ('somevalue')
+        """
+
+        v = self.config(section, name, None, untrusted)
+        if v is None:
+            return default
+        try:
+            return int(v)
+        except ValueError:
+            raise error.ConfigError(_("%s.%s is not an integer ('%s')")
+                                    % (section, name, v))
+
     def configlist(self, section, name, default=None, untrusted=False):
-        """Return a list of comma/space separated strings"""
+        """parse a configuration element as a list of comma/space separated
+        strings
+
+        >>> u = ui(); s = 'foo'
+        >>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
+        >>> u.configlist(s, 'list1')
+        ['this', 'is', 'a small', 'test']
+        """
 
         def _parse_plain(parts, s, offset):
             whitespace = False
--- a/tests/test-doctest.py	Tue May 03 21:53:13 2011 -0500
+++ b/tests/test-doctest.py	Tue May 03 22:04:11 2011 -0500
@@ -16,6 +16,9 @@
 import mercurial.store
 doctest.testmod(mercurial.store)
 
+import mercurial.ui
+doctest.testmod(mercurial.ui)
+
 import mercurial.url
 doctest.testmod(mercurial.url)
 
--- a/tests/test-ui-config.py	Tue May 03 21:53:13 2011 -0500
+++ b/tests/test-ui-config.py	Tue May 03 22:04:11 2011 -0500
@@ -5,6 +5,10 @@
     'values.string=string value',
     'values.bool1=true',
     'values.bool2=false',
+    'values.boolinvalid=foo',
+    'values.int1=42',
+    'values.int2=-42',
+    'values.intinvalid=foo',
     'lists.list1=foo',
     'lists.list2=foo bar baz',
     'lists.list3=alice, bob',
@@ -23,7 +27,7 @@
     'lists.list16="longer quotation" with "no ending quotation',
     'lists.list17=this is \\" "not a quotation mark"',
     'lists.list18=\n \n\nding\ndong',
-])
+    ])
 
 print repr(testui.configitems('values'))
 print repr(testui.configitems('lists'))
@@ -43,6 +47,9 @@
 print repr(testui.configbool('values', 'unknown'))
 print repr(testui.configbool('values', 'unknown', True))
 print "---"
+print repr(testui.configint('values', 'int1'))
+print repr(testui.configint('values', 'int2'))
+print "---"
 print repr(testui.configlist('lists', 'list1'))
 print repr(testui.configlist('lists', 'list2'))
 print repr(testui.configlist('lists', 'list3'))
@@ -79,3 +86,13 @@
 # values that aren't strings should work
 testui.setconfig('hook', 'commit', function)
 print function == testui.config('hook', 'commit')
+
+# invalid values
+try:
+    testui.configbool('values', 'boolinvalid')
+except error.ConfigError:
+    print 'boolinvalid'
+try:
+    testui.configint('values', 'intinvalid')
+except error.ConfigError:
+    print 'intinvalid'
--- a/tests/test-ui-config.py.out	Tue May 03 21:53:13 2011 -0500
+++ b/tests/test-ui-config.py.out	Tue May 03 22:04:11 2011 -0500
@@ -1,4 +1,4 @@
-[('string', 'string value'), ('bool1', 'true'), ('bool2', 'false')]
+[('string', 'string value'), ('bool1', 'true'), ('bool2', 'false'), ('boolinvalid', 'foo'), ('int1', '42'), ('int2', '-42'), ('intinvalid', 'foo')]
 [('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,,     '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"'), ('list18', '\n \n\nding\ndong')]
 ---
 'string value'
@@ -6,13 +6,16 @@
 'false'
 None
 ---
-values.string not a boolean ('string value')
+values.string is not a boolean ('string value')
 True
 False
 False
 False
 True
 ---
+42
+-42
+---
 ['foo']
 ['foo', 'bar', 'baz']
 ['alice', 'bob']
@@ -42,3 +45,5 @@
 ['foo', 'bar']
 None
 True
+boolinvalid
+intinvalid