diff options
author | Ghe Rivero <ghe@debian.org> | 2013-06-03 23:23:34 +0200 |
---|---|---|
committer | Mark McLoughlin <markmc@redhat.com> | 2013-06-06 16:14:42 +0100 |
commit | 4246ce0f373aa8f8955a99a3b6288a32547d8e80 (patch) | |
tree | 1ddf03cf0864aec3309c1d067ed186c2ef36204b /openstack | |
parent | 5231a786f2b0be11a1abda2f0a7f595586837b3c (diff) | |
download | oslo-4246ce0f373aa8f8955a99a3b6288a32547d8e80.tar.gz oslo-4246ce0f373aa8f8955a99a3b6288a32547d8e80.tar.xz oslo-4246ce0f373aa8f8955a99a3b6288a32547d8e80.zip |
Added common code into fileutils and strutils.
This methods are present in nova, cinder and also needed by ironic package.
openstack.common.strutils => nova.utils <=> cinder.utils
to_bytes(text, default=0)
openstack.common.fileutils => nova.utils <=> cinder.utils <=> nova/virt/libvirt/utils.py
file_open(*args, **kwargs)
Change-Id: Ia65499b77eb551c0ffb6d48767b899435469bfb6
Implements: blueprint image-tools
Diffstat (limited to 'openstack')
-rw-r--r-- | openstack/common/fileutils.py | 12 | ||||
-rw-r--r-- | openstack/common/strutils.py | 39 |
2 files changed, 51 insertions, 0 deletions
diff --git a/openstack/common/fileutils.py b/openstack/common/fileutils.py index 0c967da..9f8807f 100644 --- a/openstack/common/fileutils.py +++ b/openstack/common/fileutils.py @@ -96,3 +96,15 @@ def remove_path_on_error(path): except Exception: with excutils.save_and_reraise_exception(): delete_if_exists(path) + + +def file_open(*args, **kwargs): + """Open file + + see built-in file() documentation for more details + + Note: The reason this is kept in a separate module is to easily + be able to provide a stub module that doesn't alter system + state at all (for unit tests) + """ + return file(*args, **kwargs) diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py index 6d227c6..8a5367b 100644 --- a/openstack/common/strutils.py +++ b/openstack/common/strutils.py @@ -24,6 +24,17 @@ import sys from openstack.common.gettextutils import _ +# Used for looking up extensions of text +# to their 'multiplied' byte amount +BYTE_MULTIPLIERS = { + '': 1, + 't': 1024 ** 4, + 'g': 1024 ** 3, + 'm': 1024 ** 2, + 'k': 1024, +} + + TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no') @@ -148,3 +159,31 @@ def safe_encode(text, incoming=None, return text.encode(encoding, errors) return text + + +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) + + """ + # 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 |