Rev 6355: (vila) Allow callables to be used to specify config option default values in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
Patch Queue Manager
pqm at pqm.ubuntu.com
Mon Dec 12 08:06:59 UTC 2011
At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 6355 [merge]
revision-id: pqm at pqm.ubuntu.com-20111212080658-180wt6u7vgvhu9sr
parent: pqm at pqm.ubuntu.com-20111211163659-9iey381vjoj039ia
parent: v.ladeuil+lp at free.fr-20111209161727-a1bbn1tfkfb6gwwi
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2011-12-12 08:06:58 +0000
message:
(vila) Allow callables to be used to specify config option default values
(Vincent Ladeuil)
modified:
bzrlib/config.py config.py-20051011043216-070c74f4e9e338e8
bzrlib/tests/test_config.py testconfig.py-20051011041908-742d0c15d8d8c8eb
doc/en/release-notes/bzr-2.5.txt bzr2.5.txt-20110708125756-587p0hpw7oke4h05-1
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-12-09 12:04:25 +0000
+++ b/bzrlib/config.py 2011-12-09 16:15:52 +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-12-09 12:04:25 +0000
+++ b/bzrlib/tests/test_config.py 2011-12-09 16:15:52 +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):
=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt 2011-12-09 21:11:41 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt 2011-12-12 08:06:58 +0000
@@ -34,6 +34,9 @@
.. Fixes for situations where bzr would previously crash or give incorrect
or undesirable results.
+* Allow configuration option default value to be a python callable at
+ registration. (Vincent Ladeuil, #832064)
+
* Properly ignore '\n' in an option reference since this cannot be part of a
config option identifier. (Vincent Ladeuil, #902125)
More information about the bazaar-commits
mailing list