From: David-Sarah Hopwood <david-sarah@jacaranda.org>
Date: Thu, 27 Dec 2012 20:08:00 +0000 (+0000)
Subject: Allow T (terabytes), P (petabytes), and E (exabytes), case-insensitive, in parse_abbr... 
X-Git-Tag: allmydata-tahoe-1.10a1~29
X-Git-Url: https://git.rkrishnan.org/vdrive/listings/class-simplejson.JSONEncoder-index.html?a=commitdiff_plain;h=6d2ca45c4a75d55feba2fb96f2957eb86bfe9d7d;p=tahoe-lafs%2Ftahoe-lafs.git

Allow T (terabytes), P (petabytes), and E (exabytes), case-insensitive, in parse_abbreviated_size.
This also simplifies how case-insensitivity is handled, and fixes a corner case
where the wrong exception was raised when the size ends in "BB".
fixes #1812

Signed-off-by: David-Sarah Hopwood <davidsarah@mint>
---

diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py
index 0db1a89b..b2d9bbec 100644
--- a/src/allmydata/test/test_util.py
+++ b/src/allmydata/test/test_util.py
@@ -775,9 +775,20 @@ class Abbreviate(unittest.TestCase):
         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))
+        self.failUnlessEqual(p("3TB"), 3*1000*1000*1000*1000)
+        self.failUnlessEqual(p("3TiB"), 3*1024*1024*1024*1024)
+        self.failUnlessEqual(p("6PB"), 6*1000*1000*1000*1000*1000)
+        self.failUnlessEqual(p("6PiB"), 6*1024*1024*1024*1024*1024)
+        self.failUnlessEqual(p("9EB"), 9*1000*1000*1000*1000*1000*1000)
+        self.failUnlessEqual(p("9EiB"), 9*1024*1024*1024*1024*1024*1024)
 
+        e = self.failUnlessRaises(ValueError, p, "12 cubits")
+        self.failUnlessIn("12 cubits", str(e))
+        e = self.failUnlessRaises(ValueError, p, "1 BB")
+        self.failUnlessIn("1 BB", str(e))
+        e = self.failUnlessRaises(ValueError, p, "fhtagn")
+        self.failUnlessIn("fhtagn", str(e))
+        
 class Limiter(unittest.TestCase):
     timeout = 480 # This takes longer than 240 seconds on Francois's arm box.
 
diff --git a/src/allmydata/util/abbreviate.py b/src/allmydata/util/abbreviate.py
index de0571e1..46dda6c9 100644
--- a/src/allmydata/util/abbreviate.py
+++ b/src/allmydata/util/abbreviate.py
@@ -58,20 +58,25 @@ def abbreviate_space_both(s):
 def parse_abbreviated_size(s):
     if s is None or s == "":
         return None
-    m = re.match(r"^(\d+)([kKmMgG]?[iB]?[bB]?)$", s)
+    m = re.match(r"^(\d+)([KMGTPE]?[I]?[B]?)$", s.upper())
     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,
+    multiplier = {"":   1,
+                  "I":  1,
+                  "K":  1000,
+                  "M":  1000 * 1000,
+                  "G":  1000 * 1000 * 1000,
+                  "T":  1000 * 1000 * 1000 * 1000,
+                  "P":  1000 * 1000 * 1000 * 1000 * 1000,
+                  "E":  1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                   "KI": 1024,
-                  "MI": 1024*1024,
-                  "GI": 1024*1024*1024,
+                  "MI": 1024 * 1024,
+                  "GI": 1024 * 1024 * 1024,
+                  "TI": 1024 * 1024 * 1024 * 1024,
+                  "PI": 1024 * 1024 * 1024 * 1024 * 1024,
+                  "EI": 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                   }[suffix]
     return int(number) * multiplier