Python API: Preparation for RPM/DEB packaging.
[vpp.git] / vpp-api / python / vpp_papi / vpp_papi.py
index 174e61d..42ddcdc 100644 (file)
@@ -20,15 +20,15 @@ from __future__ import print_function
 import signal, os, sys
 from struct import *
 
-scriptdir = os.path.dirname(os.path.realpath(__file__))
-sys.path.append(scriptdir)
 import vpp_api
 from vpp_api_base import *
 
 # Import API definitions. The core VPE API is imported into main namespace
 import memclnt
+
+# Cheating a bit, importing it into this namespace as well as a module.
+import vpe
 from vpe import *
-vpe = sys.modules['vpe']
 
 def eprint(*args, **kwargs):
     print(*args, file=sys.stderr, **kwargs)
@@ -67,7 +67,7 @@ def msg_handler(msg):
     #
     # Collect results until control ping
     #
-    if id[0] == vpe.VL_API_CONTROL_PING_REPLY:
+    if id[0] == VL_API_CONTROL_PING_REPLY:
         results_event_set(context)
         waiting_for_reply_clear()
         return
@@ -82,7 +82,14 @@ def msg_handler(msg):
     results_event_set(context)
     waiting_for_reply_clear()
 
+def handler(signum, frame):
+    print('Signal handler called with signal', signum)
+    raise IOError("Couldn't connect to VPP!")
+
 def connect(name):
+    # Set the signal handler
+    signal.signal(signal.SIGALRM, handler)
+
     signal.alarm(3) # 3 second
     rv = vpp_api.connect(name, msg_handler)
     signal.alarm(0)
@@ -90,20 +97,16 @@ def connect(name):
     #
     # Assign message id space for plugins
     #
-    plugin_map_plugins()
-
+    try:
+        plugin_map_plugins()
+    except:
+        return -1
     return rv
 
 def disconnect():
     rv = vpp_api.disconnect()
     return rv
 
-# CLI convenience wrapper
-def cli_exec(cmd):
-    cmd += '\n'
-    r = cli_inband(len(cmd), cmd)
-    return r.reply[0].decode().rstrip('\x00')
-
 def register_event_callback(callback):
     event_callback_set(callback)
 
@@ -112,6 +115,7 @@ def plugin_name_to_id(plugin, name_to_id_table, base):
         m = globals()[plugin]
     except KeyError:
         m = sys.modules[plugin]
+
     for name in name_to_id_table:
         setattr(m, name, name_to_id_table[name] + base)
 
@@ -126,11 +130,10 @@ def plugin_map_plugins():
         #
         version = plugins[p]['version']
         name = p + '_' + format(version, '08x')
-        r = memclnt.get_first_msg_id(name.encode('ascii'))
-        ## TODO: Add error handling / raise exception
+        r = memclnt.get_first_msg_id(name)
         if r.retval != 0:
-            eprint('Failed getting first msg id for:', p)
-            continue
+            eprint('Failed getting first msg id for:', p, r, name)
+            raise
 
         # Set base
         base = r.first_msg_id
@@ -153,6 +156,8 @@ def plugin_map_plugins():
 #
 memclnt.msg_id_base_set(1)
 plugins['memclnt']['base'] = 1
+
+# vpe
 msg_id_base_set(len(plugins['memclnt']['func_table']) + 1)
 plugins['vpe']['base'] = len(plugins['memclnt']['func_table']) + 1
 api_func_table = []
@@ -160,3 +165,4 @@ api_func_table.append(None)
 api_func_table[1:] = plugins['memclnt']['func_table'] + plugins['vpe']['func_table']
 plugin_name_to_id('memclnt', plugins['memclnt']['name_to_id_table'], 1)
 plugin_name_to_id('vpe', plugins['vpe']['name_to_id_table'], plugins['vpe']['base'])
+plugin_name_to_id(__name__, plugins['vpe']['name_to_id_table'], plugins['vpe']['base'])