summaryrefslogtreecommitdiffstats
path: root/src/vpp-api/python
diff options
context:
space:
mode:
Diffstat (limited to 'src/vpp-api/python')
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_papi.py49
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_serializer.py10
2 files changed, 43 insertions, 16 deletions
diff --git a/src/vpp-api/python/vpp_papi/vpp_papi.py b/src/vpp-api/python/vpp_papi/vpp_papi.py
index e1a7059f317..5e98f92cecd 100644
--- a/src/vpp-api/python/vpp_papi/vpp_papi.py
+++ b/src/vpp-api/python/vpp_papi/vpp_papi.py
@@ -27,7 +27,7 @@ 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
if sys.version[0] == '2':
import Queue as queue
@@ -35,6 +35,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:
+ __metaclass__ = VppEnumType
+
+
def vpp_atexit(vpp_weakref):
"""Clean up VPP connection on shutdown."""
vpp_instance = vpp_weakref()
@@ -94,21 +107,22 @@ class VPP():
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(t[0]):
+ 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 len(unresolved) == 0:
break
if i > 3:
@@ -200,6 +214,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
diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py
index cac6dd61791..240912d96ba 100644
--- a/src/vpp-api/python/vpp_papi/vpp_serializer.py
+++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py
@@ -62,6 +62,13 @@ types['f64'] = BaseTypes('f64')
types['bool'] = BaseTypes('bool')
+def vpp_get_type(name):
+ try:
+ return types[name]
+ except KeyError:
+ return None
+
+
class FixedList_u8():
def __init__(self, name, field_type, num):
self.name = name
@@ -202,6 +209,9 @@ class VPPEnumType():
def __getattr__(self, name):
return self.enum[name]
+ def __nonzero__(self):
+ return True
+
def pack(self, data, kwargs=None):
return types['u32'].pack(data, kwargs)