From 5c188a23066ac368058a75f9940066bc7429a7c4 Mon Sep 17 00:00:00 2001 From: Aaron-DH Date: Thu, 10 Dec 2015 12:44:41 +0800 Subject: [PATCH] Fix name not defined error Add the missing import packages and format the log messages Move i18n to package(cloudkittyclient) Change-Id: I77e7059e8eb91aef131713f0720f58d23ae7c11f Closes-Bug: #1524680 --- cloudkittyclient/common/base.py | 3 +- cloudkittyclient/common/utils.py | 25 +++++++---- cloudkittyclient/i18n.py | 28 ++++++++++++ cloudkittyclient/openstack/common/_i18n.py | 45 ------------------- .../openstack/common/apiclient/base.py | 2 +- .../openstack/common/apiclient/client.py | 2 +- .../openstack/common/apiclient/exceptions.py | 2 +- .../openstack/common/apiclient/utils.py | 2 +- cloudkittyclient/openstack/common/cliutils.py | 2 +- tox.ini | 3 ++ 10 files changed, 55 insertions(+), 59 deletions(-) create mode 100644 cloudkittyclient/i18n.py delete mode 100644 cloudkittyclient/openstack/common/_i18n.py diff --git a/cloudkittyclient/common/base.py b/cloudkittyclient/common/base.py index 90cf218..f76100b 100644 --- a/cloudkittyclient/common/base.py +++ b/cloudkittyclient/common/base.py @@ -23,6 +23,7 @@ import copy from six.moves.urllib import parse from cloudkittyclient import exc +from cloudkittyclient.i18n import _ from cloudkittyclient.openstack.common.apiclient import base @@ -138,7 +139,7 @@ class CrudManager(base.CrudManager): 'name': self.resource_class.__name__, 'args': kwargs } - raise exc.NotFound(404, msg) + raise exc.HTTPNotFound(msg) return rl diff --git a/cloudkittyclient/common/utils.py b/cloudkittyclient/common/utils.py index 5e1bb56..b0635a0 100644 --- a/cloudkittyclient/common/utils.py +++ b/cloudkittyclient/common/utils.py @@ -27,6 +27,7 @@ import prettytable import six from cloudkittyclient import exc +from cloudkittyclient.i18n import _ from cloudkittyclient.openstack.common import cliutils @@ -142,8 +143,10 @@ def find_resource(manager, name_or_id): try: return manager.find(name=name_or_id) except exc.HTTPNotFound: - msg = ("No %s with a name or ID of '%s' exists." % - (manager.resource_class.__name__.lower(), name_or_id)) + msg = _("No %(name)s with a name or ID of '%(id)s' exists.") % { + "name": manager.resource_class.__name__.lower(), + "id": name_or_id + } raise exc.CommandError(msg) @@ -154,9 +157,12 @@ def args_array_to_dict(kwargs, key_to_convert): kwargs[key_to_convert] = dict(v.split("=", 1) for v in values_to_convert) except ValueError: - raise exc.CommandError( - '%s must be a list of key=value not "%s"' % ( - key_to_convert, values_to_convert)) + msg = _("%(key)s must be a list of key=value " + "not '%(value)s'") % { + "key": key_to_convert, + "value": values_to_convert + } + raise exc.CommandError(msg) return kwargs @@ -174,9 +180,12 @@ def args_array_to_list_of_dicts(kwargs, key_to_convert): dct[kv[0]] = kv[1].strip(" \"'") # strip spaces and quotes kwargs[key_to_convert].append(dct) except Exception: - raise exc.CommandError( - '%s must be a list of key1=value1;key2=value2;... not "%s"' % ( - key_to_convert, values_to_convert)) + msg = _("%(key)s must be a list of " + "key1=value1;key2=value2;... not '%(value)s'") % { + "key": key_to_convert, + "value": values_to_convert + } + raise exc.CommandError(msg) return kwargs diff --git a/cloudkittyclient/i18n.py b/cloudkittyclient/i18n.py new file mode 100644 index 0000000..58075b9 --- /dev/null +++ b/cloudkittyclient/i18n.py @@ -0,0 +1,28 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""oslo.i18n integration module. + +See http://docs.openstack.org/developer/oslo.i18n/usage.html + +""" + +import oslo_i18n as i18n + +_translators = i18n.TranslatorFactory(domain='cloudkittyclient') +i18n.enable_lazy() + +_ = _translators.primary +_LI = _translators.log_info +_LW = _translators.log_warning +_LE = _translators.log_error +_LC = _translators.log_critical diff --git a/cloudkittyclient/openstack/common/_i18n.py b/cloudkittyclient/openstack/common/_i18n.py deleted file mode 100644 index 500eda8..0000000 --- a/cloudkittyclient/openstack/common/_i18n.py +++ /dev/null @@ -1,45 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -"""oslo.i18n integration module. - -See http://docs.openstack.org/developer/oslo.i18n/usage.html - -""" - -try: - import oslo_i18n - - # NOTE(dhellmann): This reference to o-s-l-o will be replaced by the - # application name when this module is synced into the separate - # repository. It is OK to have more than one translation function - # using the same domain, since there will still only be one message - # catalog. - _translators = oslo_i18n.TranslatorFactory(domain='cloudkittyclient') - - # The primary translation function using the well-known name "_" - _ = _translators.primary - - # Translators for log levels. - # - # The abbreviated names are meant to reflect the usual use of a short - # name like '_'. The "L" is for "log" and the other letter comes from - # the level. - _LI = _translators.log_info - _LW = _translators.log_warning - _LE = _translators.log_error - _LC = _translators.log_critical -except ImportError: - # NOTE(dims): Support for cases where a project wants to use - # code from oslo-incubator, but is not ready to be internationalized - # (like tempest) - _ = _LI = _LW = _LE = _LC = lambda x: x diff --git a/cloudkittyclient/openstack/common/apiclient/base.py b/cloudkittyclient/openstack/common/apiclient/base.py index 5a4f27e..30a4737 100644 --- a/cloudkittyclient/openstack/common/apiclient/base.py +++ b/cloudkittyclient/openstack/common/apiclient/base.py @@ -44,7 +44,7 @@ from oslo_utils import strutils import six from six.moves.urllib import parse -from cloudkittyclient.openstack.common._i18n import _ +from cloudkittyclient.i18n import _ from cloudkittyclient.openstack.common.apiclient import exceptions diff --git a/cloudkittyclient/openstack/common/apiclient/client.py b/cloudkittyclient/openstack/common/apiclient/client.py index 747d078..cb1fa57 100644 --- a/cloudkittyclient/openstack/common/apiclient/client.py +++ b/cloudkittyclient/openstack/common/apiclient/client.py @@ -38,7 +38,7 @@ from oslo_utils import encodeutils from oslo_utils import importutils import requests -from cloudkittyclient.openstack.common._i18n import _ +from cloudkittyclient.i18n import _ from cloudkittyclient.openstack.common.apiclient import exceptions _logger = logging.getLogger(__name__) diff --git a/cloudkittyclient/openstack/common/apiclient/exceptions.py b/cloudkittyclient/openstack/common/apiclient/exceptions.py index 6677c27..5e498d5 100644 --- a/cloudkittyclient/openstack/common/apiclient/exceptions.py +++ b/cloudkittyclient/openstack/common/apiclient/exceptions.py @@ -38,7 +38,7 @@ import sys import six -from cloudkittyclient.openstack.common._i18n import _ +from cloudkittyclient.i18n import _ class ClientException(Exception): diff --git a/cloudkittyclient/openstack/common/apiclient/utils.py b/cloudkittyclient/openstack/common/apiclient/utils.py index 1aa94ba..f25a65a 100644 --- a/cloudkittyclient/openstack/common/apiclient/utils.py +++ b/cloudkittyclient/openstack/common/apiclient/utils.py @@ -28,7 +28,7 @@ from oslo_utils import encodeutils from oslo_utils import uuidutils import six -from cloudkittyclient.openstack.common._i18n import _ +from cloudkittyclient.i18n import _ from cloudkittyclient.openstack.common.apiclient import exceptions diff --git a/cloudkittyclient/openstack/common/cliutils.py b/cloudkittyclient/openstack/common/cliutils.py index 0c66935..e6541c1 100644 --- a/cloudkittyclient/openstack/common/cliutils.py +++ b/cloudkittyclient/openstack/common/cliutils.py @@ -30,7 +30,7 @@ import prettytable import six from six import moves -from cloudkittyclient.openstack.common._i18n import _ +from cloudkittyclient.i18n import _ class MissingArgs(Exception): diff --git a/tox.ini b/tox.ini index 6dfd84e..9eadbb0 100644 --- a/tox.ini +++ b/tox.ini @@ -32,3 +32,6 @@ show-source = True ignore = E123,E125,H803 builtins = _ exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build + +[hacking] +import_exceptions = cloudkittyclient.i18n