Fix lazy translation issue with watcher-db-manage

In this changeset, I fix the issue caused by the use of lazy
translations within the 'watcher-db-manage purge' subcommand.
This is caused by the PrettyTable dependency which performs
addition operations to format its tables and the __add__ magic
method is not supported by oslo_i18n._message.Message objects.

Change-Id: Idd590e882c697957cfaf1849c3d51b52797230f6
Closes-Bug: #1584652
This commit is contained in:
Vincent Françoise 2016-05-23 10:44:01 +02:00
parent 78689fbe3b
commit 277a749ca0
2 changed files with 14 additions and 3 deletions

View File

@ -15,6 +15,7 @@
# limitations under the License.
#
import oslo_i18n
from oslo_i18n import _lazy
# The domain is the name of the App which is used to generate the folder
# containing the translation files (i.e. the .pot file and the various locales)
@ -42,5 +43,9 @@ _LE = _translators.log_error
_LC = _translators.log_critical
def lazy_translation_enabled():
return _lazy.USE_LAZY
def get_available_languages():
return oslo_i18n.get_available_languages(DOMAIN)

View File

@ -28,6 +28,7 @@ import prettytable as ptable
from six.moves import input
from watcher._i18n import _, _LI
from watcher._i18n import lazy_translation_enabled
from watcher.common import context
from watcher.common import exception
from watcher.common import utils
@ -56,11 +57,11 @@ class WatcherObjectsMap(object):
])
def __init__(self):
for attr_name in self.__class__.keys():
for attr_name in self.keys():
setattr(self, attr_name, [])
def values(self):
return (getattr(self, key) for key in self.__class__.keys())
return (getattr(self, key) for key in self.keys())
@classmethod
def keys(cls):
@ -100,8 +101,13 @@ class WatcherObjectsMap(object):
def get_count_table(self):
headers = list(self.keymap.values())
headers.append(_("Total")) # We also add a total count
translated_headers = [
h.translate() if lazy_translation_enabled() else h
for h in headers
]
counters = [len(cat_vals) for cat_vals in self.values()] + [len(self)]
table = ptable.PrettyTable(field_names=headers)
table = ptable.PrettyTable(field_names=translated_headers)
table.add_row(counters)
return table.get_string()