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

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


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

------------------------------------------------------------
revno: 6063 [merge]
revision-id: pqm at pqm.ubuntu.com-20110812045846-15hlipqj7f69wn7f
parent: pqm at pqm.ubuntu.com-20110812014255-y3thbw6gdn7cw6uz
parent: v.ladeuil+lp at free.fr-20110809140131-03lysccptsnr2lih
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-08-12 04:58:46 +0000
message:
  (vila) Implement boolean options. (Vincent Ladeuil)
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
  doc/developers/configuration.txt configuration.txt-20110408142435-korjxxnskvq44sta-1
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-08-09 17:04:46 +0000
+++ b/bzrlib/config.py	2011-08-12 04:58:46 +0000
@@ -2275,14 +2275,20 @@
     value, in which config files it can be stored, etc (TBC).
     """
 
-    def __init__(self, name, default=None, help=None):
+    def __init__(self, name, default=None, help=None, from_unicode=None):
         self.name = name
         self.default = default
         self.help = help
+        self.from_unicode = from_unicode
 
     def get_default(self):
         return self.default
 
+# Predefined converters to get proper values from store
+
+def bool_from_store(unicode_str):
+    return ui.bool_from_string(unicode_str)
+
 
 class OptionRegistry(registry.Registry):
     """Register config options by their name.
@@ -2329,7 +2335,7 @@
 # Registered options in lexicographical order
 
 option_registry.register(
-    Option('dirstate.fdatasync', default=True,
+    Option('dirstate.fdatasync', default=True, from_unicode=bool_from_store,
            help='''
 Flush dirstate changes onto physical disk?
 
@@ -2351,7 +2357,7 @@
            help= 'Unicode encoding for output'
            ' (terminal encoding if not specified).'))
 option_registry.register(
-    Option('repository.fdatasync', default=True,
+    Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
            help='''\
 Flush repository changes onto physical disk?
 
@@ -2812,13 +2818,23 @@
                     break
             if value is not None:
                 break
+        # If the option is registered, it may provide additional info about
+        # value handling
+        try:
+            opt = option_registry.get(name)
+        except KeyError:
+            # Not registered
+            opt = None
+        if (opt is not None and opt.from_unicode is not None
+            and value is not None):
+            # If a value exists and the option provides a converter, use it
+            try:
+                value = opt.from_unicode(value)
+            except ValueError:
+                # Invalid values are ignored
+                value = None
         if value is None:
             # If the option is registered, it may provide a default value
-            try:
-                opt = option_registry.get(name)
-            except KeyError:
-                # Not registered
-                opt = None
             if opt is not None:
                 value = opt.get_default()
         for hook in ConfigHooks['get']:

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-08-09 10:02:34 +0000
+++ b/bzrlib/tests/test_config.py	2011-08-09 13:59:53 +0000
@@ -2937,22 +2937,55 @@
 
 class TestStackGet(TestStackWithTransport):
 
+    def setUp(self):
+        super(TestStackGet, self).setUp()
+        self.conf = self.get_stack(self)
+
     def test_get_for_empty_stack(self):
-        conf = self.get_stack(self)
-        self.assertEquals(None, conf.get('foo'))
+        self.assertEquals(None, self.conf.get('foo'))
 
     def test_get_hook(self):
-        conf = self.get_stack(self)
-        conf.store._load_from_string('foo=bar')
+        self.conf.store._load_from_string('foo=bar')
         calls = []
         def hook(*args):
             calls.append(args)
         config.ConfigHooks.install_named_hook('get', hook, None)
         self.assertLength(0, calls)
-        value = conf.get('foo')
+        value = self.conf.get('foo')
         self.assertEquals('bar', value)
         self.assertLength(1, calls)
-        self.assertEquals((conf, 'foo', 'bar'), calls[0])
+        self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
+
+
+class TestStackGetWithConverter(TestStackGet):
+
+    def setUp(self):
+        super(TestStackGetWithConverter, self).setUp()
+        self.overrideAttr(config, 'option_registry', config.OptionRegistry())
+        self.registry = config.option_registry
+
+    def register_bool_option(self, name, default):
+        b = config.Option(name, default=default, help='A boolean.',
+                          from_unicode=config.bool_from_store)
+        self.registry.register(b)
+
+    def test_get_with_bool_not_defined_default_true(self):
+        self.register_bool_option('foo', True)
+        self.assertEquals(True, self.conf.get('foo'))
+
+    def test_get_with_bool_not_defined_default_false(self):
+        self.register_bool_option('foo', False)
+        self.assertEquals(False, self.conf.get('foo'))
+
+    def test_get_with_bool_converter_not_default(self):
+        self.register_bool_option('foo', False)
+        self.conf.store._load_from_string('foo=yes')
+        self.assertEquals(True, self.conf.get('foo'))
+
+    def test_get_with_bool_converter_invalid(self):
+        self.register_bool_option('foo', False)
+        self.conf.store._load_from_string('foo=not-a-boolean')
+        self.assertEquals(False, self.conf.get('foo'))
 
 
 class TestStackSet(TestStackWithTransport):
@@ -3184,7 +3217,7 @@
         conf = config.AuthenticationConfig(_file=StringIO(
                 'foo = bar\xff'))
         self.assertRaises(errors.ConfigContentError, conf._get_config)
-        
+
     def test_missing_auth_section_header(self):
         conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
         self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')

=== modified file 'doc/developers/configuration.txt'
--- a/doc/developers/configuration.txt	2011-08-09 09:27:46 +0000
+++ b/doc/developers/configuration.txt	2011-08-09 13:51:55 +0000
@@ -23,6 +23,10 @@
   summary and can be followed by a blank line and a more detailed
   explanation.
 
+* from_unicode: a callable accepting a unicode string and returning a
+  suitable value for the option. If the string cannot be coerced it should
+  return None.
+
 Sections
 --------
 




More information about the bazaar-commits mailing list