19
19
System-level utilities and helper functions.
24
26
from cinder.openstack.common.gettextutils import _
29
# Used for looking up extensions of text
30
# to their 'multiplied' byte amount
38
BYTE_REGEX = re.compile(r'(^-?\d+)(\D*)')
27
40
TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes')
28
41
FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no')
43
SLUGIFY_STRIP_RE = re.compile(r"[^\w\s-]")
44
SLUGIFY_HYPHENATE_RE = re.compile(r"[-\s]+")
31
47
def int_from_bool_as_string(subject):
33
Interpret a string as a boolean and return either 1 or 0.
48
"""Interpret a string as a boolean and return either 1 or 0.
35
50
Any string value in:
46
61
def bool_from_string(subject, strict=False):
48
Interpret a string as a boolean.
62
"""Interpret a string as a boolean.
50
64
A case-insensitive match is performed such that strings matching 't',
51
65
'true', 'on', 'y', 'yes', or '1' are considered True and, when
80
94
def safe_decode(text, incoming=None, errors='strict'):
82
Decodes incoming str using `incoming` if they're
95
"""Decodes incoming str using `incoming` if they're not already unicode.
85
97
:param incoming: Text's current encoding
86
98
:param errors: Errors handling policy. See here for valid
120
132
def safe_encode(text, incoming=None,
121
133
encoding='utf-8', errors='strict'):
123
Encodes incoming str/unicode using `encoding`. If
124
incoming is not specified, text is expected to
125
be encoded with current python's default encoding.
126
(`sys.getdefaultencoding`)
134
"""Encodes incoming str/unicode using `encoding`.
136
If incoming is not specified, text is expected to be encoded with
137
current python's default encoding. (`sys.getdefaultencoding`)
128
139
:param incoming: Text's current encoding
129
140
:param encoding: Expected encoding for text (Default UTF-8)
148
159
return text.encode(encoding, errors)
164
def to_bytes(text, default=0):
165
"""Converts a string into an integer of bytes.
167
Looks at the last characters of the text to determine
168
what conversion is needed to turn the input text into a byte number.
169
Supports "B, K(B), M(B), G(B), and T(B)". (case insensitive)
171
:param text: String input for bytes size conversion.
172
:param default: Default return value when text is blank.
175
match = BYTE_REGEX.search(text)
177
magnitude = int(match.group(1))
178
mult_key_org = match.group(2)
182
msg = _('Invalid string format: %s') % text
186
mult_key = mult_key_org.lower().replace('b', '', 1)
187
multiplier = BYTE_MULTIPLIERS.get(mult_key)
188
if multiplier is None:
189
msg = _('Unknown byte multiplier: %s') % mult_key_org
191
return magnitude * multiplier
194
def to_slug(value, incoming=None, errors="strict"):
197
Convert to lowercase, remove non-word characters, and convert spaces
200
Inspired by Django's `slugify` filter.
202
:param value: Text to slugify
203
:param incoming: Text's current encoding
204
:param errors: Errors handling policy. See here for valid
205
values http://docs.python.org/2/library/codecs.html
206
:returns: slugified unicode representation of `value`
207
:raises TypeError: If text is not an instance of basestring
209
value = safe_decode(value, incoming, errors)
210
# NOTE(aababilov): no need to use safe_(encode|decode) here:
211
# encodings are always "ascii", error handling is always "ignore"
212
# and types are always known (first: unicode; second: str)
213
value = unicodedata.normalize("NFKD", value).encode(
214
"ascii", "ignore").decode("ascii")
215
value = SLUGIFY_STRIP_RE.sub("", value).strip().lower()
216
return SLUGIFY_HYPHENATE_RE.sub("-", value)