# limitations under the License.
#
import collections
+from enum import IntFlag
import logging
import socket
import struct
import sys
-if sys.version_info <= (3, 4):
- from aenum import IntEnum # noqa: F401
-else:
- from enum import IntEnum # noqa: F401
+from . import vpp_format
-if sys.version_info <= (3, 6):
- from aenum import IntFlag # noqa: F401
-else:
-
- from enum import IntFlag # noqa: F401
-
-from . import vpp_format # noqa: E402
#
# Set log-level in application by doing e.g.:
# logger = logging.getLogger('vpp_serializer')
# logger.setLevel(logging.DEBUG)
#
-logger = logging.getLogger(__name__)
+logger = logging.getLogger('vpp_papi.serializer')
+
-if sys.version[0] == '2':
- def check(d): type(d) is dict
-else:
- def check(d): type(d) is dict or type(d) is bytes
+def check(d):
+ return type(d) is dict or type(d) is bytes
def conversion_required(data, field_type):
return vpp_format.conversion_unpacker_table[field_type](data)
-# TODO: post 20.01, remove inherit from object.
-class Packer(object):
+class Packer:
options = {}
def pack(self, data, kwargs):
raise NotImplementedError
# override as appropriate in subclasses
- def _get_packer_with_options(self, f_type, options):
+ @staticmethod
+ def _get_packer_with_options(f_type, options):
return types[f_type]
def get_packer_with_options(self, f_type, options):
if options is not None:
try:
- return self._get_packer_with_options(f_type, options)
+ c = types[f_type].__class__
+ return c._get_packer_with_options(f_type, options)
except IndexError:
raise VPPSerializerValueError(
"Options not supported for {}{} ({})".
- format(f_type, types[f_type].__class__,
- options))
+ format(f_type, types[f_type].__class__,
+ options))
class BaseTypes(Packer):
def __init__(self, type, elements=0, options=None):
+ self._type = type
+ self._elements = elements
base_types = {'u8': '>B',
'i8': '>b',
'string': '>s',
def unpack(self, data, offset, result=None, ntc=False):
return self.packer.unpack_from(data, offset)[0], self.packer.size
- def _get_packer_with_options(self, f_type, options):
- c = types[f_type].__class__
- return c(f_type, options=options)
+ @staticmethod
+ def _get_packer_with_options(f_type, options):
+ return BaseTypes(f_type, options=options)
+
+ def __repr__(self):
+ return "BaseTypes(type=%s, elements=%s, options=%s)" % (self._type,
+ self._elements,
+ self.options)
class String(Packer):
self.fixed = True if num else False
if self.fixed and not self.limit:
raise VPPSerializerValueError(
- "Invalid argument length for: {}, {} maximum {}".
- format(list, len(list), self.limit))
+ "Invalid combination for: {}, {} fixed:{} limit:{}".
+ format(name, options, self.fixed, self.limit))
def pack(self, list, kwargs=None):
if not list:
return '', 0
p = BaseTypes('u8', length)
x, size = p.unpack(data, offset + length_field_size)
- #x2 = x.split(b'\0', 1)[0]
return (x.decode('ascii', errors='replace'), size + length_field_size)
-types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'),
+types = {'u8': BaseTypes('u8'), 'i8': BaseTypes('i8'),
+ 'u16': BaseTypes('u16'), 'i16': BaseTypes('i16'),
'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'),
- 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'),
+ 'u64': BaseTypes('u64'), 'i64': BaseTypes('i64'),
+ 'f64': BaseTypes('f64'),
'bool': BaseTypes('bool'), 'string': String}
class_types = {}
.format(self.name, len(data[offset:]), self.num))
return self.packer.unpack(data, offset)
+ def __repr__(self):
+ return "FixedList_u8(name=%s, field_type=%s, num=%s)" % (
+ self.name, self.field_type, self.num
+ )
+
class FixedList(Packer):
def __init__(self, name, field_type, num):
total += size
return result, total
+ def __repr__(self):
+ return "FixedList(name=%s, field_type=%s, num=%s)" % (
+ self.name, self.field_type, self.num)
+
class VLAList(Packer):
def __init__(self, name, field_type, len_field_name, index):
total += size
return r, total
+ def __repr__(self):
+ return "VLAList(name=%s, field_type=%s, " \
+ "len_field_name=%s, index=%s)" % (
+ self.name, self.field_type, self.length_field, self.index
+ )
+
class VLAList_legacy(Packer):
def __init__(self, name, field_type):
+ self.name = name
+ self.field_type = field_type
self.packer = types[field_type]
self.size = self.packer.size
total += size
return r, total
+ def __repr__(self):
+ return "VLAList_legacy(name=%s, field_type=%s)" % (
+ self.name, self.field_type
+ )
+
+# Will change to IntEnum after 21.04 release
class VPPEnumType(Packer):
+ output_class = IntFlag
+
def __init__(self, name, msgdef, options=None):
self.size = types['u32'].size
self.name = name
continue
ename, evalue = f
e_hash[ename] = evalue
- self.enum = IntFlag(name, e_hash)
+ self.enum = self.output_class(name, e_hash)
types[name] = self
- class_types[name] = VPPEnumType
+ class_types[name] = self.__class__
self.options = options
def __getattr__(self, name):
def __bool__(self):
return True
- # TODO: Remove post 20.01.
- if sys.version[0] == '2':
- __nonzero__ = __bool__
-
def pack(self, data, kwargs=None):
if data is None: # Default to zero if not specified
if self.options and 'default' in self.options:
x, size = types[self.enumtype].unpack(data, offset)
return self.enum(x), size
- def _get_packer_with_options(self, f_type, options):
- c = types[f_type].__class__
- return c(f_type, types[f_type].msgdef, options=options)
+ @classmethod
+ def _get_packer_with_options(cls, f_type, options):
+ return cls(f_type, types[f_type].msgdef, options=options)
+
+ def __repr__(self):
+ return "%s(name=%s, msgdef=%s, options=%s)" % (
+ self.__class__.__name__, self.name, self.msgdef, self.options
+ )
+
+
+class VPPEnumFlagType(VPPEnumType):
+ output_class = IntFlag
+
+ def __init__(self, name, msgdef, options=None):
+ super(VPPEnumFlagType, self).__init__(name, msgdef, options)
class VPPUnionType(Packer):
def __init__(self, name, msgdef):
self.name = name
+ self.msgdef = msgdef
self.size = 0
self.maxindex = 0
fields = []
r.append(x)
return self.tuple._make(r), maxsize
+ def __repr__(self):
+ return"VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef)
+
class VPPTypeAlias(Packer):
def __init__(self, name, msgdef, options=None):
return self.packer.pack(data, kwargs)
- def _get_packer_with_options(self, f_type, options):
- c = types[f_type].__class__
- return c(f_type, types[f_type].msgdef, options=options)
+ @staticmethod
+ def _get_packer_with_options(f_type, options):
+ return VPPTypeAlias(f_type, types[f_type].msgdef, options=options)
def unpack(self, data, offset=0, result=None, ntc=False):
if ntc is False and self.name in vpp_format.conversion_unpacker_table:
return conversion_unpacker(t, self.name), size
return t, size
+ def __repr__(self):
+ return "VPPTypeAlias(name=%s, msgdef=%s, options=%s)" % (
+ self.name, self.msgdef, self.options)
+
class VPPType(Packer):
# Set everything up to be able to pack / unpack
t = conversion_unpacker(t, self.name)
return t, total
+ def __repr__(self):
+ return "%s(name=%s, msgdef=%s)" % (
+ self.__class__.__name__, self.name, self.msgdef
+ )
+
class VPPMessage(VPPType):
pass