diff options
author | Rick Harris <rconradharris@gmail.com> | 2013-05-13 19:35:09 +0000 |
---|---|---|
committer | Rick Harris <rconradharris@gmail.com> | 2013-05-14 17:11:01 +0000 |
commit | 536c39e8e315af4b1823cfa378f3db441a212b1a (patch) | |
tree | 18517023b563a2ed4d4a6e9744f70378883ad5f4 /openstack | |
parent | 20379f2816774469287502cf857dc01a93ad1370 (diff) | |
download | oslo-536c39e8e315af4b1823cfa378f3db441a212b1a.tar.gz oslo-536c39e8e315af4b1823cfa378f3db441a212b1a.tar.xz oslo-536c39e8e315af4b1823cfa378f3db441a212b1a.zip |
Add 't', 'y', and `strict` to `bool_from_string`
't' and 'y' are commonly used for booleans as well, and since it's used in
Nova already, probably makes sense to add common support for it.
`strict` allows callers to detect anonmolous values so they can be handled
separately. This is particularly useful when parsing input from API calls
where bad values might result in 4XX errors instead of being treated as False.
Change-Id: I6218a1895f72cfbfd81776602e63ca3d093475e0
Diffstat (limited to 'openstack')
-rw-r--r-- | openstack/common/strutils.py | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/openstack/common/strutils.py b/openstack/common/strutils.py index d6dfb13..6d227c6 100644 --- a/openstack/common/strutils.py +++ b/openstack/common/strutils.py @@ -21,6 +21,12 @@ System-level utilities and helper functions. import sys +from openstack.common.gettextutils import _ + + +TRUE_STRINGS = ('1', 't', 'true', 'on', 'y', 'yes') +FALSE_STRINGS = ('0', 'f', 'false', 'off', 'n', 'no') + def int_from_bool_as_string(subject): """ @@ -37,27 +43,38 @@ def int_from_bool_as_string(subject): return bool_from_string(subject) and 1 or 0 -def bool_from_string(subject): +def bool_from_string(subject, strict=False): """ Interpret a string as a boolean. - Any string value in: - - ('True', 'true', 'On', 'on', 'Yes', 'yes', '1') + A case-insensitive match is performed such that strings matching 't', + 'true', 'on', 'y', 'yes', or '1' are considered True and, when + `strict=False`, anything else is considered False. - is interpreted as a boolean True. + Useful for JSON-decoded stuff and config file parsing. - Useful for JSON-decoded stuff and config file parsing + If `strict=True`, unrecognized values, including None, will raise a + ValueError which is useful when parsing values passed in from an API call. + Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. """ - try: - # True or 1 or '1' -> True - # False or < 1 or > 1 or '0' -> False - return int(subject) == 1 - except TypeError: - # None -> False + if not isinstance(subject, basestring): + subject = str(subject) + + lowered = subject.strip().lower() + + if lowered in TRUE_STRINGS: + return True + elif lowered in FALSE_STRINGS: + return False + elif strict: + acceptable = ', '.join( + "'%s'" % s for s in sorted(TRUE_STRINGS + FALSE_STRINGS)) + msg = _("Unrecognized value '%(val)s', acceptable values are:" + " %(acceptable)s") % {'val': subject, + 'acceptable': acceptable} + raise ValueError(msg) + else: return False - except ValueError: - return subject.strip().lower() in ('true', 'on', 'yes') def safe_decode(text, incoming=None, errors='strict'): |