Rev 6350: Allow config option default value to be a python callable in file:///home/vila/src/bzr/bugs/832064-default-value-callable/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Fri Dec 9 16:15:32 UTC 2011
At file:///home/vila/src/bzr/bugs/832064-default-value-callable/
------------------------------------------------------------
revno: 6350
revision-id: v.ladeuil+lp at free.fr-20111209161532-ktm62xw0euieo7wj
parent: pqm at pqm.ubuntu.com-20111206163008-eoqz60dpbysebble
fixes bug: https://launchpad.net/bugs/832064
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 832064-default-value-callable
timestamp: Fri 2011-12-09 17:15:32 +0100
message:
Allow config option default value to be a python callable
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-11-29 14:59:18 +0000
+++ b/bzrlib/config.py 2011-12-09 16:15:32 +0000
@@ -2331,8 +2331,10 @@
:param default: the default value to use when none exist in the config
stores. This is either a string that ``from_unicode`` will convert
- into the proper type or a python object that can be stringified (so
- only the empty list is supported for example).
+ into the proper type, a callable returning a unicode string so that
+ ``from_unicode`` can be used on the return value, or a python
+ object that can be stringified (so only the empty list is supported
+ for example).
:param default_from_env: A list of environment variables which can
provide a default value. 'default' will be used only if none of the
@@ -2367,6 +2369,8 @@
elif isinstance(default, (str, unicode, bool, int, float)):
# Rely on python to convert strings, booleans and integers
self.default = u'%s' % (default,)
+ elif callable(default):
+ self.default = default
else:
# other python objects are not expected
raise AssertionError('%r is not supported as a default value'
@@ -2407,7 +2411,13 @@
continue
if value is None:
# Otherwise, fallback to the value defined at registration
- value = self.default
+ if callable(self.default):
+ value = self.default()
+ if not isinstance(value, unicode):
+ raise AssertionError(
+ 'Callable default values should be unicode')
+ else:
+ value = self.default
return value
def get_help_text(self, additional_see_also=None, plain=True):
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-11-22 11:50:36 +0000
+++ b/bzrlib/tests/test_config.py 2011-12-09 16:15:32 +0000
@@ -2275,6 +2275,12 @@
opt = config.Option('foo', default='bar')
self.assertEquals('bar', opt.get_default())
+ def test_callable_default_value(self):
+ def bar_as_unicode():
+ return u'bar'
+ opt = config.Option('foo', default=bar_as_unicode)
+ self.assertEquals('bar', opt.get_default())
+
def test_default_value_from_env(self):
opt = config.Option('foo', default='bar', default_from_env=['FOO'])
self.overrideEnv('FOO', 'quux')
@@ -2296,6 +2302,12 @@
self.assertRaises(AssertionError, config.Option, 'foo',
default=object())
+ def test_not_supported_callable_default_value_not_unicode(self):
+ def bar_not_unicode():
+ return 'bar'
+ opt = config.Option('foo', default=bar_not_unicode)
+ self.assertRaises(AssertionError, opt.get_default)
+
class TestOptionConverterMixin(object):
@@ -2363,6 +2375,7 @@
opt = self.get_option()
self.assertConverted(16, opt, u'16')
+
class TestOptionWithListConverter(tests.TestCase, TestOptionConverterMixin):
def get_option(self):
More information about the bazaar-commits
mailing list