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=6531514569d8b08242c738c61d3453468a44f7ca;hp=b0be8c81e696215578cfc68965b6ac7ac1468a50;hpb=da47f67be23ebadc6102d359dd971ca9d9e4e33e;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 b0be8c81e69..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): @@ -148,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: @@ -272,7 +261,7 @@ class FixedList(Packer): return result, total def __repr__(self): - return "FixedList_(name=%s, field_type=%s, num=%s)" % ( + return "FixedList(name=%s, field_type=%s, num=%s)" % ( self.name, self.field_type, self.num) @@ -367,7 +356,10 @@ class VLAList_legacy(Packer): ) +# 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 @@ -382,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): @@ -393,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: @@ -410,16 +398,23 @@ class VPPEnumType(Packer): x, size = types[self.enumtype].unpack(data, offset) return self.enum(x), size - @staticmethod - def _get_packer_with_options(f_type, options): - return VPPEnumType(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 "VPPEnumType(name=%s, msgdef=%s, options=%s)" % ( - self.name, self.msgdef, self.options + 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