Remove all usage of six library
Replace six with Python 3 style code. Change-Id: I3d2b0f7a78d4914913a3af3b68388392bf30b982
This commit is contained in:
parent
3cf0406eae
commit
8609dc2c4c
@ -31,7 +31,6 @@ reno==3.1.0
|
||||
requests==2.14.2
|
||||
requestsexceptions==1.2.0
|
||||
rfc3986==0.3.1
|
||||
six==1.10.0
|
||||
smmap==0.9.0
|
||||
snowballstemmer==1.2.1
|
||||
Sphinx==2.0.0
|
||||
|
@ -44,9 +44,9 @@ def install(domain):
|
||||
|
||||
:param domain: the translation domain
|
||||
"""
|
||||
from six import moves
|
||||
import builtins
|
||||
tf = _factory.TranslatorFactory(domain)
|
||||
moves.builtins.__dict__['_'] = tf.primary
|
||||
builtins.__dict__['_'] = tf.primary
|
||||
|
||||
|
||||
_AVAILABLE_LANGUAGES = {}
|
||||
|
@ -23,8 +23,6 @@ import logging
|
||||
import os
|
||||
import warnings
|
||||
|
||||
import six
|
||||
|
||||
from oslo_i18n import _locale
|
||||
from oslo_i18n import _translate
|
||||
|
||||
@ -35,7 +33,7 @@ CONTEXT_SEPARATOR = "\x04"
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Message(six.text_type):
|
||||
class Message(str):
|
||||
"""A Message object is a unicode object that can be translated.
|
||||
|
||||
Translation of Message is done explicitly using the translate() method.
|
||||
@ -184,7 +182,7 @@ class Message(six.text_type):
|
||||
# by the base class (i.e. unicode()), the only thing we do here is
|
||||
# save the original msgid and the parameters in case of a translation
|
||||
params = self._sanitize_mod_params(other)
|
||||
unicode_mod = self._safe_translate(six.text_type(self), params)
|
||||
unicode_mod = self._safe_translate(str(self), params)
|
||||
modded = Message(self.msgid,
|
||||
msgtext=unicode_mod,
|
||||
params=params,
|
||||
@ -221,7 +219,7 @@ class Message(six.text_type):
|
||||
except Exception:
|
||||
# Fallback to casting to unicode this will handle the
|
||||
# python code-like objects that can't be deep-copied
|
||||
return six.text_type(param)
|
||||
return str(param)
|
||||
|
||||
def __add__(self, other):
|
||||
from oslo_i18n._i18n import _
|
||||
|
@ -14,8 +14,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import six
|
||||
|
||||
__all__ = [
|
||||
'translate',
|
||||
]
|
||||
@ -41,7 +39,7 @@ def translate(obj, desired_locale=None):
|
||||
if not isinstance(message, _message.Message):
|
||||
# If the object to translate is not already translatable,
|
||||
# let's first get its unicode representation
|
||||
message = six.text_type(obj)
|
||||
message = str(obj)
|
||||
if isinstance(message, _message.Message):
|
||||
# Even after unicoding() we still need to check if we are
|
||||
# running with translatable unicode before translating
|
||||
|
@ -16,7 +16,6 @@
|
||||
import gettext
|
||||
|
||||
import fixtures
|
||||
import six
|
||||
|
||||
from oslo_i18n import _lazy
|
||||
from oslo_i18n import _message
|
||||
@ -65,7 +64,7 @@ class Translation(fixtures.Fixture):
|
||||
:type msg: str or unicode
|
||||
|
||||
"""
|
||||
return six.text_type(msg)
|
||||
return str(msg)
|
||||
|
||||
|
||||
class ToggleLazy(fixtures.Fixture):
|
||||
|
@ -17,7 +17,6 @@
|
||||
from unittest import mock
|
||||
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
|
||||
from oslo_i18n import _factory
|
||||
from oslo_i18n import _lazy
|
||||
@ -62,48 +61,9 @@ class TranslatorFactoryTest(test_base.BaseTestCase):
|
||||
r = tf.primary('some text')
|
||||
self.assertNotIsInstance(r, _message.Message)
|
||||
|
||||
def test_py3(self):
|
||||
_lazy.enable_lazy(False)
|
||||
with mock.patch.object(six, 'PY3', True):
|
||||
with mock.patch('gettext.translation') as translation:
|
||||
trans = mock.Mock()
|
||||
translation.return_value = trans
|
||||
trans.ugettext.side_effect = AssertionError(
|
||||
'should have called gettext')
|
||||
tf = _factory.TranslatorFactory('domain')
|
||||
tf.primary('some text')
|
||||
trans.gettext.assert_called_with('some text')
|
||||
|
||||
def test_log_level_domain_name(self):
|
||||
with mock.patch.object(_factory.TranslatorFactory,
|
||||
'_make_translation_func') as mtf:
|
||||
tf = _factory.TranslatorFactory('domain')
|
||||
tf._make_log_translation_func('mylevel')
|
||||
mtf.assert_called_with('domain-log-mylevel')
|
||||
|
||||
def test_contextual_form_py3(self):
|
||||
_lazy.enable_lazy(False)
|
||||
with mock.patch.object(six, 'PY3', True):
|
||||
with mock.patch('gettext.translation') as translation:
|
||||
trans = mock.Mock()
|
||||
translation.return_value = trans
|
||||
trans.ugettext.side_effect = AssertionError(
|
||||
'should have called gettext')
|
||||
trans.gettext.return_value = "some text"
|
||||
tf = _factory.TranslatorFactory('domain')
|
||||
tf.contextual_form('context', 'some text')
|
||||
trans.gettext.assert_called_with(
|
||||
"%s%s%s" % ('context', CONTEXT_SEPARATOR, 'some text'))
|
||||
|
||||
def test_plural_form_py3(self):
|
||||
_lazy.enable_lazy(False)
|
||||
with mock.patch.object(six, 'PY3', True):
|
||||
with mock.patch('gettext.translation') as translation:
|
||||
trans = mock.Mock()
|
||||
translation.return_value = trans
|
||||
trans.ungettext.side_effect = AssertionError(
|
||||
'should have called ngettext')
|
||||
tf = _factory.TranslatorFactory('domain')
|
||||
tf.plural_form('single', 'plural', 1)
|
||||
trans.ngettext.assert_called_with(
|
||||
'single', 'plural', 1)
|
||||
|
@ -13,7 +13,6 @@
|
||||
# under the License.
|
||||
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
|
||||
import oslo_i18n
|
||||
from oslo_i18n import _gettextutils
|
||||
@ -38,7 +37,7 @@ class TranslationFixtureTest(test_base.BaseTestCase):
|
||||
def test_immediate(self):
|
||||
msg = self.trans_fixture.immediate('this is a lazy message')
|
||||
self.assertNotIsInstance(msg, _message.Message)
|
||||
self.assertIsInstance(msg, six.text_type)
|
||||
self.assertIsInstance(msg, str)
|
||||
self.assertEqual(u'this is a lazy message', msg)
|
||||
|
||||
|
||||
|
@ -14,12 +14,12 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import builtins
|
||||
import gettext
|
||||
import logging
|
||||
from unittest import mock
|
||||
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
|
||||
from oslo_i18n import _factory
|
||||
from oslo_i18n import _gettextutils
|
||||
@ -49,8 +49,7 @@ class GettextTest(test_base.BaseTestCase):
|
||||
def test__gettextutils_install(self):
|
||||
_gettextutils.install('blaa')
|
||||
_lazy.enable_lazy(False)
|
||||
self.assertTrue(isinstance(self.t.primary('A String'),
|
||||
six.text_type))
|
||||
self.assertTrue(isinstance(self.t.primary('A String'), str))
|
||||
|
||||
_gettextutils.install('blaa')
|
||||
_lazy.enable_lazy(True)
|
||||
@ -68,10 +67,10 @@ class GettextTest(test_base.BaseTestCase):
|
||||
with mock.patch('os.environ.get') as environ_get:
|
||||
with mock.patch('gettext.install'):
|
||||
environ_get.return_value = '/foo/bar'
|
||||
if '_' in six.moves.builtins.__dict__:
|
||||
del six.moves.builtins.__dict__['_']
|
||||
if '_' in builtins.__dict__:
|
||||
del builtins.__dict__['_']
|
||||
_gettextutils.install('blaa')
|
||||
self.assertIn('_', six.moves.builtins.__dict__)
|
||||
self.assertIn('_', builtins.__dict__)
|
||||
|
||||
def test_get_available_languages(self):
|
||||
# Only the languages available for a specific translation domain
|
||||
|
@ -14,11 +14,11 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import io
|
||||
import logging
|
||||
from unittest import mock
|
||||
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
|
||||
from oslo_i18n import _message
|
||||
from oslo_i18n import log as i18n_log
|
||||
@ -32,7 +32,7 @@ class TranslationHandlerTestCase(test_base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TranslationHandlerTestCase, self).setUp()
|
||||
|
||||
self.stream = six.StringIO()
|
||||
self.stream = io.StringIO()
|
||||
self.destination_handler = logging.StreamHandler(self.stream)
|
||||
self.translation_handler = i18n_log.TranslationHandler('zh_CN')
|
||||
self.translation_handler.setTarget(self.destination_handler)
|
||||
|
@ -19,7 +19,6 @@ from unittest import mock
|
||||
import warnings
|
||||
|
||||
from oslotest import base as test_base
|
||||
import six
|
||||
import testtools
|
||||
|
||||
from oslo_i18n import _message
|
||||
@ -42,7 +41,7 @@ class MessageTestCase(test_base.BaseTestCase):
|
||||
|
||||
def test_message_is_unicode(self):
|
||||
message = _message.Message('some %s') % 'message'
|
||||
self.assertIsInstance(message, six.text_type)
|
||||
self.assertIsInstance(message, str)
|
||||
|
||||
@mock.patch('locale.getdefaultlocale')
|
||||
@mock.patch('gettext.translation')
|
||||
@ -67,7 +66,7 @@ class MessageTestCase(test_base.BaseTestCase):
|
||||
|
||||
def test_translation_returns_unicode(self):
|
||||
message = _message.Message('some %s') % 'message'
|
||||
self.assertIsInstance(message.translation(), six.text_type)
|
||||
self.assertIsInstance(message.translation(), str)
|
||||
|
||||
def test_mod_with_named_parameters(self):
|
||||
msgid = ("%(description)s\nCommand: %(cmd)s\n"
|
||||
@ -324,7 +323,7 @@ class MessageTestCase(test_base.BaseTestCase):
|
||||
# Here we are not testing the Message object directly but the result
|
||||
# of unicoding() an object whose unicode representation is a Message
|
||||
obj = utils.SomeObject(message)
|
||||
unicoded_obj = six.text_type(obj)
|
||||
unicoded_obj = str(obj)
|
||||
|
||||
self.assertEqual(es_translation, unicoded_obj.translation('es'))
|
||||
|
||||
@ -491,7 +490,7 @@ class MessageTestCase(test_base.BaseTestCase):
|
||||
expected_translation = es_translation % param
|
||||
|
||||
obj = utils.SomeObject(msg)
|
||||
unicoded_obj = six.text_type(obj)
|
||||
unicoded_obj = str(obj)
|
||||
|
||||
self.assertEqual(expected_translation, unicoded_obj.translation('es'))
|
||||
self.assertEqual(default_translation, unicoded_obj.translation('XX'))
|
||||
|
@ -12,7 +12,6 @@
|
||||
"""A few tests that use the public API to ensure the imports work."""
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import oslo_i18n
|
||||
from oslo_i18n import _lazy
|
||||
@ -24,8 +23,7 @@ class PublicAPITest(unittest.TestCase):
|
||||
oslo_i18n.TranslatorFactory('domain')
|
||||
|
||||
def test_install(self):
|
||||
with mock.patch('six.moves.builtins'):
|
||||
oslo_i18n.install('domain')
|
||||
oslo_i18n.install('domain')
|
||||
|
||||
def test_get_available_languages(self):
|
||||
oslo_i18n.get_available_languages('domains')
|
||||
|
@ -3,4 +3,3 @@
|
||||
# process, which may cause wedges in the gate later.
|
||||
|
||||
pbr!=2.1.0,>=2.0.0 # Apache-2.0
|
||||
six>=1.10.0 # MIT
|
||||
|
Loading…
Reference in New Issue
Block a user