python3: Store AsciiStringType class data as str

AsciiStringType data are mostly IP addresses and they must be
text_type in python3 for text_to_bin to work.
Also introduce AsciiStringListType, which is suitable for lists of
IP addresses.

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-07-03 11:27:06 +09:00 committed by FUJITA Tomonori
parent 1bb849b304
commit 7da17ac941

View File

@ -60,10 +60,17 @@ class TypeDescr(object):
class AsciiStringType(TypeDescr):
@staticmethod
def encode(v):
# TODO: AsciiStringType data should probably be stored as
# text_type in class data. This isinstance() check exists
# because OFPDescStats violates this.
if six.PY3 and isinstance(v, six.text_type):
return v
return six.text_type(v, 'ascii')
@staticmethod
def decode(v):
if six.PY3:
return v
return v.encode('ascii')
@ -77,6 +84,16 @@ class Utf8StringType(TypeDescr):
return v.encode('utf-8')
class AsciiStringListType(TypeDescr):
@staticmethod
def encode(v):
return [AsciiStringType.encode(x) for x in v]
@staticmethod
def decode(v):
return [AsciiStringType.decode(x) for x in v]
class NXFlowSpecFieldType(TypeDescr):
# ("field_name", 0) <-> ["field_name", 0]
@ -98,6 +115,7 @@ class NXFlowSpecFieldType(TypeDescr):
_types = {
'ascii': AsciiStringType,
'utf-8': Utf8StringType,
'asciilist': AsciiStringListType,
'nx-flow-spec-field': NXFlowSpecFieldType, # XXX this should not be here
}
@ -112,12 +130,13 @@ class StringifyMixin(object):
Currently the following types are implemented.
===== ==========
Type Descrption
===== ==========
ascii US-ASCII
utf-8 UTF-8
===== ==========
========= =============
Type Descrption
========= =============
ascii US-ASCII
utf-8 UTF-8
asciilist list of ascii
========= =============
Example::
_TYPE = {