From d382a20517fb40d4ab869c53be512757e8c93570 Mon Sep 17 00:00:00 2001 From: "Ivan A. Melnikov" Date: Thu, 17 Oct 2013 11:42:13 +0400 Subject: [PATCH] Update oslo and bring py3kcompat in Related-bug: #1240827 Change-Id: I8f4c78f1ff1cb73fca8af9f6df8392d1636ae95f --- openstack-common.conf | 1 + taskflow/openstack/common/gettextutils.py | 20 +++++--- .../openstack/common/py3kcompat/__init__.py | 17 +++++++ .../openstack/common/py3kcompat/urlutils.py | 51 +++++++++++++++++++ 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 taskflow/openstack/common/py3kcompat/__init__.py create mode 100644 taskflow/openstack/common/py3kcompat/urlutils.py diff --git a/openstack-common.conf b/openstack-common.conf index 316ef96ff..4bf242014 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -5,6 +5,7 @@ module=excutils module=importutils module=install_venv_common module=jsonutils +module=py3kcompat module=timeutils module=uuidutils module=versionutils diff --git a/taskflow/openstack/common/gettextutils.py b/taskflow/openstack/common/gettextutils.py index 2bfd38c1f..9728034e2 100644 --- a/taskflow/openstack/common/gettextutils.py +++ b/taskflow/openstack/common/gettextutils.py @@ -329,13 +329,21 @@ def get_available_languages(domain): def get_localized_message(message, user_locale): - """Gets a localized version of the given message in the given locale.""" + """Gets a localized version of the given message in the given locale. + + If the message is not a Message object the message is returned as-is. + If the locale is None the message is translated to the default locale. + + :returns: the translated message in unicode, or the original message if + it could not be translated + """ + translated = message if isinstance(message, Message): - if user_locale: - message.locale = user_locale - return six.text_type(message) - else: - return message + original_locale = message.locale + message.locale = user_locale + translated = six.text_type(message) + message.locale = original_locale + return translated class LocaleHandler(logging.Handler): diff --git a/taskflow/openstack/common/py3kcompat/__init__.py b/taskflow/openstack/common/py3kcompat/__init__.py new file mode 100644 index 000000000..be894cf50 --- /dev/null +++ b/taskflow/openstack/common/py3kcompat/__init__.py @@ -0,0 +1,17 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2013 Canonical Ltd. +# All Rights Reserved. +# +# 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. +# diff --git a/taskflow/openstack/common/py3kcompat/urlutils.py b/taskflow/openstack/common/py3kcompat/urlutils.py new file mode 100644 index 000000000..8459c3b45 --- /dev/null +++ b/taskflow/openstack/common/py3kcompat/urlutils.py @@ -0,0 +1,51 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2013 Canonical Ltd. +# All Rights Reserved. +# +# 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. +# + +""" +Python2/Python3 compatibility layer for OpenStack +""" + +import six + +if six.PY3: + # python3 + import urllib.parse + + urlencode = urllib.parse.urlencode + urljoin = urllib.parse.urljoin + quote = urllib.parse.quote + parse_qsl = urllib.parse.parse_qsl + unquote = urllib.parse.unquote + urlparse = urllib.parse.urlparse + urlsplit = urllib.parse.urlsplit + urlunsplit = urllib.parse.urlunsplit +else: + # python2 + import urllib + import urlparse + + urlencode = urllib.urlencode + quote = urllib.quote + unquote = urllib.unquote + + parse = urlparse + parse_qsl = parse.parse_qsl + urljoin = parse.urljoin + urlparse = parse.urlparse + urlsplit = parse.urlsplit + urlunsplit = parse.urlunsplit