summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-01-13 01:33:32 +0000
committerGerrit Code Review <review@openstack.org>2018-01-13 01:33:32 +0000
commita449545a196c6940e56fd1e319b96f85f588ba0d (patch)
treec9e71edab307c2bcdf4239a2b22e684fe1f6040b
parentd9ec025c39ed7eb96ba172fada5f924cc850d91a (diff)
parent7a00f832f9765a8a4a0640ee69da2ce77f9bf6f0 (diff)
downloadoslo-utils-a449545a196c6940e56fd1e319b96f85f588ba0d.tar.gz
Merge "Add a mixed mode parser to string_to_bytes"3.35.0
-rw-r--r--oslo_utils/strutils.py28
-rw-r--r--oslo_utils/tests/test_strutils.py9
2 files changed, 35 insertions, 2 deletions
diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py
index d03617d..929b34d 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 0dae693..af1faf1 100644
--- a/oslo_utils/tests/test_strutils.py
+++ b/oslo_utils/tests/test_strutils.py
@@ -187,6 +187,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)),
]
@@ -259,6 +260,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,