Rev 6065: (vila) Implement integer config options. (Vincent Ladeuil) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri Aug 12 10:54:58 UTC 2011


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6065 [merge]
revision-id: pqm at pqm.ubuntu.com-20110812105451-s1i361x4d409godc
parent: pqm at pqm.ubuntu.com-20110812075738-z6kjnvy20806946j
parent: v.ladeuil+lp at free.fr-20110810143840-ipsnql1dxu1yghwq
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-08-12 10:54:51 +0000
message:
  (vila) Implement integer config options. (Vincent Ladeuil)
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
  doc/en/release-notes/bzr-2.5.txt bzr2.5.txt-20110708125756-587p0hpw7oke4h05-1
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-08-12 07:57:38 +0000
+++ b/bzrlib/config.py	2011-08-12 10:54:51 +0000
@@ -2293,10 +2293,10 @@
 
         :param invalid: the action to be taken when an invalid value is
             encountered in a store. This is called only when from_unicode is
-            invoked to convert a string and returns None or raise
-            ValueError. Accepted values are: None (ignore invalid values),
-            'warning' (emit a warning), 'error' emit an error message and
-            terminates.
+            invoked to convert a string and returns None or raise ValueError or
+            TypeError. Accepted values are: None (ignore invalid values),
+            'warning' (emit a warning), 'error' (emit an error message and
+            terminates).
         """
         self.name = name
         self.default = default
@@ -2315,6 +2315,10 @@
     return ui.bool_from_string(unicode_str)
 
 
+def int_from_store(unicode_str):
+    return int(unicode_str)
+
+
 class OptionRegistry(registry.Registry):
     """Register config options by their name.
 
@@ -2855,7 +2859,7 @@
             # If a value exists and the option provides a converter, use it
             try:
                 converted = opt.from_unicode(value)
-            except ValueError:
+            except (ValueError, TypeError):
                 # Invalid values are ignored
                 converted = None
             if converted is None and opt.invalid is not None:

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-08-10 13:26:27 +0000
+++ b/bzrlib/tests/test_config.py	2011-08-10 14:38:40 +0000
@@ -2983,11 +2983,16 @@
         self.conf.store._load_from_string('foo=yes')
         self.assertEquals(True, self.conf.get('foo'))
 
-    def test_get_with_bool_converter_invalid(self):
+    def test_get_with_bool_converter_invalid_string(self):
         self.register_bool_option('foo', False)
         self.conf.store._load_from_string('foo=not-a-boolean')
         self.assertEquals(False, self.conf.get('foo'))
 
+    def test_get_with_bool_converter_invalid_list(self):
+        self.register_bool_option('foo', False)
+        self.conf.store._load_from_string('foo=not,a,boolean')
+        self.assertEquals(False, self.conf.get('foo'))
+
     def test_get_invalid_warns(self):
         self.register_bool_option('foo', False, invalid='warning')
         self.conf.store._load_from_string('foo=not-a-boolean')
@@ -3005,6 +3010,34 @@
         self.conf.store._load_from_string('foo=not-a-boolean')
         self.assertRaises(errors.ConfigOptionValueError, self.conf.get, 'foo')
 
+    def register_integer_option(self, name, default):
+        i = config.Option(name, default=default, help='A boolean.',
+                          from_unicode=config.int_from_store)
+        self.registry.register(i)
+
+    def test_get_with_integer_not_defined_returns_default(self):
+        self.register_integer_option('foo', 42)
+        self.assertEquals(42, self.conf.get('foo'))
+
+    def test_get_with_integer_converter_not_default(self):
+        self.register_integer_option('foo', 42)
+        self.conf.store._load_from_string('foo=16')
+        self.assertEquals(16, self.conf.get('foo'))
+
+    def test_get_with_integer_converter_invalid_string(self):
+        # We don't set a default value
+        self.register_integer_option('foo', None)
+        self.conf.store._load_from_string('foo=forty-two')
+        # No default value, so we should get None
+        self.assertEquals(None, self.conf.get('foo'))
+
+    def test_get_with_integer_converter_invalid_list(self):
+        # We don't set a default value
+        self.register_integer_option('foo', None)
+        self.conf.store._load_from_string('foo=a,list')
+        # No default value, so we should get None
+        self.assertEquals(None, self.conf.get('foo'))
+
 
 class TestStackSet(TestStackWithTransport):
 

=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt	2011-08-12 07:57:38 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt	2011-08-12 10:54:51 +0000
@@ -20,6 +20,11 @@
 
 .. New commands, options, etc that users may wish to try out.
 
+* A ``from_unicode`` parameter can be specified when registering a config
+  option. This implements boolean and integer config options when the
+  provided ``bool_from_store`` and ``int_from_store`` are used.
+  (Vincent Ladeuil)
+
 * Accessing a packaging branch on Launchpad (eg, ``lp:ubuntu/bzr``) now
   checks to see if the most recent published source package version for
   that project is present in the branch tags. This should help developers




More information about the bazaar-commits mailing list