summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-07-04 11:35:04 +1200
committerLars Wirzenius <liw@liw.fi>2010-07-04 11:35:04 +1200
commit5f1dde68e0ddebf8158c95e7cc895609039cbcde (patch)
treef368692c42e5304e36d85a7ed15c14cb640997bf
parentbe71819f03d5ff69879c71709c3598f0400afdd8 (diff)
downloadobnam-5f1dde68e0ddebf8158c95e7cc895609039cbcde.tar.gz
Fix parsing of b-less units.
-rw-r--r--obnamlib/sizeparse.py19
-rw-r--r--obnamlib/sizeparse_tests.py9
2 files changed, 20 insertions, 8 deletions
diff --git a/obnamlib/sizeparse.py b/obnamlib/sizeparse.py
index 78dd63ac..0f37a039 100644
--- a/obnamlib/sizeparse.py
+++ b/obnamlib/sizeparse.py
@@ -43,13 +43,16 @@ class ByteSizeParser(object):
r'(?P<unit>[kmg]?i?b?)?$', re.I)
units = {
- 'b': ('B', 1),
- 'kb': ('kB', 1000),
- 'kib': ('KiB', 1024),
- 'mb': ('kB', 1000**2),
- 'mib': ('KiB', 1024**2),
- 'gb': ('GB', 1000**3),
- 'gib': ('GiB', 1024**3),
+ 'b': 1,
+ 'k': 1000,
+ 'kb': 1000,
+ 'kib': 1024,
+ 'm': 1000**2,
+ 'mb': 1000**2,
+ 'mib': 1024**2,
+ 'g': 1000**3,
+ 'gb': 1000**3,
+ 'gib': 1024**3,
}
def __init__(self):
@@ -70,5 +73,5 @@ class ByteSizeParser(object):
unit = self.default_unit
elif unit.lower() not in self.units:
raise UnitNameError(unit)
- unit_name, factor = self.units[unit.lower()]
+ factor = self.units[unit.lower()]
return size * factor
diff --git a/obnamlib/sizeparse_tests.py b/obnamlib/sizeparse_tests.py
index fcafb653..6b679226 100644
--- a/obnamlib/sizeparse_tests.py
+++ b/obnamlib/sizeparse_tests.py
@@ -37,18 +37,27 @@ class ByteSizeParserTests(unittest.TestCase):
def test_parses_size_with_byte_unit(self):
self.assertEqual(self.p.parse('123 B'), 123)
+ def test_parses_size_with_kilo_unit(self):
+ self.assertEqual(self.p.parse('123 k'), 123 * 1000)
+
def test_parses_size_with_kilobyte_unit(self):
self.assertEqual(self.p.parse('123 kB'), 123 * 1000)
def test_parses_size_with_kibibyte_unit(self):
self.assertEqual(self.p.parse('123 KiB'), 123 * 1024)
+ def test_parses_size_with_mega_unit(self):
+ self.assertEqual(self.p.parse('123 m'), 123 * 1000**2)
+
def test_parses_size_with_megabyte_unit(self):
self.assertEqual(self.p.parse('123 MB'), 123 * 1000**2)
def test_parses_size_with_mebibyte_unit(self):
self.assertEqual(self.p.parse('123 MiB'), 123 * 1024**2)
+ def test_parses_size_with_giga_unit(self):
+ self.assertEqual(self.p.parse('123 g'), 123 * 1000**3)
+
def test_parses_size_with_gigabyte_unit(self):
self.assertEqual(self.p.parse('123 GB'), 123 * 1000**3)