]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Sync latest gettextutils from oslo-incubator
authorMatt Odden <mrodden@us.ibm.com>
Tue, 9 Jul 2013 05:22:28 +0000 (05:22 +0000)
committerMatt Odden <mrodden@us.ibm.com>
Wed, 31 Jul 2013 06:23:35 +0000 (06:23 +0000)
Pull some necessary changes from oslo for
implementing user-locale-api

partially implements bp user-locale-api

Change-Id: I6ac57e094e155760129e8df0e60bc7f2968aa439

neutron/openstack/common/gettextutils.py

index 0fd139e3f4ab6a49dfa03fa2a1a29359f9a9ac43..bda41979eb577f09fc97215954b827d401d7085f 100644 (file)
@@ -28,8 +28,11 @@ import copy
 import gettext
 import logging.handlers
 import os
+import re
 import UserString
 
+import six
+
 _localedir = os.environ.get('neutron'.upper() + '_LOCALEDIR')
 _t = gettext.translation('neutron', localedir=_localedir, fallback=True)
 
@@ -120,7 +123,29 @@ class Message(UserString.UserString, object):
         if self.params is not None:
             full_msg = full_msg % self.params
 
-        return unicode(full_msg)
+        return six.text_type(full_msg)
+
+    def _save_dictionary_parameter(self, dict_param):
+        full_msg = self.data
+        # look for %(blah) fields in string;
+        # ignore %% and deal with the
+        # case where % is first character on the line
+        keys = re.findall('(?:[^%]|^)%\((\w*)\)[a-z]', full_msg)
+
+        # if we don't find any %(blah) blocks but have a %s
+        if not keys and re.findall('(?:[^%]|^)%[a-z]', full_msg):
+            # apparently the full dictionary is the parameter
+            params = copy.deepcopy(dict_param)
+        else:
+            params = {}
+            for key in keys:
+                try:
+                    params[key] = copy.deepcopy(dict_param[key])
+                except TypeError:
+                    # cast uncopyable thing to unicode string
+                    params[key] = unicode(dict_param[key])
+
+        return params
 
     def _save_parameters(self, other):
         # we check for None later to see if
@@ -128,8 +153,16 @@ class Message(UserString.UserString, object):
         # so encapsulate if our parameter is actually None
         if other is None:
             self.params = (other, )
+        elif isinstance(other, dict):
+            self.params = self._save_dictionary_parameter(other)
         else:
-            self.params = copy.deepcopy(other)
+            # fallback to casting to unicode,
+            # this will handle the problematic python code-like
+            # objects that cannot be deep-copied
+            try:
+                self.params = copy.deepcopy(other)
+            except TypeError:
+                self.params = unicode(other)
 
         return self