From d58b4fd21304ec2f4e8445ca20cbad1518cae044 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Fri, 14 Jun 2013 09:43:40 +0800 Subject: [PATCH] Replace utils.to_bytes() with strutils.to_bytes() The to_bytes() function have moved to oslo. Replaced/removed the to_bytes() function with the one in strutils. Change-Id: I53947b7a333057fec738a02f5643be59e563290d --- cinder/image/image_utils.py | 5 ++-- cinder/openstack/common/strutils.py | 44 +++++++++++++++-------------- cinder/utils.py | 36 ----------------------- 3 files changed, 26 insertions(+), 59 deletions(-) diff --git a/cinder/image/image_utils.py b/cinder/image/image_utils.py index 2c5450e92..d9b810a1f 100644 --- a/cinder/image/image_utils.py +++ b/cinder/image/image_utils.py @@ -35,6 +35,7 @@ from oslo.config import cfg from cinder import exception from cinder.openstack.common import fileutils from cinder.openstack.common import log as logging +from cinder.openstack.common import strutils from cinder import utils @@ -94,8 +95,8 @@ class QemuImgInfo(object): if real_size: details = real_size.group(1) try: - details = utils.to_bytes(details) - except (TypeError, ValueError): + details = strutils.to_bytes(details) + except TypeError: pass return details diff --git a/cinder/openstack/common/strutils.py b/cinder/openstack/common/strutils.py index 2f93610dd..8cd996599 100644 --- a/cinder/openstack/common/strutils.py +++ b/cinder/openstack/common/strutils.py @@ -35,7 +35,7 @@ BYTE_MULTIPLIERS = { 'm': 1024 ** 2, 'k': 1024, } - +BYTE_REGEX = re.compile(r'(^-?\d+)(\D*)') TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no') @@ -162,31 +162,33 @@ def safe_encode(text, incoming=None, def to_bytes(text, default=0): - """Try to turn a string into a number of bytes. Looks at the last - characters of the text to determine what conversion is needed to - turn the input text into a byte number. + """Converts a string into an integer of bytes. + + Looks at the last characters of the text to determine + what conversion is needed to turn the input text into a byte number. + Supports "B, K(B), M(B), G(B), and T(B)". (case insensitive) - Supports: B/b, K/k, M/m, G/g, T/t (or the same with b/B on the end) + :param text: String input for bytes size conversion. + :param default: Default return value when text is blank. """ - # Take off everything not number 'like' (which should leave - # only the byte 'identifier' left) - mult_key_org = text.lstrip('-1234567890') - mult_key = mult_key_org.lower() - mult_key_len = len(mult_key) - if mult_key.endswith("b"): - mult_key = mult_key[0:-1] - try: - multiplier = BYTE_MULTIPLIERS[mult_key] - if mult_key_len: - # Empty cases shouldn't cause text[0:-0] - text = text[0:-mult_key_len] - return int(text) * multiplier - except KeyError: - msg = _('Unknown byte multiplier: %s') % mult_key_org + match = BYTE_REGEX.search(text) + if match: + magnitude = int(match.group(1)) + mult_key_org = match.group(2) + if not mult_key_org: + return magnitude + elif text: + msg = _('Invalid string format: %s') % text raise TypeError(msg) - except ValueError: + else: return default + mult_key = mult_key_org.lower().replace('b', '', 1) + multiplier = BYTE_MULTIPLIERS.get(mult_key) + if multiplier is None: + msg = _('Unknown byte multiplier: %s') % mult_key_org + raise TypeError(msg) + return magnitude * multiplier def to_slug(value, incoming=None, errors="strict"): diff --git a/cinder/utils.py b/cinder/utils.py index 98c172dbf..a13a571ce 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -1124,39 +1124,3 @@ class UndoManager(object): LOG.exception(msg, **kwargs) self._rollback() - - -def to_bytes(text, default=0): - """Try to turn a string into a number of bytes. Looks at the last - characters of the text to determine what conversion is needed to - turn the input text into a byte number. - - Supports: B/b, K/k, M/m, G/g, T/t (or the same with b/B on the end) - - """ - BYTE_MULTIPLIERS = { - '': 1, - 't': 1024 ** 4, - 'g': 1024 ** 3, - 'm': 1024 ** 2, - 'k': 1024, - } - - # Take off everything not number 'like' (which should leave - # only the byte 'identifier' left) - mult_key_org = text.lstrip('-1234567890') - mult_key = mult_key_org.lower() - mult_key_len = len(mult_key) - if mult_key.endswith("b"): - mult_key = mult_key[0:-1] - try: - multiplier = BYTE_MULTIPLIERS[mult_key] - if mult_key_len: - # Empty cases shouldn't cause text[0:-0] - text = text[0:-mult_key_len] - return int(text) * multiplier - except KeyError: - msg = _('Unknown byte multiplier: %s') % mult_key_org - raise TypeError(msg) - except ValueError: - return default -- 2.45.2