Rev 6379: Add int_SI_from_store as a config option helper in file:///home/vila/src/bzr/experimental/config-si-unit/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Fri Dec 16 16:20:02 UTC 2011
At file:///home/vila/src/bzr/experimental/config-si-unit/
------------------------------------------------------------
revno: 6379
revision-id: v.ladeuil+lp at free.fr-20111216162001-qe39b3khelh34u1v
parent: pqm at pqm.ubuntu.com-20111215183846-l332p0xr8hy0ekhf
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-si-unit
timestamp: Fri 2011-12-16 17:20:01 +0100
message:
Add int_SI_from_store as a config option helper
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-12-15 18:38:46 +0000
+++ b/bzrlib/config.py 2011-12-16 16:20:01 +0000
@@ -2455,6 +2455,25 @@
return int(unicode_str)
+_unit_sfxs = dict(K=10**3, M=10**6, G=10**9)
+
+def int_SI_from_store(unicode_str):
+ regexp = "^(\d+)(([" + ''.join(_unit_sfxs) + "])b?)?$"
+ p = re.compile(regexp, re.IGNORECASE)
+ m = p.match(unicode_str)
+ val = None
+ if m is not None:
+ val, _, unit = m.groups()
+ val = int(val)
+ if unit:
+ try:
+ coeff = _unit_sfxs[unit.upper()]
+ except KeyError:
+ raise ValueError(gettext('{0} is not an SI unit.').format(unit))
+ val *= coeff
+ return val
+
+
def float_from_store(unicode_str):
return float(unicode_str)
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-12-15 11:53:48 +0000
+++ b/bzrlib/tests/test_config.py 2011-12-16 16:20:01 +0000
@@ -1080,6 +1080,7 @@
self.assertEqual(None, get_si('non-exist'))
self.assertEqual(42, get_si('non-exist-with-default', 42))
+
class TestSupressWarning(TestIniConfig):
def make_warnings_config(self, s):
@@ -2358,7 +2359,8 @@
class TestOptionConverterMixin(object):
def assertConverted(self, expected, opt, value):
- self.assertEquals(expected, opt.convert_from_unicode(value))
+ self.assertEquals(expected, opt.convert_from_unicode(value),
+ 'Expecting %s, got %s' % (expected, value,))
def assertWarns(self, opt, value):
warnings = []
@@ -2377,7 +2379,8 @@
def assertConvertInvalid(self, opt, invalid_value):
opt.invalid = None
- self.assertEquals(None, opt.convert_from_unicode(invalid_value))
+ self.assertEquals(None, opt.convert_from_unicode(invalid_value),
+ '%s is not None' % (invalid_value,))
opt.invalid = 'warning'
self.assertWarns(opt, invalid_value)
opt.invalid = 'error'
@@ -2422,6 +2425,31 @@
self.assertConverted(16, opt, u'16')
+class TestOptionWithSIUnitConverter(tests.TestCase, TestOptionConverterMixin):
+
+ def get_option(self):
+ return config.Option('foo', help='An integer in SI units.',
+ from_unicode=config.int_SI_from_store)
+
+ def test_convert_invalid(self):
+ opt = self.get_option()
+ self.assertConvertInvalid(opt, u'not-a-unit')
+ self.assertConvertInvalid(opt, u'Gb') # Forgot the int
+ self.assertConvertInvalid(opt, u'1b') # Forgot the unit
+ self.assertConvertInvalid(opt, u'1GG')
+ self.assertConvertInvalid(opt, u'1Mbb')
+ self.assertConvertInvalid(opt, u'1MM')
+
+ def test_convert_valid(self):
+ opt = self.get_option()
+ self.assertConverted(int(5e3), opt, u'5kb')
+ self.assertConverted(int(5e6), opt, u'5M')
+ self.assertConverted(int(5e6), opt, u'5MB')
+ self.assertConverted(int(5e9), opt, u'5g')
+ self.assertConverted(int(5e9), opt, u'5gB')
+ self.assertConverted(100, opt, u'100')
+
+
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-15 14:47:22 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt 2011-12-16 16:20:01 +0000
@@ -99,6 +99,9 @@
.. Major internal changes, unlikely to be visible to users or plugin
developers, but interesting for bzr developers.
+* Configuration options can be SI units by using ``int_SI_from_unicode`` as
+ their ``convert_from_unicode`` helper. (Vincent Ladeuil)
+
* ControlDir now has a get_branches method that returns a dictionary
whose keys are the names of the branches and whose values are the
branches themselves. The active branch uses the key None.
More information about the bazaar-commits
mailing list