Fix Python 3 issues

* Replace unicode with six.text_type
* Replace xrange() with range() in tests, performances don't matter
  there
* Get StringIO from six
* Serializer.print_to_output(): on Python 3, don't encode Unicode to
  UTF-8 before calling print(). On Python 3, print() expects Unicode
  strings.

This patch was initially generated by the sixer tool using
operations: stringio, unicode, xrange.

Change-Id: Ifef87e6feac47286f3858800bcd11125f34d4c20
This commit is contained in:
Victor Stinner 2015-09-10 12:04:46 +02:00
parent b7a8eff49d
commit 1a69bb67dd
7 changed files with 19 additions and 16 deletions

View File

@ -22,6 +22,8 @@ import sys
from time import sleep
import urllib2
import six
from fuelclient.cli.error import DeployProgressError
from fuelclient.cli.error import exit_with_error
@ -54,7 +56,7 @@ def format_table(data, acceptable_keys=None, column_to_join=None):
)
for row in rows:
column_widths.update(
(index, max(column_widths[index], len(unicode(element))))
(index, max(column_widths[index], len(six.text_type(element))))
for index, element in enumerate(row)
)
row_template = u' | '.join(
@ -66,7 +68,7 @@ def format_table(data, acceptable_keys=None, column_to_join=None):
(row_template.format(*header),
u'-|-'.join(column_widths[column_index] * u'-'
for column_index in range(number_of_columns)),
u'\n'.join(row_template.format(*map(unicode, x))
u'\n'.join(row_template.format(*map(six.text_type, x))
for x in rows))
)

View File

@ -18,6 +18,7 @@ from itertools import imap
import json
import os
import six
import yaml
from fuelclient.cli import error
@ -78,7 +79,7 @@ class Serializer(object):
if self.format_flags:
self.print_formatted(formatted_data)
else:
if isinstance(arg, unicode):
if six.PY2 and isinstance(arg, six.text_type):
arg = arg.encode('utf-8')
print_method(arg)

View File

@ -26,9 +26,9 @@ import subprocess
import sys
import tempfile
from StringIO import StringIO
import mock
from six import StringIO
from fuelclient.cli.parser import main

View File

@ -14,10 +14,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO
import mock
import requests_mock
from six import moves
from fuelclient.objects.environment import Environment
from fuelclient.tests import base
@ -34,7 +34,7 @@ class TestEnvironment(base.UnitTestCase):
m_requests.get(url, json={'id': cluster_id, 'status': 'operational'})
m_delete = m_requests.delete(url)
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.execute(cmd.split())
self.assertIn('--force', m_stdout.getvalue())
@ -52,7 +52,7 @@ class TestEnvironment(base.UnitTestCase):
m_requests.get('/api/v1/clusters/{0}/'.format(cluster_id),
json=cluster_data)
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.execute(
'fuel env create --name test --rel 1 --network-mode nova'
.split()
@ -73,7 +73,7 @@ class TestEnvironment(base.UnitTestCase):
m_requests.get('/api/v1/clusters/{0}/'.format(cluster_id),
json=cluster_data)
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.execute(
'fuel env create --name test --rel 1 --nst gre'
.split()
@ -111,7 +111,7 @@ class TestEnvironment(base.UnitTestCase):
m_requests.get('/api/v1/clusters/{0}/'.format(cluster_id),
json=cluster_data)
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.execute('fuel env create'
' --name test --rel 1 --mode multinode'.split())

View File

@ -14,9 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO
import mock
from six import moves
from fuelclient.tests.unit.v2.cli import test_engine
from fuelclient.tests.utils import fake_env
@ -64,7 +64,7 @@ class TestEnvCommand(test_engine.BaseCLITest):
def test_nova_net_deprecation_warning(self):
args = 'env create -r 1 -n nova env42'
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.exec_command(args)
self.assertIn(
'WARNING: nova-network is deprecated since 6.1 release',
@ -74,7 +74,7 @@ class TestEnvCommand(test_engine.BaseCLITest):
def test_neutron_gre_deprecation_warning(self):
args = 'env create -r 1 -n neutron -nst gre env42'
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.exec_command(args)
self.assertIn(
"WARNING: GRE network segmentation type is deprecated "
@ -95,7 +95,7 @@ class TestEnvCommand(test_engine.BaseCLITest):
env = fake_env.get_fake_env(status='operational')
self.m_client.get_by_id.return_value = env
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.exec_command(args)
self.assertIn('--force', m_stdout.getvalue())

View File

@ -14,8 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO
import mock
from six import moves
import yaml
from fuelclient.tests.unit.v2.cli import test_engine
@ -33,7 +33,7 @@ class TestFuelVersionCommand(test_engine.BaseCLITest):
def test_fuel_version(self):
args = 'fuel-version'
with mock.patch('sys.stdout', new=cStringIO.StringIO()) as m_stdout:
with mock.patch('sys.stdout', new=moves.cStringIO()) as m_stdout:
self.exec_command(args)
self.assertEqual(fake_fuel_version.get_fake_fuel_version(),
yaml.safe_load(m_stdout.getvalue()))

View File

@ -35,7 +35,7 @@ def random_string(lenght, prefix='', postfix='', charset=None):
charset = charset or string.ascii_letters + string.digits
base_length = lenght - (len(prefix) + len(postfix))
base = ''.join([str(random.choice(charset)) for i in xrange(base_length)])
base = ''.join([str(random.choice(charset)) for i in range(base_length)])
return '{prefix}{base}{postfix}'.format(prefix=prefix,
postfix=postfix,