API: Add support for type aliases
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_papi.py
index e1a7059..bd2682f 100644 (file)
@@ -27,7 +27,8 @@ import fnmatch
 import weakref
 import atexit
 from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType, BaseTypes
-from . vpp_serializer import VPPMessage
+from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias
+from . vpp_format import VPPFormat
 
 if sys.version[0] == '2':
     import Queue as queue
@@ -35,6 +36,19 @@ else:
     import queue as queue
 
 
+class VppEnumType(type):
+    def __getattr__(cls, name):
+        t = vpp_get_type(name)
+        return t.enum
+
+
+# Python3
+# class VppEnum(metaclass=VppEnumType):
+#    pass
+class VppEnum(object):
+    __metaclass__ = VppEnumType
+
+
 def vpp_atexit(vpp_weakref):
     """Clean up VPP connection on shutdown."""
     vpp_instance = vpp_weakref()
@@ -63,7 +77,7 @@ class FuncWrapper(object):
         return self._func(**kwargs)
 
 
-class VPP():
+class VPP(object):
     """VPP interface.
 
     This class provides the APIs to VPP.  The APIs are loaded
@@ -88,27 +102,35 @@ class VPP():
         for t in api['types']:
             t[0] = 'vl_api_' + t[0] + '_t'
             types[t[0]] = {'type': 'type', 'data': t}
+        for t, v in api['aliases'].items():
+            types['vl_api_' + t + '_t'] = {'type': 'alias', 'data': v}
 
         i = 0
         while True:
             unresolved = {}
             for k, v in types.items():
                 t = v['data']
-                if v['type'] == 'enum':
-                    try:
-                        VPPEnumType(t[0], t[1:])
-                    except ValueError:
-                        unresolved[k] = v
-                elif v['type'] == 'union':
-                    try:
-                        VPPUnionType(t[0], t[1:])
-                    except ValueError:
-                        unresolved[k] = v
-                elif v['type'] == 'type':
-                    try:
-                        VPPType(t[0], t[1:])
-                    except ValueError:
-                        unresolved[k] = v
+                if not vpp_get_type(k):
+                    if v['type'] == 'enum':
+                        try:
+                            VPPEnumType(t[0], t[1:])
+                        except ValueError:
+                            unresolved[k] = v
+                    elif v['type'] == 'union':
+                        try:
+                            VPPUnionType(t[0], t[1:])
+                        except ValueError:
+                            unresolved[k] = v
+                    elif v['type'] == 'type':
+                        try:
+                            VPPType(t[0], t[1:])
+                        except ValueError:
+                            unresolved[k] = v
+                    elif v['type'] == 'alias':
+                        try:
+                            VPPTypeAlias(k, t)
+                        except ValueError:
+                            unresolved[k] = v
             if len(unresolved) == 0:
                 break
             if i > 3:
@@ -124,7 +146,7 @@ class VPP():
                 self.logger.error('Not implemented error for {}'.format(m[0]))
 
     def __init__(self, apifiles=None, testmode=False, async_thread=True,
-                 logger=logging.getLogger('vpp_papi'), loglevel='debug',
+                 logger=None, loglevel=None,
                  read_timeout=5, use_socket=False,
                  server_address='/run/vpp-api.sock'):
         """Create a VPP API object.
@@ -200,6 +222,9 @@ class VPP():
                 return self.context
     get_context = ContextId()
 
+    def get_type(self, name):
+        return vpp_get_type(name)
+
     @classmethod
     def find_api_dir(cls):
         """Attempt to find the best directory in which API definition