python3: Use six.text_type instead of unicode

This is the partial patch to supporting python 3.
unicode was removed in python 3. So we use six.text_type instead of unicode.

Signed-off-by: Fumihiko Kakuma <kakuma@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
fumihiko kakuma 2015-06-24 14:05:18 +09:00 committed by FUJITA Tomonori
parent f65ecadfaa
commit 267bcda006
9 changed files with 22 additions and 13 deletions

View File

@ -19,6 +19,7 @@ import itertools
import logging import logging
import operator import operator
import os import os
import six
import sys import sys
import weakref import weakref
@ -761,7 +762,7 @@ class VSCtlContext(object):
for ovsrec_row in self.idl.tables[ for ovsrec_row in self.idl.tables[
vsctl_row_id.table].rows.values(): vsctl_row_id.table].rows.values():
name = getattr(ovsrec_row, vsctl_row_id.name_column) name = getattr(ovsrec_row, vsctl_row_id.name_column)
assert type(name) in (list, str, unicode) assert type(name) in (list, str, six.text_type)
if type(name) != list and name == record_id: if type(name) != list and name == record_id:
if (referrer): if (referrer):
vsctl_fatal('multiple rows in %s match "%s"' % vsctl_fatal('multiple rows in %s match "%s"' %

View File

@ -19,6 +19,7 @@
import base64 import base64
import collections import collections
import inspect import inspect
import six
# Some arguments to __init__ is mungled in order to avoid name conflicts # Some arguments to __init__ is mungled in order to avoid name conflicts
@ -59,7 +60,7 @@ class TypeDescr(object):
class AsciiStringType(TypeDescr): class AsciiStringType(TypeDescr):
@staticmethod @staticmethod
def encode(v): def encode(v):
return unicode(v, 'ascii') return six.text_type(v, 'ascii')
@staticmethod @staticmethod
def decode(v): def decode(v):
@ -69,7 +70,7 @@ class AsciiStringType(TypeDescr):
class Utf8StringType(TypeDescr): class Utf8StringType(TypeDescr):
@staticmethod @staticmethod
def encode(v): def encode(v):
return unicode(v, 'utf-8') return six.text_type(v, 'utf-8')
@staticmethod @staticmethod
def decode(v): def decode(v):
@ -154,7 +155,7 @@ class StringifyMixin(object):
if len(dict_) != 1: if len(dict_) != 1:
return False return False
k = list(dict_.keys())[0] k = list(dict_.keys())[0]
if not isinstance(k, (bytes, unicode)): if not isinstance(k, (bytes, six.text_type)):
return False return False
for p in cls._class_prefixes: for p in cls._class_prefixes:
if k.startswith(p): if k.startswith(p):
@ -186,7 +187,7 @@ class StringifyMixin(object):
@classmethod @classmethod
def _get_default_encoder(cls, encode_string): def _get_default_encoder(cls, encode_string):
def _encode(v): def _encode(v):
if isinstance(v, (bytes, unicode)): if isinstance(v, (bytes, six.text_type)):
json_value = encode_string(v) json_value = encode_string(v)
elif isinstance(v, list): elif isinstance(v, list):
json_value = map(_encode, v) json_value = map(_encode, v)
@ -268,7 +269,7 @@ class StringifyMixin(object):
@classmethod @classmethod
def _get_default_decoder(cls, decode_string): def _get_default_decoder(cls, decode_string):
def _decode(json_value, **additional_args): def _decode(json_value, **additional_args):
if isinstance(json_value, (bytes, unicode)): if isinstance(json_value, (bytes, six.text_type)):
v = decode_string(json_value) v = decode_string(json_value)
elif isinstance(json_value, list): elif isinstance(json_value, list):
v = map(_decode, json_value) v = map(_decode, json_value)

View File

@ -18,6 +18,7 @@
Decoder/Encoder implementations of OpenFlow 1.2. Decoder/Encoder implementations of OpenFlow 1.2.
""" """
import six
import struct import struct
import itertools import itertools
@ -1481,7 +1482,7 @@ class OFPActionSetField(OFPAction):
assert len(kwargs) == 1 assert len(kwargs) == 1
key = list(kwargs.keys())[0] key = list(kwargs.keys())[0]
value = kwargs[key] value = kwargs[key]
assert isinstance(key, (str, unicode)) assert isinstance(key, (str, six.text_type))
assert not isinstance(value, tuple) # no mask assert not isinstance(value, tuple) # no mask
self.key = key self.key = key
self.value = value self.value = value

View File

@ -40,6 +40,7 @@ The following extensions are not implemented yet.
- EXT-192-v Vacancy events Extension - EXT-192-v Vacancy events Extension
""" """
import six
import struct import struct
import itertools import itertools
@ -3063,7 +3064,7 @@ class OFPActionSetField(OFPAction):
assert len(kwargs) == 1 assert len(kwargs) == 1
key = list(kwargs.keys())[0] key = list(kwargs.keys())[0]
value = kwargs[key] value = kwargs[key]
assert isinstance(key, (str, unicode)) assert isinstance(key, (str, six.text_type))
assert not isinstance(value, tuple) # no mask assert not isinstance(value, tuple) # no mask
self.key = key self.key = key
self.value = value self.value = value

View File

@ -18,6 +18,7 @@
Decoder/Encoder implementations of OpenFlow 1.4. Decoder/Encoder implementations of OpenFlow 1.4.
""" """
import six
import struct import struct
import itertools import itertools
@ -5357,7 +5358,7 @@ class OFPActionSetField(OFPAction):
assert len(kwargs) == 1 assert len(kwargs) == 1
key = list(kwargs.keys())[0] key = list(kwargs.keys())[0]
value = kwargs[key] value = kwargs[key]
assert isinstance(key, (str, unicode)) assert isinstance(key, (str, six.text_type))
assert not isinstance(value, tuple) # no mask assert not isinstance(value, tuple) # no mask
self.key = key self.key = key
self.value = value self.value = value

View File

@ -18,6 +18,7 @@
Decoder/Encoder implementations of OpenFlow 1.5. Decoder/Encoder implementations of OpenFlow 1.5.
""" """
import six
import struct import struct
import itertools import itertools
@ -5467,7 +5468,7 @@ class OFPActionSetField(OFPAction):
assert len(kwargs) == 1 assert len(kwargs) == 1
key = list(kwargs.keys())[0] key = list(kwargs.keys())[0]
value = kwargs[key] value = kwargs[key]
assert isinstance(key, (str, unicode)) assert isinstance(key, (str, six.text_type))
assert not isinstance(value, tuple) # no mask assert not isinstance(value, tuple) # no mask
self.key = key self.key = key
self.value = value self.value = value

View File

@ -3,6 +3,7 @@ import json
import logging import logging
import pprint import pprint
import re import re
import six
(STATUS_OK, STATUS_ERROR) = range(2) (STATUS_OK, STATUS_ERROR) = range(2)
@ -109,7 +110,7 @@ class Command(object):
if resp.status == STATUS_OK: if resp.status == STATUS_OK:
if type(resp.value) in (str, bool, int, float, unicode): if type(resp.value) in (str, bool, int, float, six.text_type):
return str(resp.value) return str(resp.value)
ret = '' ret = ''

View File

@ -1,5 +1,6 @@
import json import json
import logging import logging
import six
import time import time
from datetime import datetime from datetime import datetime
@ -15,7 +16,7 @@ class ApgwFormatter(logging.Formatter):
'timestamp': datetime.utcfromtimestamp( 'timestamp': datetime.utcfromtimestamp(
time.time() time.time()
).strftime(self.LOG_TIME_FORMAT), ).strftime(self.LOG_TIME_FORMAT),
'msg': unicode(record.msg), 'msg': six.text_type(record.msg),
'level': record.levelname 'level': record.levelname
} }

View File

@ -21,6 +21,7 @@ import math
import netaddr import netaddr
import os import os
import signal import signal
import six
import sys import sys
import time import time
import traceback import traceback
@ -1326,7 +1327,7 @@ class TestFile(stringify.StringifyMixin):
try: try:
json_list = json.loads(buf) json_list = json.loads(buf)
for test_json in json_list: for test_json in json_list:
if isinstance(test_json, unicode): if isinstance(test_json, six.text_type):
self.description = test_json self.description = test_json
else: else:
self._normalize_test_json(test_json) self._normalize_test_json(test_json)