[asterisk-commits] russell: branch russell/bindings r103278 - in /team/russell/bindings/res: ./ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Feb 10 14:12:10 CST 2008


Author: russell
Date: Sun Feb 10 14:12:09 2008
New Revision: 103278

URL: http://svn.digium.com/view/asterisk?view=rev&rev=103278
Log:
move the cdr specific code into it's own file

Modified:
    team/russell/bindings/res/Makefile
    team/russell/bindings/res/res_python.c
    team/russell/bindings/res/res_python/res_python.h

Modified: team/russell/bindings/res/Makefile
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/Makefile?view=diff&rev=103278&r1=103277&r2=103278
==============================================================================
--- team/russell/bindings/res/Makefile (original)
+++ team/russell/bindings/res/Makefile Sun Feb 10 14:12:09 2008
@@ -48,7 +48,9 @@
 
 ael/pval.o: ael/pval.c
 
-res_python.so: res_python.o python_cdr_wrap.o
+res_python.so: res_python.o python_cdr_wrap.o res_python/cdr.o
+
+res_python/cdr.o: ASTCFLAGS+=$(PYTHON_INCLUDE)
 
 python_cdr_wrap.o: ASTCFLAGS+=$(PYTHON_INCLUDE)
 

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=103278&r1=103277&r2=103278
==============================================================================
--- team/russell/bindings/res/res_python.c (original)
+++ team/russell/bindings/res/res_python.c Sun Feb 10 14:12:09 2008
@@ -18,13 +18,9 @@
 
 /*! \file
  *
- * \brief Python bindings for CDR logging
+ * \brief Python bindings for Asterisk
  *
  * \author Russell Bryant <russell at digium.com>
- *
- * \arg See also \ref AstCDR
- *
- * \ingroup cdr_drivers
  */
 
 /*** MODULEINFO
@@ -36,162 +32,18 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <Python.h>
-
-#include "asterisk/cdr.h"
 #include "asterisk/module.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/config.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/cli.h"
 
-#include "../bindings/swigpyrun.h"
-
 #include "res_python/res_python.h"
 
-static const char config_file[] = "bindings.conf";
-
-static const char cdr_mod_name[] = "python";
-
-static PyThreadState *main_thread_state;
+PyThreadState *main_thread_state;
 
 /*! \brief Don't let python register signal handlers */
 #define INIT_SIGS 0
-
-struct py_cdr_mod {
-	AST_LIST_ENTRY(py_cdr_mod) entry;
-	char name[PATH_MAX];
-};
-
-/*! \note We're going to execute all of them ... */
-#define PY_CDR_MODS_BUCKETS 1
-struct ao2_container *py_cdr_mods;
-
-static struct py_cdr_mod *unref_py_cdr_mod(struct py_cdr_mod *py_cdr_mod)
-{
-	ao2_ref(py_cdr_mod, -1);
-
-	return NULL;
-}
-
-static void post_python_cdr(struct py_cdr_mod *py_cdr_mod, struct ast_cdr *cdr)
-{
-	PyThreadState *my_thread_state;
-	PyObject *func_obj = NULL, *mod_obj = NULL, *fn_obj = NULL,
-		*cdr_obj = NULL, *args_obj = NULL, *res_obj = NULL;
-	swig_type_info *ast_cdr_type_info = NULL;
-
-	PyEval_AcquireLock();
-
-	my_thread_state = PyThreadState_New(main_thread_state->interp);
-	PyThreadState_Swap(my_thread_state);
-
-	fn_obj = PyString_FromString(py_cdr_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", py_cdr_mod->name);
-		if (PyErr_Occurred())
-			PyErr_Print();
-		goto cleanup;
-	}
-
-	func_obj = PyObject_GetAttrString(mod_obj, "ast_post_cdr");
-	if (!func_obj) {
-		ast_log(LOG_ERROR, "Did not find ast_post_cdr in module '%s'\n", py_cdr_mod->name);
-		goto cleanup;
-	}
-
-	if (!PyCallable_Check(func_obj)) {
-		ast_log(LOG_ERROR, "ast_post_cdr found in '%s' but not callable\n", py_cdr_mod->name);
-		goto cleanup;
-	}
-
-	ast_cdr_type_info = SWIG_TypeQuery("_p_ast_cdr");
-	if (!ast_cdr_type_info) {
-		ast_log(LOG_ERROR, "Failed to look up ast_cdr type\n");
-		goto cleanup;
-	}
-
-	cdr_obj = SWIG_NewPointerObj(cdr, ast_cdr_type_info, 0);
-	if (!cdr_obj) {
-		ast_log(LOG_ERROR, "Failed to create CDR 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, cdr_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(cdr_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();
-}
-
-static int python_log(struct ast_cdr *cdr)
-{
-	struct ao2_iterator i;
-	struct py_cdr_mod *py_cdr_mod;
-
-	i = ao2_iterator_init(py_cdr_mods, 0);
-	while ((py_cdr_mod = ao2_iterator_next(&i))) {
-		post_python_cdr(py_cdr_mod, cdr);
-		py_cdr_mod = unref_py_cdr_mod(py_cdr_mod);
-	}
-
-	return 0;
-}
-
-static void py_cdr_mod_destroy(void *obj)
-{
-	return;
-}
-
-static void add_py_cdr_mod(const char *name)
-{
-	struct py_cdr_mod *py_cdr_mod;
-
-	if (!(py_cdr_mod = ao2_alloc(sizeof(*py_cdr_mod), py_cdr_mod_destroy)))
-		return;
-
-	ast_copy_string(py_cdr_mod->name, name, sizeof(py_cdr_mod->name));
-
-	ao2_link(py_cdr_mods, py_cdr_mod);
-
-	py_cdr_mod = unref_py_cdr_mod(py_cdr_mod);
-}
-
-static int print_py_cdr_mod(void *obj, void *arg, int flags)
-{
-	int fd = *((int *) arg);
-	struct py_cdr_mod *py_cdr_mod = obj;
-
-	ast_cli(fd, "=== ---> %s\n", py_cdr_mod->name);
-
-	return 0;
-}
 
 static char *cli_python_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
@@ -224,72 +76,36 @@
 
 	ast_cli(a->fd, "===\n=== Configured CDR Modules:\n");
 
-	ao2_callback(py_cdr_mods, 0, print_py_cdr_mod, &a->fd);
+	print_python_cdr_settings(&a->fd);
 
 	ast_cli(a->fd, "===\n=============================================================\n\n");
 
 	return 0;
 }
 
-static struct ast_cli_entry cli_cdr_python[] = {
+static struct ast_cli_entry cli_python[] = {
 	AST_CLI_DEFINE(cli_python_settings, "Show Python bindings settings"),
 };
 
 static int reload_module(void)
 {
-	struct ast_config *cfg;
-	struct ast_flags cfg_flags = { 0 };
-	struct ast_variable *var;
+	return python_cdr_reload();
+}
 
-	/* Unlink everything in the container */
-	ao2_callback(py_cdr_mods, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, NULL, NULL);
+static int unload_module(void)
+{
+	python_cdr_unload();
 
-	if (!(cfg = ast_config_load(config_file, cfg_flags)))
-		return -1;
+	ast_cli_unregister_multiple(cli_python, ARRAY_LEN(cli_python));
 
-	for (var = ast_variable_browse(cfg, "python"); var; var = var->next) {
-		if (strcasecmp(var->name, "cdr"))
-			continue;
-
-		add_py_cdr_mod(var->value);
-	}
-
-	ast_config_destroy(cfg);
+	PyEval_AcquireLock();
+	Py_Finalize();
 
 	return 0;
 }
 
-static int unload_module(void)
-{
-	ast_cdr_unregister(cdr_mod_name);
-	ast_cli_unregister_multiple(cli_cdr_python, ARRAY_LEN(cli_cdr_python));
-
-	PyEval_AcquireLock();
-	Py_Finalize();
-
-	ao2_ref(py_cdr_mods, -1);
-	py_cdr_mods = NULL;
-
-	return 0;
-}
-
-static int hash_zero(const void *user_obj, const int flags)
-{
-	return 0;
-}
-
-static int match_all(void *obj, void *arg, int flags)
-{
-	return CMP_MATCH;
-}
-
 static int load_module(void)
 {
-	if (!(py_cdr_mods = ao2_container_alloc(PY_CDR_MODS_BUCKETS, hash_zero, match_all)))
-		return AST_MODULE_LOAD_DECLINE;
-
-	reload_module();
-
 #ifdef __Darwin__ /* XXX Fix this check */
 	Py_Initialize();
 #else
@@ -298,26 +114,20 @@
 
 	PyEval_InitThreads();
 
+	python_cdr_load();
+
 	main_thread_state = PyThreadState_Get();
-
-	init_cdr(); /* init cdr module from swig */
 
 	PyEval_ReleaseLock();
 
-	if (ast_cdr_register(cdr_mod_name, ast_module_info->description, python_log)) {
-		PyEval_AcquireLock();
-		Py_Finalize();
-		ao2_ref(py_cdr_mods, -1);
-		py_cdr_mods = NULL;
-		return AST_MODULE_LOAD_DECLINE;
-	}
+	reload_module();
 
-	ast_cli_register_multiple(cli_cdr_python, ARRAY_LEN(cli_cdr_python));
+	ast_cli_register_multiple(cli_python, ARRAY_LEN(cli_python));
 
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Python bindings for CDR logging",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Python bindings",
 	.load = load_module,
 	.unload = unload_module,
 	.reload = reload_module,

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=103278&r1=103277&r2=103278
==============================================================================
--- team/russell/bindings/res/res_python/res_python.h (original)
+++ team/russell/bindings/res/res_python/res_python.h Sun Feb 10 14:12:09 2008
@@ -19,7 +19,16 @@
 #ifndef RES_PYTHON_H
 #define RES_PYTHON_H
 
+#include <Python.h>
+
+extern PyThreadState *main_thread_state;
+
 /*! defined by python_cdr_wrap.c */
 void init_cdr(void);
 
+int python_cdr_load(void);
+int python_cdr_unload(void);
+int python_cdr_reload(void);
+void print_python_cdr_settings(int *fd);
+
 #endif /* RES_PYTHON_H */




More information about the asterisk-commits mailing list