From: Brian Warner Date: Tue, 2 Dec 2008 00:24:12 +0000 (-0700) Subject: util/abbreviate: add abbreviated-size parser X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/provisioning?a=commitdiff_plain;h=39a41f1d854a6e9fc35e9eece4834b7c47b23b0a;p=tahoe-lafs%2Ftahoe-lafs.git util/abbreviate: add abbreviated-size parser --- diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index f44fa09b..d69f8480 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -510,6 +510,22 @@ class Abbreviate(unittest.TestCase): self.failUnlessEqual(abbreviate.abbreviate_space_both(1234567), "(1.23 MB, 1.18 MiB)") + def test_parse_space(self): + p = abbreviate.parse_abbreviated_size + self.failUnlessEqual(p(""), None) + self.failUnlessEqual(p(None), None) + self.failUnlessEqual(p("123"), 123) + self.failUnlessEqual(p("123B"), 123) + self.failUnlessEqual(p("2K"), 2000) + self.failUnlessEqual(p("2kb"), 2000) + self.failUnlessEqual(p("2KiB"), 2048) + self.failUnlessEqual(p("10MB"), 10*1000*1000) + self.failUnlessEqual(p("10MiB"), 10*1024*1024) + self.failUnlessEqual(p("5G"), 5*1000*1000*1000) + self.failUnlessEqual(p("4GiB"), 4*1024*1024*1024) + e = self.failUnlessRaises(ValueError, p, "12 cubits") + self.failUnless("12 cubits" in str(e)) + class Limiter(unittest.TestCase): def job(self, i, foo): self.calls.append( (i, foo) ) diff --git a/src/allmydata/util/abbreviate.py b/src/allmydata/util/abbreviate.py index 13c51c78..de0571e1 100644 --- a/src/allmydata/util/abbreviate.py +++ b/src/allmydata/util/abbreviate.py @@ -1,4 +1,6 @@ +import re + HOUR = 3600 DAY = 24*3600 WEEK = 7*DAY @@ -53,3 +55,23 @@ def abbreviate_space_both(s): return "(%s, %s)" % (abbreviate_space(s, True), abbreviate_space(s, False)) +def parse_abbreviated_size(s): + if s is None or s == "": + return None + m = re.match(r"^(\d+)([kKmMgG]?[iB]?[bB]?)$", s) + if not m: + raise ValueError("unparseable value %s" % s) + number, suffix = m.groups() + suffix = suffix.upper() + if suffix.endswith("B"): + suffix = suffix[:-1] + multiplier = {"": 1, + "I": 1, + "K": 1000, + "M": 1000 * 1000, + "G": 1000 * 1000 * 1000, + "KI": 1024, + "MI": 1024*1024, + "GI": 1024*1024*1024, + }[suffix] + return int(number) * multiplier