Skip to content

Commit

Permalink
Support IEC suffix for sizes
Browse files Browse the repository at this point in the history
To be consistent with what the "aws s3 ls" command outputs.
As per review feedback.
  • Loading branch information
jamesls committed Feb 6, 2015
1 parent e187527 commit 4efade1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions awscli/customizations/s3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
'mb': 1024 ** 2,
'gb': 1024 ** 3,
'tb': 1024 ** 4,
'kib': 1024,
'mib': 1024 ** 2,
'gib': 1024 ** 3,
'tib': 1024 ** 4,
}


Expand Down Expand Up @@ -86,7 +90,12 @@ def human_readable_to_bytes(value):
:returns: The converted value in bytes as an integer
"""
suffix = value[-2:].lower()
value = value.lower()
if value[-2:] == 'ib':
# Assume IEC suffix.
suffix = value[-3:].lower()
else:
suffix = value[-2:].lower()
has_size_identifier = (
len(value) >= 2 and suffix in SIZE_SUFFIX)
if not has_size_identifier:
Expand All @@ -96,7 +105,7 @@ def human_readable_to_bytes(value):
raise ValueError("Invalid size value: %s" % value)
else:
multiplier = SIZE_SUFFIX[suffix]
return int(value[:-2]) * multiplier
return int(value[:-len(suffix)]) * multiplier


class AppendFilter(argparse.Action):
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/customizations/s3/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_convert_human_readable_to_bytes():
yield _test_convert_human_readable_to_bytes, "1GB", 1024 ** 3
yield _test_convert_human_readable_to_bytes, "1TB", 1024 ** 4

# Also because of the "ls" output for s3, we support
# the IEC "mebibyte" format (MiB).
yield _test_convert_human_readable_to_bytes, "1KiB", 1024
yield _test_convert_human_readable_to_bytes, "1kib", 1024
yield _test_convert_human_readable_to_bytes, "1MiB", 1024 ** 2
yield _test_convert_human_readable_to_bytes, "1GiB", 1024 ** 3
yield _test_convert_human_readable_to_bytes, "1TiB", 1024 ** 4


def _test_convert_human_readable_to_bytes(size_str, expected):
assert_equal(human_readable_to_bytes(size_str), expected)
Expand Down

0 comments on commit 4efade1

Please sign in to comment.