X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvpp-api%2Fpython%2Fvpp_papi%2Fvpp_serializer.py;h=644aeac65c63a7747e9382b760b7f54c2a08b415;hb=3825d93af;hp=8361cd3c2666293285bc696a6956f6c0ac66ea23;hpb=418ebb711e7f32a160f824cee7de0122076d216a;p=vpp.git diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index 8361cd3c266..644aeac65c6 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -13,35 +13,25 @@ # 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): @@ -66,8 +56,7 @@ def conversion_unpacker(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): @@ -77,22 +66,26 @@ class Packer(object): 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', @@ -124,9 +117,14 @@ class BaseTypes(Packer): 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): @@ -139,8 +137,8 @@ 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: @@ -168,13 +166,14 @@ class String(Packer): 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 = {} @@ -226,6 +225,11 @@ class FixedList_u8(Packer): .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): @@ -256,6 +260,10 @@ class FixedList(Packer): 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): @@ -304,9 +312,17 @@ class VLAList(Packer): 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 @@ -334,8 +350,16 @@ class VLAList_legacy(Packer): 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 @@ -350,9 +374,9 @@ class VPPEnumType(Packer): 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): @@ -361,10 +385,6 @@ class VPPEnumType(Packer): 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: @@ -378,14 +398,27 @@ class VPPEnumType(Packer): 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 = [] @@ -432,6 +465,9 @@ class VPPUnionType(Packer): 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): @@ -472,9 +508,9 @@ class VPPTypeAlias(Packer): 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: @@ -487,6 +523,10 @@ class VPPTypeAlias(Packer): 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 @@ -609,6 +649,11 @@ class VPPType(Packer): 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