diff options
author | Ian Wienand <iwienand@redhat.com> | 2017-09-13 18:53:55 +1000 |
---|---|---|
committer | Ian Wienand <iwienand@redhat.com> | 2017-09-13 21:10:39 +1000 |
commit | 7a00f832f9765a8a4a0640ee69da2ce77f9bf6f0 (patch) | |
tree | 36a379f200ce725b76b4ad3fb941c77e4356727b | |
parent | 08e52a1101518ccc3ecba4f383c5e0ec8db64a5e (diff) | |
download | oslo-utils-7a00f832f9765a8a4a0640ee69da2ce77f9bf6f0.tar.gz |
Add a mixed mode parser to string_to_bytes
This proposes a "mixed" parsing type that interprets the IEC-ish "i"
units as power-of-two and the SI-style "non-i" units as power-of-ten.
This is a round-about way of saying it is basically making it the same
as the way GNU parted interprets such strings [1].
While I'd be the first to admit it's a bit ugly, I think it could
serve a role for people who have to interpret such strings for
compatability reasons. In particuarly within diskimage-builder, we've
accepted these strings for partition creation (being modeled on
parted) and I'd certainly like to not have our own unit parser if we
can help it.
[1] https://www.gnu.org/software/parted/manual/html_node/unit.html
Change-Id: I772bcb6651484d68afabd1965e5db033439c6257
-rw-r--r-- | oslo_utils/strutils.py | 28 | ||||
-rw-r--r-- | oslo_utils/tests/test_strutils.py | 9 |
2 files changed, 35 insertions, 2 deletions
diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py index 7376ce0..69e562f 100644 --- a/oslo_utils/strutils.py +++ b/oslo_utils/strutils.py @@ -44,6 +44,7 @@ UNIT_PREFIX_EXPONENT = { UNIT_SYSTEM_INFO = { 'IEC': (1024, re.compile(r'(^[-+]?\d*\.?\d+)([KMGT]i?)?(b|bit|B)$')), 'SI': (1000, re.compile(r'(^[-+]?\d*\.?\d+)([kMGT])?(b|bit|B)$')), + 'mixed': (None, re.compile(r'(^[-+]?\d*\.?\d+)([kKMGT]i?)?(b|bit|B)$')), } TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') @@ -164,7 +165,7 @@ def is_valid_boolstr(value): def string_to_bytes(text, unit_system='IEC', return_int=False): """Converts a string into an float representation of bytes. - The units supported for IEC :: + The units supported for IEC / mixed:: Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it) KB, KiB, MB, MiB, GB, GiB, TB, TiB @@ -174,7 +175,17 @@ def string_to_bytes(text, unit_system='IEC', return_int=False): kb(it), Mb(it), Gb(it), Tb(it) kB, MB, GB, TB - Note that the SI unit system does not support capital letter 'K' + SI units are interpreted as power-of-ten (e.g. 1kb = 1000b). Note + that the SI unit system does not support capital letter 'K' + + IEC units are interpreted as power-of-two (e.g. 1MiB = 1MB = + 1024b) + + Mixed units interpret the "i" to mean IEC, and no "i" to mean SI + (e.g. 1kb = 1000b, 1kib == 1024b). Additionaly, mixed units + interpret 'K' as power-of-ten. This mode is not particuarly + useful for new code, but can help with compatability for parsers + such as GNU parted. :param text: String input for bytes size conversion. :param unit_system: Unit system for byte size conversion. @@ -195,9 +206,22 @@ def string_to_bytes(text, unit_system='IEC', return_int=False): unit_prefix = match.group(2) if match.group(3) in ['b', 'bit']: magnitude /= 8 + + # In the mixed matcher, IEC units (with a trailing 'i') are + # interpreted as power-of-two, others as power-of-ten + if unit_system == 'mixed': + if unit_prefix and not unit_prefix.endswith('i'): + # For maximum compatability in mixed mode, we understand + # "K" (which is not strict SI) as "k" + if unit_prefix.startswith == 'K': + unit_prefix = 'k' + base = 1000 + else: + base = 1024 else: msg = _('Invalid string format: %s') % text raise ValueError(msg) + if not unit_prefix: res = magnitude else: diff --git a/oslo_utils/tests/test_strutils.py b/oslo_utils/tests/test_strutils.py index ebc4002..1162a21 100644 --- a/oslo_utils/tests/test_strutils.py +++ b/oslo_utils/tests/test_strutils.py @@ -186,6 +186,7 @@ class StringToBytesTest(test_base.BaseTestCase): _unit_system = [ ('si', dict(unit_system='SI')), ('iec', dict(unit_system='IEC')), + ('mixed', dict(unit_system='mixed')), ('invalid_unit_system', dict(unit_system='KKK', assert_error=True)), ] @@ -258,6 +259,14 @@ class StringToBytesTest(test_base.BaseTestCase): res = getattr(units, unit_prefix) else: res = getattr(units, '%si' % unit_prefix) + elif unit_system == 'mixed': + # Note: this will return 'i' units as power-of-two, + # and other units as power-of-ten. Additionally, for + # compatability a "K" is interpreted as "k" in mixed + # mode + if unit_prefix == 'K': + unit_prefix = 'k' + res = getattr(units, unit_prefix) return res text = ''.join([self.sign, self.magnitude, self.unit_prefix, |