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
=== modified file 'bzrlib/config.py'
--- bzrlib/config.py 2011-12-09 12:04:25 +0000
+++ bzrlib/config.py 2011-12-09 16:30:32 +0000
@@ -2331,8 +2331,10 @@
23312331
2332 :param default: the default value to use when none exist in the config2332 :param default: the default value to use when none exist in the config
2333 stores. This is either a string that ``from_unicode`` will convert2333 stores. This is either a string that ``from_unicode`` will convert
2334 into the proper type or a python object that can be stringified (so2334 into the proper type, a callable returning a unicode string so that
2335 only the empty list is supported for example).2335 ``from_unicode`` can be used on the return value, or a python
2336 object that can be stringified (so only the empty list is supported
2337 for example).
23362338
2337 :param default_from_env: A list of environment variables which can2339 :param default_from_env: A list of environment variables which can
2338 provide a default value. 'default' will be used only if none of the2340 provide a default value. 'default' will be used only if none of the
@@ -2367,6 +2369,8 @@
2367 elif isinstance(default, (str, unicode, bool, int, float)):2369 elif isinstance(default, (str, unicode, bool, int, float)):
2368 # Rely on python to convert strings, booleans and integers2370 # Rely on python to convert strings, booleans and integers
2369 self.default = u'%s' % (default,)2371 self.default = u'%s' % (default,)
2372 elif callable(default):
2373 self.default = default
2370 else:2374 else:
2371 # other python objects are not expected2375 # other python objects are not expected
2372 raise AssertionError('%r is not supported as a default value'2376 raise AssertionError('%r is not supported as a default value'
@@ -2407,7 +2411,13 @@
2407 continue2411 continue
2408 if value is None:2412 if value is None:
2409 # Otherwise, fallback to the value defined at registration2413 # Otherwise, fallback to the value defined at registration
2410 value = self.default2414 if callable(self.default):
2415 value = self.default()
2416 if not isinstance(value, unicode):
2417 raise AssertionError(
2418 'Callable default values should be unicode')
2419 else:
2420 value = self.default
2411 return value2421 return value
24122422
2413 def get_help_text(self, additional_see_also=None, plain=True):2423 def get_help_text(self, additional_see_also=None, plain=True):
24142424
=== modified file 'bzrlib/tests/test_config.py'
--- bzrlib/tests/test_config.py 2011-12-09 12:04:25 +0000
+++ bzrlib/tests/test_config.py 2011-12-09 16:30:32 +0000
@@ -2275,6 +2275,12 @@
2275 opt = config.Option('foo', default='bar')2275 opt = config.Option('foo', default='bar')
2276 self.assertEquals('bar', opt.get_default())2276 self.assertEquals('bar', opt.get_default())
22772277
2278 def test_callable_default_value(self):
2279 def bar_as_unicode():
2280 return u'bar'
2281 opt = config.Option('foo', default=bar_as_unicode)
2282 self.assertEquals('bar', opt.get_default())
2283
2278 def test_default_value_from_env(self):2284 def test_default_value_from_env(self):
2279 opt = config.Option('foo', default='bar', default_from_env=['FOO'])2285 opt = config.Option('foo', default='bar', default_from_env=['FOO'])
2280 self.overrideEnv('FOO', 'quux')2286 self.overrideEnv('FOO', 'quux')
@@ -2296,6 +2302,12 @@
2296 self.assertRaises(AssertionError, config.Option, 'foo',2302 self.assertRaises(AssertionError, config.Option, 'foo',
2297 default=object())2303 default=object())
22982304
2305 def test_not_supported_callable_default_value_not_unicode(self):
2306 def bar_not_unicode():
2307 return 'bar'
2308 opt = config.Option('foo', default=bar_not_unicode)
2309 self.assertRaises(AssertionError, opt.get_default)
2310
22992311
2300class TestOptionConverterMixin(object):2312class TestOptionConverterMixin(object):
23012313
@@ -2363,6 +2375,7 @@
2363 opt = self.get_option()2375 opt = self.get_option()
2364 self.assertConverted(16, opt, u'16')2376 self.assertConverted(16, opt, u'16')
23652377
2378
2366class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):2379class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):
23672380
2368 def get_option(self):2381 def get_option(self):
23692382
=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- doc/en/release-notes/bzr-2.5.txt 2011-12-09 12:04:25 +0000
+++ doc/en/release-notes/bzr-2.5.txt 2011-12-09 16:30:32 +0000
@@ -32,6 +32,9 @@
32.. Fixes for situations where bzr would previously crash or give incorrect32.. Fixes for situations where bzr would previously crash or give incorrect
33 or undesirable results.33 or undesirable results.
3434
35* Allow configuration option default value to be a python callable at
36 registration. (Vincent Ladeuil, #832064)
37
35* Properly ignore '\n' in an option reference since this cannot be part of a38* Properly ignore '\n' in an option reference since this cannot be part of a
36 config option identifier. (Vincent Ladeuil, #902125)39 config option identifier. (Vincent Ladeuil, #902125)
3740