Rev 6453: (jelmer) Add bzrlib.config.RegistryOption. (Jelmer Vernooij) in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

Patch Queue Manager pqm at pqm.ubuntu.com
Sat Jan 28 16:56:13 UTC 2012


At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6453 [merge]
revision-id: pqm at pqm.ubuntu.com-20120128165612-ji88oyq3ghpmzy0g
parent: pqm at pqm.ubuntu.com-20120128160025-fw2c8vn92w31kmy1
parent: jelmer at samba.org-20120128153727-xavwiygat5fzouy5
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Sat 2012-01-28 16:56:12 +0000
message:
  (jelmer) Add bzrlib.config.RegistryOption. (Jelmer Vernooij)
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
  doc/en/release-notes/bzr-2.6.txt bzr2.6.txt-20120116134316-8w1xxom1c7vcu1t5-1
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2012-01-27 21:59:34 +0000
+++ b/bzrlib/config.py	2012-01-28 16:56:12 +0000
@@ -2396,13 +2396,17 @@
             raise AssertionError('%r is not supported as a default value'
                                  % (default,))
         self.default_from_env = default_from_env
-        self.help = help
+        self._help = help
         self.from_unicode = from_unicode
         self.unquote = unquote
         if invalid and invalid not in ('warning', 'error'):
             raise AssertionError("%s not supported for 'invalid'" % (invalid,))
         self.invalid = invalid
 
+    @property
+    def help(self):
+        return self._help
+
     def convert_from_unicode(self, store, unicode_value):
         if self.unquote and store is not None and unicode_value is not None:
             unicode_value = store.unquote(unicode_value)
@@ -2548,6 +2552,42 @@
         return l
 
 
+class RegistryOption(Option):
+    """Option for a choice from a registry."""
+
+    def __init__(self, name, registry, default_from_env=None,
+                 help=None, invalid=None):
+        """A registry based Option definition.
+
+        This overrides the base class so the conversion from a unicode string
+        can take quoting into account.
+        """
+        super(RegistryOption, self).__init__(
+            name, default=lambda: unicode(registry.default_key),
+            default_from_env=default_from_env,
+            from_unicode=self.from_unicode, help=help,
+            invalid=invalid, unquote=False)
+        self.registry = registry
+
+    def from_unicode(self, unicode_str):
+        if not isinstance(unicode_str, basestring):
+            raise TypeError
+        try:
+            return self.registry.get(unicode_str)
+        except KeyError:
+            raise ValueError(
+                "Invalid value %s for %s."
+                "See help for a list of possible values." % (unicode_str,
+                    self.name))
+
+    @property
+    def help(self):
+        ret = [self._help, "\n\nThe following values are supported:\n"]
+        for key in self.registry.keys():
+            ret.append(" %s - %s\n" % (key, self.registry.get_help(key)))
+        return "".join(ret)
+
+
 class OptionRegistry(registry.Registry):
     """Register config options by their name.
 

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2012-01-09 12:20:41 +0000
+++ b/bzrlib/tests/test_config.py	2012-01-27 21:25:21 +0000
@@ -35,6 +35,7 @@
     mail_client,
     ui,
     urlutils,
+    registry as _mod_registry,
     remote,
     tests,
     trace,
@@ -2480,6 +2481,54 @@
         self.assertConverted([u'bar'], opt, u'bar')
 
 
+class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
+
+    def get_option(self, registry):
+        return config.RegistryOption('foo', registry,
+                help='A registry option.')
+
+    def test_convert_invalid(self):
+        registry = _mod_registry.Registry()
+        opt = self.get_option(registry)
+        self.assertConvertInvalid(opt, [1])
+        self.assertConvertInvalid(opt, u"notregistered")
+
+    def test_convert_valid(self):
+        registry = _mod_registry.Registry()
+        registry.register("someval", 1234)
+        opt = self.get_option(registry)
+        # Using a bare str() just in case
+        self.assertConverted(1234, opt, "someval")
+        self.assertConverted(1234, opt, u'someval')
+        self.assertConverted(None, opt, None)
+
+    def test_help(self):
+        registry = _mod_registry.Registry()
+        registry.register("someval", 1234, help="some option")
+        registry.register("dunno", 1234, help="some other option")
+        opt = self.get_option(registry)
+        self.assertEquals(
+            'A registry option.\n'
+            '\n'
+            'The following values are supported:\n'
+            ' dunno - some other option\n'
+            ' someval - some option\n',
+            opt.help)
+
+    def test_get_help_text(self):
+        registry = _mod_registry.Registry()
+        registry.register("someval", 1234, help="some option")
+        registry.register("dunno", 1234, help="some other option")
+        opt = self.get_option(registry)
+        self.assertEquals(
+            'A registry option.\n'
+            '\n'
+            'The following values are supported:\n'
+            ' dunno - some other option\n'
+            ' someval - some option\n',
+            opt.get_help_text())
+
+
 class TestOptionRegistry(tests.TestCase):
 
     def setUp(self):

=== modified file 'doc/developers/configuration.txt'
--- a/doc/developers/configuration.txt	2012-01-09 12:20:41 +0000
+++ b/doc/developers/configuration.txt	2012-01-28 15:37:27 +0000
@@ -213,6 +213,10 @@
 
 If you need a list value, you should use ``ListOption`` instead.
 
+For options that take their values from a ``Registry`` object,
+``RegistryOption`` can be used. This will automatically take care of
+looking up the specified values in the dictionary and documenting the
+possible values in help.
 
 Sections
 --------

=== modified file 'doc/en/release-notes/bzr-2.6.txt'
--- a/doc/en/release-notes/bzr-2.6.txt	2012-01-24 16:10:08 +0000
+++ b/doc/en/release-notes/bzr-2.6.txt	2012-01-27 21:11:36 +0000
@@ -52,6 +52,9 @@
   or tuples of bytestrings.
   (Jelmer Vernooij)
 
+* New configuration option class ``RegistryOption`` which is backed
+  onto a registry. (Jelmer Vernooij)
+
 Internals
 *********
 




More information about the bazaar-commits mailing list