summaryrefslogtreecommitdiffstats
path: root/openstack/common
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-07-20 13:26:11 +0000
committerGerrit Code Review <review@openstack.org>2013-07-20 13:26:11 +0000
commit6dd15766c82079db345a45cc20a9410c968cd599 (patch)
tree8e41c31d8662a97bd1a5c64e5e7d6657191a2f9d /openstack/common
parent3c69baf5c8db4b98df93c8ef5702d7df095d69ff (diff)
parent79ee3672d46ea324d5c1fca6e9f97876e3c267f2 (diff)
downloadoslo-6dd15766c82079db345a45cc20a9410c968cd599.tar.gz
oslo-6dd15766c82079db345a45cc20a9410c968cd599.tar.xz
oslo-6dd15766c82079db345a45cc20a9410c968cd599.zip
Merge "Add more robust gettext interpolation handling"
Diffstat (limited to 'openstack/common')
-rw-r--r--openstack/common/gettextutils.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/openstack/common/gettextutils.py b/openstack/common/gettextutils.py
index cacdb5b..635a434 100644
--- a/openstack/common/gettextutils.py
+++ b/openstack/common/gettextutils.py
@@ -28,6 +28,7 @@ import copy
import gettext
import logging.handlers
import os
+import re
import UserString
import six
@@ -124,14 +125,44 @@ class Message(UserString.UserString, object):
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
# we actually have parameters to inject,
# 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