mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-24 15:16:41 +00:00
util/abbreviate: add abbreviated-size parser
This commit is contained in:
parent
fe6abac87b
commit
39a41f1d85
@ -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) )
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user