From c08e46d5ba780791e39a69444b6a9ae6f9e3906c Mon Sep 17 00:00:00 2001 From: Brian Rosmaita Date: Wed, 31 May 2023 13:28:24 -0400 Subject: [PATCH] strutils: update string_to_bytes Update the string_to_bytes function to support current IEC/SI prefixes. Change-Id: I9aa51df4825ffbe87598feb06821e8984cacd461 --- oslo_utils/strutils.py | 33 ++++++++++++++++++++++++------- oslo_utils/tests/test_strutils.py | 12 +++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py index 2bd89ee9..2eb88fa8 100644 --- a/oslo_utils/strutils.py +++ b/oslo_utils/strutils.py @@ -37,11 +37,24 @@ UNIT_PREFIX_EXPONENT = { 'Gi': 3, 'T': 4, 'Ti': 4, + 'P': 5, + 'Pi': 5, + 'E': 6, + 'Ei': 6, + 'Z': 7, + 'Zi': 7, + 'Y': 8, + 'Yi': 8, + 'R': 9, + 'Ri': 9, + 'Q': 10, + 'Qi': 10, } 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)$')), + 'IEC': (1024, re.compile(r'(^[-+]?\d*\.?\d+)([KMGTPEZYRQ]i?)?(b|bit|B)$')), + 'SI': (1000, re.compile(r'(^[-+]?\d*\.?\d+)([kMGTPEZYRQ])?(b|bit|B)$')), + 'mixed': (None, re.compile( + r'(^[-+]?\d*\.?\d+)([kKMGTPEZYRQ]i?)?(b|bit|B)$')), } TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') @@ -182,13 +195,19 @@ def string_to_bytes(text, unit_system='IEC', return_int=False): 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 + Kb(it), Kib(it), Mb(it), Mib(it), Gb(it), Gib(it), Tb(it), Tib(it), + Pb(it), Pib(it), Eb(it), Eib(it), Zb(it), Zib(it), Yb(it), Yib(it), + Rb(it), Rib(it), Qb(it), Qib(it) + + KB, KiB, MB, MiB, GB, GiB, TB, TiB, PB, PiB, EB, EiB, ZB, ZiB, + YB, YiB, RB, RiB, QB, QiB The units supported for SI :: - kb(it), Mb(it), Gb(it), Tb(it) - kB, MB, GB, TB + kb(it), Mb(it), Gb(it), Tb(it), Pb(it), Eb(it), Zb(it), Yb(it), + Rb(it), Qb(it) + + kB, MB, GB, TB, PB, EB, ZB, YB, RB, QB SI units are interpreted as power-of-ten (e.g. 1kb = 1000b). Note that the SI unit system does not support capital letter 'K' diff --git a/oslo_utils/tests/test_strutils.py b/oslo_utils/tests/test_strutils.py index 2be5d92c..a2ac5dac 100644 --- a/oslo_utils/tests/test_strutils.py +++ b/oslo_utils/tests/test_strutils.py @@ -207,10 +207,22 @@ class StringToBytesTest(test_base.BaseTestCase): ('M', dict(unit_prefix='M')), ('G', dict(unit_prefix='G')), ('T', dict(unit_prefix='T')), + ('P', dict(unit_prefix='P')), + ('E', dict(unit_prefix='E')), + ('Z', dict(unit_prefix='Z')), + ('Y', dict(unit_prefix='Y')), + ('R', dict(unit_prefix='R')), + ('Q', dict(unit_prefix='Q')), ('Ki', dict(unit_prefix='Ki')), ('Mi', dict(unit_prefix='Mi')), ('Gi', dict(unit_prefix='Gi')), ('Ti', dict(unit_prefix='Ti')), + ('Pi', dict(unit_prefix='Pi')), + ('Ei', dict(unit_prefix='Ei')), + ('Zi', dict(unit_prefix='Zi')), + ('Yi', dict(unit_prefix='Yi')), + ('Ri', dict(unit_prefix='Ri')), + ('Qi', dict(unit_prefix='Qi')), ('invalid_unit_prefix', dict(unit_prefix='B', assert_error=True)), ]