[svn-commits] russell: branch russell/bindings r103320 - in /team/russell/bindings: binding...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Feb 11 14:43:24 CST 2008


Author: russell
Date: Mon Feb 11 14:43:23 2008
New Revision: 103320

URL: http://svn.digium.com/view/asterisk?view=rev&rev=103320
Log:
application interface works, but doesn't let you do much useful right now ...

exten => 123,1,Python(ast_channel_test)

(ast_channel test) Channel is Console/default, currently executing default,580,2.

$ cat /var/lib/asterisk/python/ast_channel_test.py
import ast_channel

def ast_app(chan):
	print '(ast_channel test) Channel is ' + chan.get_name() + ', currently executing ' \
		+ chan.get_context() + ',' + chan.get_exten() + ',' + repr(chan.get_priority()) + '.'

Added:
    team/russell/bindings/bindings/examples/ast_channel_test.py   (with props)
Modified:
    team/russell/bindings/bindings/ast_channel.i
    team/russell/bindings/bindings/examples/cdr_csv.py
    team/russell/bindings/res/res_python.c
    team/russell/bindings/res/res_python/channel.c
    team/russell/bindings/res/res_python/res_python.h

Modified: team/russell/bindings/bindings/ast_channel.i
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/ast_channel.i?view=diff&rev=103320&r1=103319&r2=103320
==============================================================================
--- team/russell/bindings/bindings/ast_channel.i (original)
+++ team/russell/bindings/bindings/ast_channel.i Mon Feb 11 14:43:23 2008
@@ -8,10 +8,52 @@
 %}
 
 struct ast_channel {
+    /* This is an opaque type as far as this interface is concerned */
 };
 
 %extend ast_channel {
-    const char *get_name(void) {
+    const char *get_name(void)
+    {
         return $self->name;
     }
+
+    const char *get_uniqueid(void)
+    {
+        return $self->uniqueid;
+    }
+
+    const char *get_language(void)
+    {
+        return $self->language;
+    }
+
+    const char *get_context(void)
+    {
+        return $self->context;
+    }
+
+    const char *get_exten(void)
+    {
+        return $self->exten;
+    }
+
+    int get_priority(void)
+    {
+        return $self->priority;
+    }
+
+    const char *get_macro_context(void)
+    {
+        return $self->macrocontext;
+    }
+
+    const char *get_macro_exten(void)
+    {
+        return $self->macroexten;
+    }
+
+    int get_macro_priority(void)
+    {
+        return $self->macropriority;
+    }
 };

Added: team/russell/bindings/bindings/examples/ast_channel_test.py
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/examples/ast_channel_test.py?view=auto&rev=103320
==============================================================================
--- team/russell/bindings/bindings/examples/ast_channel_test.py (added)
+++ team/russell/bindings/bindings/examples/ast_channel_test.py Mon Feb 11 14:43:23 2008
@@ -1,0 +1,5 @@
+import ast_channel
+
+def ast_app(chan):
+	print '(ast_channel test) Channel is ' + chan.get_name() + ', currently executing ' \
+		+ chan.get_context() + ',' + chan.get_exten() + ',' + repr(chan.get_priority()) + '.'

Propchange: team/russell/bindings/bindings/examples/ast_channel_test.py
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/bindings/bindings/examples/ast_channel_test.py
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/bindings/bindings/examples/ast_channel_test.py
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/bindings/bindings/examples/cdr_csv.py
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/examples/cdr_csv.py?view=diff&rev=103320&r1=103319&r2=103320
==============================================================================
--- team/russell/bindings/bindings/examples/cdr_csv.py (original)
+++ team/russell/bindings/bindings/examples/cdr_csv.py Mon Feb 11 14:43:23 2008
@@ -7,7 +7,6 @@
 '''
 
 import ast_cdr
-import string
 import time
 
 def csv_val(val):

Modified: team/russell/bindings/res/res_python.c
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/res_python.c?view=diff&rev=103320&r1=103319&r2=103320
==============================================================================
--- team/russell/bindings/res/res_python.c (original)
+++ team/russell/bindings/res/res_python.c Mon Feb 11 14:43:23 2008
@@ -119,7 +119,7 @@
 	/* Is there a better way to do this?  If so, I don't know it ... */
 	setenv("PYTHONPATH", path, 0);
 
-#ifdef __Darwin__ /* XXX Fix this check */
+#ifdef __Darwin__ /* XXX Fix this check ... InitializeEx() was new in Python 2.4 i think */
 	Py_Initialize();
 #else
 	Py_InitializeEx(INIT_SIGS);
@@ -146,3 +146,4 @@
 	.unload = unload_module,
 	.reload = reload_module,
 );
+struct ast_module_info *python_module_info = &__mod_info;

Modified: team/russell/bindings/res/res_python/channel.c
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/res_python/channel.c?view=diff&rev=103320&r1=103319&r2=103320
==============================================================================
--- team/russell/bindings/res/res_python/channel.c (original)
+++ team/russell/bindings/res/res_python/channel.c Mon Feb 11 14:43:23 2008
@@ -27,8 +27,6 @@
 
 #include "asterisk.h"
 
-#include "asterisk/cdr.h"
-#include "asterisk/cli.h"
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
 #include "asterisk/channel.h"
@@ -36,15 +34,89 @@
 #include "res_python.h"
 #include "../bindings/swigpyrun.h"
 
-static const char python_app[] = "Python";
-static const char python_synopsis[] = "Channel control via a Python application";
-static const char python_desc[] =
+static char python_app[] = "Python";
+static char python_synopsis[] = "Channel control via a Python application";
+static char python_desc[] =
 "Python(<python module>):\n"
 "   Do stuff!\n"
 "";
 
 static int python_exec(struct ast_channel *chan, void *data)
 {
+	PyThreadState *my_thread_state;
+	PyObject *func_obj = NULL, *mod_obj = NULL, *fn_obj = NULL,
+		*channel_obj = NULL, *args_obj = NULL, *res_obj = NULL;
+	swig_type_info *ast_channel_type_info = NULL;
+	const char *mod_name = data;
+
+	PyEval_AcquireLock();
+
+	my_thread_state = PyThreadState_New(main_thread_state->interp);
+	PyThreadState_Swap(my_thread_state);
+
+	fn_obj = PyString_FromString(mod_name);
+	if (!fn_obj) {
+		ast_log(LOG_ERROR, "creating a python string failed\n");
+		goto cleanup;
+	}
+
+	mod_obj = PyImport_Import(fn_obj);
+	if (!mod_obj) {
+		ast_log(LOG_ERROR, "Failed to open python module (%s)\n", mod_name);
+		if (PyErr_Occurred())
+			PyErr_Print();
+		goto cleanup;
+	}
+
+	func_obj = PyObject_GetAttrString(mod_obj, "ast_app");
+	if (!func_obj) {
+		ast_log(LOG_ERROR, "Did not find ast_app in module '%s'\n", mod_name);
+		goto cleanup;
+	}
+
+	if (!PyCallable_Check(func_obj)) {
+		ast_log(LOG_ERROR, "ast_app found in '%s' but not callable\n", mod_name);
+		goto cleanup;
+	}
+
+	ast_channel_type_info = SWIG_TypeQuery("_p_ast_channel");
+	if (!ast_channel_type_info) {
+		ast_log(LOG_ERROR, "Failed to look up ast_channel type\n");
+		goto cleanup;
+	}
+
+	channel_obj = SWIG_NewPointerObj(chan, ast_channel_type_info, 0);
+	if (!channel_obj) {
+		ast_log(LOG_ERROR, "Failed to create channel object\n");
+		goto cleanup;
+	}
+
+	args_obj = PyTuple_New(1);
+	if (!args_obj) {
+		ast_log(LOG_ERROR, "Failed to get tuple thingy\n");
+		goto cleanup;
+	}
+
+	PyTuple_SetItem(args_obj, 0, channel_obj);
+
+	res_obj = PyObject_CallObject(func_obj, args_obj);
+	if (!res_obj) {
+		ast_log(LOG_ERROR, "Failed to call func\n");
+		goto cleanup;
+	}
+
+cleanup:
+	Py_XDECREF(args_obj);
+	Py_XDECREF(channel_obj);
+	Py_XDECREF(func_obj);
+	Py_XDECREF(mod_obj);
+	Py_XDECREF(fn_obj);
+	PyThreadState_Swap(NULL);
+	PyThreadState_Clear(my_thread_state);
+	PyThreadState_Delete(my_thread_state);
+
+	PyEval_ReleaseLock();
+
 	return 0;
 }
 
@@ -64,7 +136,7 @@
 
 	init_ast_channel(); /* init ast_channel module from swig */
 
-	res = ast_register_application(python_app, python_exec, python_synopsis, python_desc);
+	res = ast_register_application2(python_app, python_exec, python_synopsis, python_desc, python_module_info->self);
 
 	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
 }

Modified: team/russell/bindings/res/res_python/res_python.h
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/res_python/res_python.h?view=diff&rev=103320&r1=103319&r2=103320
==============================================================================
--- team/russell/bindings/res/res_python/res_python.h (original)
+++ team/russell/bindings/res/res_python/res_python.h Mon Feb 11 14:43:23 2008
@@ -21,7 +21,10 @@
 
 #include <Python.h>
 
+#include "asterisk/module.h"
+
 extern PyThreadState *main_thread_state;
+extern struct ast_module_info *python_module_info;
 
 /*! defined by python_ast_cdr_wrap.c */
 void init_ast_cdr(void);




More information about the svn-commits mailing list