Merge lp:~vila/bzr/832064-default-value-callable into lp:bzr

Proposed by Vincent Ladeuil
Status: Merged
Approved by: Vincent Ladeuil
Approved revision: no longer in the source branch.
Merged at revision: 6355
Proposed branch: lp:~vila/bzr/832064-default-value-callable
Merge into: lp:bzr
Diff against target: 91 lines (+29/-3)
3 files modified
bzrlib/config.py (+13/-3)
bzrlib/tests/test_config.py (+13/-0)
doc/en/release-notes/bzr-2.5.txt (+3/-0)
To merge this branch: bzr merge lp:~vila/bzr/832064-default-value-callable
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) Approve
Review via email: mp+85155@code.launchpad.net

Commit message

Allow callables to be used to specify config option default values

Description of the change

Easy fix for a medium bug allowing more options to be migrated trivially.

When registering an option, it is now possible to specify a python callable
which must return a unicode string (from_unicode will be called if/when
needed).

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

sent to pqm by email

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 2011-12-09 12:04:25 +0000
3+++ bzrlib/config.py 2011-12-09 16:30:32 +0000
4@@ -2331,8 +2331,10 @@
5
6 :param default: the default value to use when none exist in the config
7 stores. This is either a string that ``from_unicode`` will convert
8- into the proper type or a python object that can be stringified (so
9- only the empty list is supported for example).
10+ into the proper type, a callable returning a unicode string so that
11+ ``from_unicode`` can be used on the return value, or a python
12+ object that can be stringified (so only the empty list is supported
13+ for example).
14
15 :param default_from_env: A list of environment variables which can
16 provide a default value. 'default' will be used only if none of the
17@@ -2367,6 +2369,8 @@
18 elif isinstance(default, (str, unicode, bool, int, float)):
19 # Rely on python to convert strings, booleans and integers
20 self.default = u'%s' % (default,)
21+ elif callable(default):
22+ self.default = default
23 else:
24 # other python objects are not expected
25 raise AssertionError('%r is not supported as a default value'
26@@ -2407,7 +2411,13 @@
27 continue
28 if value is None:
29 # Otherwise, fallback to the value defined at registration
30- value = self.default
31+ if callable(self.default):
32+ value = self.default()
33+ if not isinstance(value, unicode):
34+ raise AssertionError(
35+ 'Callable default values should be unicode')
36+ else:
37+ value = self.default
38 return value
39
40 def get_help_text(self, additional_see_also=None, plain=True):
41
42=== modified file 'bzrlib/tests/test_config.py'
43--- bzrlib/tests/test_config.py 2011-12-09 12:04:25 +0000
44+++ bzrlib/tests/test_config.py 2011-12-09 16:30:32 +0000
45@@ -2275,6 +2275,12 @@
46 opt = config.Option('foo', default='bar')
47 self.assertEquals('bar', opt.get_default())
48
49+ def test_callable_default_value(self):
50+ def bar_as_unicode():
51+ return u'bar'
52+ opt = config.Option('foo', default=bar_as_unicode)
53+ self.assertEquals('bar', opt.get_default())
54+
55 def test_default_value_from_env(self):
56 opt = config.Option('foo', default='bar', default_from_env=['FOO'])
57 self.overrideEnv('FOO', 'quux')
58@@ -2296,6 +2302,12 @@
59 self.assertRaises(AssertionError, config.Option, 'foo',
60 default=object())
61
62+ def test_not_supported_callable_default_value_not_unicode(self):
63+ def bar_not_unicode():
64+ return 'bar'
65+ opt = config.Option('foo', default=bar_not_unicode)
66+ self.assertRaises(AssertionError, opt.get_default)
67+
68
69 class TestOptionConverterMixin(object):
70
71@@ -2363,6 +2375,7 @@
72 opt = self.get_option()
73 self.assertConverted(16, opt, u'16')
74
75+
76 class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):
77
78 def get_option(self):
79
80=== modified file 'doc/en/release-notes/bzr-2.5.txt'
81--- doc/en/release-notes/bzr-2.5.txt 2011-12-09 12:04:25 +0000
82+++ doc/en/release-notes/bzr-2.5.txt 2011-12-09 16:30:32 +0000
83@@ -32,6 +32,9 @@
84 .. Fixes for situations where bzr would previously crash or give incorrect
85 or undesirable results.
86
87+* Allow configuration option default value to be a python callable at
88+ registration. (Vincent Ladeuil, #832064)
89+
90 * Properly ignore '\n' in an option reference since this cannot be part of a
91 config option identifier. (Vincent Ladeuil, #902125)
92