diff options
author | gordon chung <gord@live.ca> | 2014-10-07 13:57:22 -0400 |
---|---|---|
committer | gordon chung <gord@live.ca> | 2014-12-05 14:54:23 -0500 |
commit | 292e55073a94abd7a51487bfc5e9529c73143a0f (patch) | |
tree | c1fb6970df432b02922ba0dc357cbf2bedb975a3 /ceilometerclient/openstack/common/cliutils.py | |
parent | 27a810146b2e5503a7b40e1ef2342005bdfd9bd2 (diff) | |
download | python-ceilometerclient-292e55073a94abd7a51487bfc5e9529c73143a0f.tar.gz |
sync with oslo and use oslo.i18n
- remove strutils and gettextutils from openstack/common
- start use of oslo.i18n
sync to Change-Id: Ia1820495a989f4f84530ab83f2d87d53a9f761df
Change-Id: I7e2ead688ac9fcab0ac6a2479e0eca12af415b07
Diffstat (limited to 'ceilometerclient/openstack/common/cliutils.py')
-rw-r--r-- | ceilometerclient/openstack/common/cliutils.py | 100 |
1 files changed, 23 insertions, 77 deletions
diff --git a/ceilometerclient/openstack/common/cliutils.py b/ceilometerclient/openstack/common/cliutils.py index c690028..341cd01 100644 --- a/ceilometerclient/openstack/common/cliutils.py +++ b/ceilometerclient/openstack/common/cliutils.py @@ -24,14 +24,21 @@ import os import sys import textwrap +from oslo.utils import encodeutils +from oslo.utils import strutils import prettytable import six from six import moves -from ceilometerclient.openstack.common.apiclient import exceptions -from ceilometerclient.openstack.common.gettextutils import _ -from ceilometerclient.openstack.common import strutils -from ceilometerclient.openstack.common import uuidutils +from ceilometerclient.openstack.common._i18n import _ + + +class MissingArgs(Exception): + """Supplied arguments are not sufficient for calling a function.""" + def __init__(self, missing): + self.missing = missing + msg = _("Missing arguments: %s") % ", ".join(missing) + super(MissingArgs, self).__init__(msg) def validate_args(fn, *args, **kwargs): @@ -64,7 +71,7 @@ def validate_args(fn, *args, **kwargs): missing = [arg for arg in required_args if arg not in kwargs] missing = missing[len(args):] if missing: - raise exceptions.MissingArgs(missing) + raise MissingArgs(missing) def arg(*args, **kwargs): @@ -156,7 +163,7 @@ def print_list(objs, fields, formatters=None, sortby_index=0, kwargs = {} else: kwargs = {'sortby': field_labels[sortby_index]} - pt = prettytable.PrettyTable(field_labels, caching=False) + pt = prettytable.PrettyTable(field_labels) pt.align = 'l' for o in objs: @@ -173,7 +180,10 @@ def print_list(objs, fields, formatters=None, sortby_index=0, row.append(data) pt.add_row(row) - print(strutils.safe_encode(pt.get_string(**kwargs))) + if six.PY3: + print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode()) + else: + print(encodeutils.safe_encode(pt.get_string(**kwargs))) def print_dict(dct, dict_property="Property", wrap=0): @@ -183,7 +193,7 @@ def print_dict(dct, dict_property="Property", wrap=0): :param dict_property: name of the first column :param wrap: wrapping for the second column """ - pt = prettytable.PrettyTable([dict_property, 'Value'], caching=False) + pt = prettytable.PrettyTable([dict_property, 'Value']) pt.align = 'l' for k, v in six.iteritems(dct): # convert dict to str to check length @@ -201,7 +211,11 @@ def print_dict(dct, dict_property="Property", wrap=0): col1 = '' else: pt.add_row([k, v]) - print(strutils.safe_encode(pt.get_string())) + + if six.PY3: + print(encodeutils.safe_encode(pt.get_string()).decode()) + else: + print(encodeutils.safe_encode(pt.get_string())) def get_password(max_password_prompts=3): @@ -225,74 +239,6 @@ def get_password(max_password_prompts=3): return pw -def find_resource(manager, name_or_id, **find_args): - """Look for resource in a given manager. - - Used as a helper for the _find_* methods. - Example: - - .. code-block:: python - - def _find_hypervisor(cs, hypervisor): - #Get a hypervisor by name or ID. - return cliutils.find_resource(cs.hypervisors, hypervisor) - """ - # first try to get entity as integer id - try: - return manager.get(int(name_or_id)) - except (TypeError, ValueError, exceptions.NotFound): - pass - - # now try to get entity as uuid - try: - if six.PY2: - tmp_id = strutils.safe_encode(name_or_id) - else: - tmp_id = strutils.safe_decode(name_or_id) - - if uuidutils.is_uuid_like(tmp_id): - return manager.get(tmp_id) - except (TypeError, ValueError, exceptions.NotFound): - pass - - # for str id which is not uuid - if getattr(manager, 'is_alphanum_id_allowed', False): - try: - return manager.get(name_or_id) - except exceptions.NotFound: - pass - - try: - try: - return manager.find(human_id=name_or_id, **find_args) - except exceptions.NotFound: - pass - - # finally try to find entity by name - try: - resource = getattr(manager, 'resource_class', None) - name_attr = resource.NAME_ATTR if resource else 'name' - kwargs = {name_attr: name_or_id} - kwargs.update(find_args) - return manager.find(**kwargs) - except exceptions.NotFound: - msg = _("No %(name)s with a name or " - "ID of '%(name_or_id)s' exists.") % \ - { - "name": manager.resource_class.__name__.lower(), - "name_or_id": name_or_id - } - raise exceptions.CommandError(msg) - except exceptions.NoUniqueMatch: - msg = _("Multiple %(name)s matches found for " - "'%(name_or_id)s', use an ID to be more specific.") % \ - { - "name": manager.resource_class.__name__.lower(), - "name_or_id": name_or_id - } - raise exceptions.CommandError(msg) - - def service_type(stype): """Adds 'service_type' attribute to decorated function. |