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
1=== modified file 'bzrlib/config.py'
2--- bzrlib/config.py 2012-01-20 13:21:01 +0000
3+++ bzrlib/config.py 2012-01-28 15:40:30 +0000
4@@ -2396,13 +2396,17 @@
5 raise AssertionError('%r is not supported as a default value'
6 % (default,))
7 self.default_from_env = default_from_env
8- self.help = help
9+ self._help = help
10 self.from_unicode = from_unicode
11 self.unquote = unquote
12 if invalid and invalid not in ('warning', 'error'):
13 raise AssertionError("%s not supported for 'invalid'" % (invalid,))
14 self.invalid = invalid
15
16+ @property
17+ def help(self):
18+ return self._help
19+
20 def convert_from_unicode(self, store, unicode_value):
21 if self.unquote and store is not None and unicode_value is not None:
22 unicode_value = store.unquote(unicode_value)
23@@ -2548,6 +2552,42 @@
24 return l
25
26
27+class RegistryOption(Option):
28+ """Option for a choice from a registry."""
29+
30+ def __init__(self, name, registry, default_from_env=None,
31+ help=None, invalid=None):
32+ """A registry based Option definition.
33+
34+ This overrides the base class so the conversion from a unicode string
35+ can take quoting into account.
36+ """
37+ super(RegistryOption, self).__init__(
38+ name, default=lambda: unicode(registry.default_key),
39+ default_from_env=default_from_env,
40+ from_unicode=self.from_unicode, help=help,
41+ invalid=invalid, unquote=False)
42+ self.registry = registry
43+
44+ def from_unicode(self, unicode_str):
45+ if not isinstance(unicode_str, basestring):
46+ raise TypeError
47+ try:
48+ return self.registry.get(unicode_str)
49+ except KeyError:
50+ raise ValueError(
51+ "Invalid value %s for %s."
52+ "See help for a list of possible values." % (unicode_str,
53+ self.name))
54+
55+ @property
56+ def help(self):
57+ ret = [self._help, "\n\nThe following values are supported:\n"]
58+ for key in self.registry.keys():
59+ ret.append(" %s - %s\n" % (key, self.registry.get_help(key)))
60+ return "".join(ret)
61+
62+
63 class OptionRegistry(registry.Registry):
64 """Register config options by their name.
65
66
67=== modified file 'bzrlib/tests/test_config.py'
68--- bzrlib/tests/test_config.py 2012-01-09 12:20:41 +0000
69+++ bzrlib/tests/test_config.py 2012-01-28 15:40:30 +0000
70@@ -35,6 +35,7 @@
71 mail_client,
72 ui,
73 urlutils,
74+ registry as _mod_registry,
75 remote,
76 tests,
77 trace,
78@@ -2480,6 +2481,54 @@
79 self.assertConverted([u'bar'], opt, u'bar')
80
81
82+class TestRegistryOption(tests.TestCase, TestOptionConverterMixin):
83+
84+ def get_option(self, registry):
85+ return config.RegistryOption('foo', registry,
86+ help='A registry option.')
87+
88+ def test_convert_invalid(self):
89+ registry = _mod_registry.Registry()
90+ opt = self.get_option(registry)
91+ self.assertConvertInvalid(opt, [1])
92+ self.assertConvertInvalid(opt, u"notregistered")
93+
94+ def test_convert_valid(self):
95+ registry = _mod_registry.Registry()
96+ registry.register("someval", 1234)
97+ opt = self.get_option(registry)
98+ # Using a bare str() just in case
99+ self.assertConverted(1234, opt, "someval")
100+ self.assertConverted(1234, opt, u'someval')
101+ self.assertConverted(None, opt, None)
102+
103+ def test_help(self):
104+ registry = _mod_registry.Registry()
105+ registry.register("someval", 1234, help="some option")
106+ registry.register("dunno", 1234, help="some other option")
107+ opt = self.get_option(registry)
108+ self.assertEquals(
109+ 'A registry option.\n'
110+ '\n'
111+ 'The following values are supported:\n'
112+ ' dunno - some other option\n'
113+ ' someval - some option\n',
114+ opt.help)
115+
116+ def test_get_help_text(self):
117+ registry = _mod_registry.Registry()
118+ registry.register("someval", 1234, help="some option")
119+ registry.register("dunno", 1234, help="some other option")
120+ opt = self.get_option(registry)
121+ self.assertEquals(
122+ 'A registry option.\n'
123+ '\n'
124+ 'The following values are supported:\n'
125+ ' dunno - some other option\n'
126+ ' someval - some option\n',
127+ opt.get_help_text())
128+
129+
130 class TestOptionRegistry(tests.TestCase):
131
132 def setUp(self):
133
134=== modified file 'doc/developers/configuration.txt'
135--- doc/developers/configuration.txt 2012-01-09 12:20:41 +0000
136+++ doc/developers/configuration.txt 2012-01-28 15:40:30 +0000
137@@ -213,6 +213,10 @@
138
139 If you need a list value, you should use ``ListOption`` instead.
140
141+For options that take their values from a ``Registry`` object,
142+``RegistryOption`` can be used. This will automatically take care of
143+looking up the specified values in the dictionary and documenting the
144+possible values in help.
145
146 Sections
147 --------
148
149=== modified file 'doc/en/release-notes/bzr-2.6.txt'
150--- doc/en/release-notes/bzr-2.6.txt 2012-01-24 16:10:08 +0000
151+++ doc/en/release-notes/bzr-2.6.txt 2012-01-28 15:40:30 +0000
152@@ -52,6 +52,9 @@
153 or tuples of bytestrings.
154 (Jelmer Vernooij)
155
156+* New configuration option class ``RegistryOption`` which is backed
157+ onto a registry. (Jelmer Vernooij)
158+
159 Internals
160 *********
161