Merge lp:~jelmer/bzr/config-registry-option into lp:bzr

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6453
Proposed branch: lp:~jelmer/bzr/config-registry-option
Merge into: lp:bzr
Diff against target: 160 lines (+97/-1)
4 files modified
bzrlib/config.py (+41/-1)
bzrlib/tests/test_config.py (+49/-0)
doc/developers/configuration.txt (+4/-0)
doc/en/release-notes/bzr-2.6.txt (+3/-0)
To merge this branch: bzr merge lp:~jelmer/bzr/config-registry-option
Reviewer Review Type Date Requested Status
Vincent Ladeuil Needs Fixing
Review via email: mp+90534@code.launchpad.net

Commit message

Add bzrlib.config.RegistryOption.

Description of the change

Add bzrlib.config.RegistryOption - an Option subclass which can be set to a registry key, and provides an according help.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

Perfectly the spirit for code and tests :)

Just add a line in doc/developers/configuration.txt mentioning the RegistryOption and we're all set (this doc may need more detailed explanations but no urgency ;).

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bzrlib/config.py'
--- bzrlib/config.py 2012-01-20 13:21:01 +0000
+++ bzrlib/config.py 2012-01-28 15:40:30 +0000
@@ -2396,13 +2396,17 @@
2396 raise AssertionError('%r is not supported as a default value'2396 raise AssertionError('%r is not supported as a default value'
2397 % (default,))2397 % (default,))
2398 self.default_from_env = default_from_env2398 self.default_from_env = default_from_env
2399 self.help = help2399 self._help = help
2400 self.from_unicode = from_unicode2400 self.from_unicode = from_unicode
2401 self.unquote = unquote2401 self.unquote = unquote
2402 if invalid and invalid not in ('warning', 'error'):2402 if invalid and invalid not in ('warning', 'error'):
2403 raise AssertionError("%s not supported for 'invalid'" % (invalid,))2403 raise AssertionError("%s not supported for 'invalid'" % (invalid,))
2404 self.invalid = invalid2404 self.invalid = invalid
24052405
2406 @property
2407 def help(self):
2408 return self._help
2409
2406 def convert_from_unicode(self, store, unicode_value):2410 def convert_from_unicode(self, store, unicode_value):
2407 if self.unquote and store is not None and unicode_value is not None:2411 if self.unquote and store is not None and unicode_value is not None:
2408 unicode_value = store.unquote(unicode_value)2412 unicode_value = store.unquote(unicode_value)
@@ -2548,6 +2552,42 @@
2548 return l2552 return l
25492553
25502554
2555class RegistryOption(Option):
2556 """Option for a choice from a registry."""
2557
2558 def __init__(self, name, registry, default_from_env=None,
2559 help=None, invalid=None):
2560 """A registry based Option definition.
2561
2562 This overrides the base class so the conversion from a unicode string
2563 can take quoting into account.
2564 """
2565 super(RegistryOption, self).__init__(
2566 name, default=lambda: unicode(registry.default_key),
2567 default_from_env=default_from_env,
2568 from_unicode=self.from_unicode, help=help,
2569 invalid=invalid, unquote=False)
2570 self.registry = registry
2571
2572 def from_unicode(self, unicode_str):
2573 if not isinstance(unicode_str, basestring):
2574 raise TypeError
2575 try:
2576 return self.registry.get(unicode_str)
2577 except KeyError:
2578 raise ValueError(
2579 "Invalid value %s for %s."
2580 "See help for a list of possible values." % (unicode_str,
2581 self.name))
2582
2583 @property
2584 def help(self):
2585 ret = [self._help, "\n\nThe following values are supported:\n"]
2586 for key in self.registry.keys():
2587 ret.append(" %s - %s\n" % (key, self.registry.get_help(key)))
2588 return "".join(ret)
2589
2590
2551class OptionRegistry(registry.Registry):2591class OptionRegistry(registry.Registry):
2552 """Register config options by their name.2592 """Register config options by their name.
25532593
25542594
=== modified file 'bzrlib/tests/test_config.py'
--- bzrlib/tests/test_config.py 2012-01-09 12:20:41 +0000
+++ bzrlib/tests/test_config.py 2012-01-28 15:40:30 +0000
@@ -35,6 +35,7 @@
35 mail_client,35 mail_client,
36 ui,36 ui,
37 urlutils,37 urlutils,
38 registry as _mod_registry,
38 remote,39 remote,
39 tests,40 tests,
40 trace,41 trace,
@@ -2480,6 +2481,54 @@
2480 self.assertConverted([u'bar'], opt, u'bar')2481 self.assertConverted([u'bar'], opt, u'bar')
24812482
24822483
2484class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
2485
2486 def get_option(self, registry):
2487 return config.RegistryOption('foo', registry,
2488 help='A registry option.')
2489
2490 def test_convert_invalid(self):
2491 registry = _mod_registry.Registry()
2492 opt = self.get_option(registry)
2493 self.assertConvertInvalid(opt, [1])
2494 self.assertConvertInvalid(opt, u"notregistered")
2495
2496 def test_convert_valid(self):
2497 registry = _mod_registry.Registry()
2498 registry.register("someval", 1234)
2499 opt = self.get_option(registry)
2500 # Using a bare str() just in case
2501 self.assertConverted(1234, opt, "someval")
2502 self.assertConverted(1234, opt, u'someval')
2503 self.assertConverted(None, opt, None)
2504
2505 def test_help(self):
2506 registry = _mod_registry.Registry()
2507 registry.register("someval", 1234, help="some option")
2508 registry.register("dunno", 1234, help="some other option")
2509 opt = self.get_option(registry)
2510 self.assertEquals(
2511 'A registry option.\n'
2512 '\n'
2513 'The following values are supported:\n'
2514 ' dunno - some other option\n'
2515 ' someval - some option\n',
2516 opt.help)
2517
2518 def test_get_help_text(self):
2519 registry = _mod_registry.Registry()
2520 registry.register("someval", 1234, help="some option")
2521 registry.register("dunno", 1234, help="some other option")
2522 opt = self.get_option(registry)
2523 self.assertEquals(
2524 'A registry option.\n'
2525 '\n'
2526 'The following values are supported:\n'
2527 ' dunno - some other option\n'
2528 ' someval - some option\n',
2529 opt.get_help_text())
2530
2531
2483class TestOptionRegistry(tests.TestCase):2532class TestOptionRegistry(tests.TestCase):
24842533
2485 def setUp(self):2534 def setUp(self):
24862535
=== modified file 'doc/developers/configuration.txt'
--- doc/developers/configuration.txt 2012-01-09 12:20:41 +0000
+++ doc/developers/configuration.txt 2012-01-28 15:40:30 +0000
@@ -213,6 +213,10 @@
213213
214If you need a list value, you should use ``ListOption`` instead.214If you need a list value, you should use ``ListOption`` instead.
215215
216For options that take their values from a ``Registry`` object,
217``RegistryOption`` can be used. This will automatically take care of
218looking up the specified values in the dictionary and documenting the
219possible values in help.
216220
217Sections221Sections
218--------222--------
219223
=== modified file 'doc/en/release-notes/bzr-2.6.txt'
--- doc/en/release-notes/bzr-2.6.txt 2012-01-24 16:10:08 +0000
+++ doc/en/release-notes/bzr-2.6.txt 2012-01-28 15:40:30 +0000
@@ -52,6 +52,9 @@
52 or tuples of bytestrings.52 or tuples of bytestrings.
53 (Jelmer Vernooij)53 (Jelmer Vernooij)
5454
55* New configuration option class ``RegistryOption`` which is backed
56 onto a registry. (Jelmer Vernooij)
57
55Internals58Internals
56*********59*********
5760