From 277a749ca0887819048ae1892b0dfa543bf30d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Fran=C3=A7oise?= Date: Mon, 23 May 2016 10:44:01 +0200 Subject: [PATCH] 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 --- watcher/_i18n.py | 5 +++++ watcher/db/purge.py | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/watcher/_i18n.py b/watcher/_i18n.py index 543ba8c0f..d7a1b7374 100644 --- a/watcher/_i18n.py +++ b/watcher/_i18n.py @@ -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) diff --git a/watcher/db/purge.py b/watcher/db/purge.py index a66682108..b348dda79 100644 --- a/watcher/db/purge.py +++ b/watcher/db/purge.py @@ -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()