From 669cdc4ffd22e5bee5aabfb8118f4449a48c2f30 Mon Sep 17 00:00:00 2001 From: Matthew Farrellee Date: Sun, 22 Dec 2013 08:01:36 -0500 Subject: [PATCH] Replace some utils.bool_from_str with strutils Instead of using novaclient.utils' bool_from_str, use bool_from_string from the oslo incubating strutils Notes: 0. utils.bool_from_str was strict, only accepted a small set of values, while strutils.bool_from_string is not strict by default, anything not true is false 1. bool_from_string accepts on/off, which bool_from_str did not Change-Id: I04744844b55697819289def081d3c9117ed0713f --- novaclient/shell.py | 6 ++++-- novaclient/utils.py | 15 --------------- novaclient/v1_1/flavors.py | 4 ++-- novaclient/v1_1/shell.py | 11 +++++++---- novaclient/v3/shell.py | 11 +++++++---- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/novaclient/shell.py b/novaclient/shell.py index a7615b48c..c4a0c9ec3 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -145,7 +145,8 @@ class SecretsHelper(object): def password(self): if self._validate_string(self.args.os_password): return self.args.os_password - verify_pass = utils.bool_from_str(utils.env("OS_VERIFY_PASSWORD")) + verify_pass = strutils.bool_from_string( + utils.env("OS_VERIFY_PASSWORD", default=False), True) return self._prompt_password(verify_pass) @property @@ -242,7 +243,8 @@ class OpenStackComputeShell(object): help="Print debugging output") parser.add_argument('--os-cache', - default=utils.bool_from_str(utils.env('OS_CACHE', default=False)), + default=strutils.bool_from_string( + utils.env('OS_CACHE', default=False), True), action='store_true', help="Use the auth token cache. Defaults to False if env[OS_CACHE]" " is not set.") diff --git a/novaclient/utils.py b/novaclient/utils.py index 6e6406037..8a57255be 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -60,21 +60,6 @@ def add_arg(f, *args, **kwargs): f.arguments.insert(0, (args, kwargs)) -def bool_from_str(val): - """Convert a string representation of a bool into a bool value.""" - - if not val: - return False - try: - return bool(int(val)) - except ValueError: - if val.lower() in ['true', 'yes', 'y']: - return True - if val.lower() in ['false', 'no', 'n']: - return False - raise - - def add_resource_manager_extra_kwargs_hook(f, hook): """Add hook to bind CLI arguments to ResourceManager calls. diff --git a/novaclient/v1_1/flavors.py b/novaclient/v1_1/flavors.py index dfafdab3e..cfb06d1ed 100644 --- a/novaclient/v1_1/flavors.py +++ b/novaclient/v1_1/flavors.py @@ -18,7 +18,7 @@ Flavor interface. from novaclient import base from novaclient import exceptions from novaclient.openstack.common.py3kcompat import urlutils -from novaclient import utils +from novaclient.openstack.common import strutils class Flavor(base.Resource): @@ -195,7 +195,7 @@ class FlavorManager(base.ManagerWithFind): raise exceptions.CommandError("rxtx_factor must be a float.") try: - is_public = utils.bool_from_str(is_public) + is_public = strutils.bool_from_string(is_public, True) except Exception: raise exceptions.CommandError("is_public must be a boolean.") diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 0f7ffc241..dfa9c155d 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -652,7 +652,7 @@ def do_flavor_show(cs, args): @utils.arg('--is-public', metavar='', help="Make flavor accessible to the public (default true)", - type=utils.bool_from_str, + type=lambda v: strutils.bool_from_string(v, True), default=True) def do_flavor_create(cs, args): """Create a new flavor""" @@ -1076,7 +1076,8 @@ def do_image_delete(cs, args): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?', @@ -1612,7 +1613,8 @@ def _translate_availability_zone_keys(collection): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?', @@ -2234,7 +2236,8 @@ def do_secgroup_delete(cs, args): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?', diff --git a/novaclient/v3/shell.py b/novaclient/v3/shell.py index 8eaf2cb1d..7648e62fe 100644 --- a/novaclient/v3/shell.py +++ b/novaclient/v3/shell.py @@ -512,7 +512,7 @@ def do_flavor_show(cs, args): @utils.arg('--is-public', metavar='', help="Make flavor accessible to the public (default true)", - type=utils.bool_from_str, + type=lambda v: strutils.bool_from_string(v, True), default=True) def do_flavor_create(cs, args): """Create a new flavor""" @@ -935,7 +935,8 @@ def do_image_delete(cs, args): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?', @@ -1441,7 +1442,8 @@ def _translate_availability_zone_keys(collection): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?', @@ -2036,7 +2038,8 @@ def do_secgroup_delete(cs, args): nargs='?', type=int, const=1, - default=int(utils.bool_from_str(os.environ.get("ALL_TENANTS", 'false'))), + default=int(strutils.bool_from_string( + os.environ.get("ALL_TENANTS", 'false'), True)), help='Display information from all tenants (Admin only).') @utils.arg('--all_tenants', nargs='?',