]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
util/abbreviate: add abbreviated-size parser
authorBrian Warner <warner@lothar.com>
Tue, 2 Dec 2008 00:24:12 +0000 (17:24 -0700)
committerBrian Warner <warner@lothar.com>
Tue, 2 Dec 2008 00:24:12 +0000 (17:24 -0700)
src/allmydata/test/test_util.py
src/allmydata/util/abbreviate.py

index f44fa09b880b37c7e42b6916c89dc96337478f9b..d69f8480722b1ac55590d3fea549a06d8c3efbbe 100644 (file)
@@ -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) )
index 13c51c78e800f9f85a264d16ddf31c78703e49d3..de0571e1da4c94d29085580cd9b46348997b52cf 100644 (file)
@@ -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