papi: fix typo in repr
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_serializer.py
index 8361cd3..0929487 100644 (file)
@@ -77,22 +77,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 +128,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):
@@ -168,13 +177,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 +236,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 +271,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 +323,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,6 +361,11 @@ 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
+        )
+
 
 class VPPEnumType(Packer):
     def __init__(self, name, msgdef, options=None):
@@ -378,14 +410,20 @@ 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)
+    @staticmethod
+    def _get_packer_with_options(f_type, options):
+        return VPPEnumType(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
+        )
 
 
 class VPPUnionType(Packer):
     def __init__(self, name, msgdef):
         self.name = name
+        self.msgdef = msgdef
         self.size = 0
         self.maxindex = 0
         fields = []
@@ -432,6 +470,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 +513,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 +528,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 +654,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