[asterisk-commits] russell: branch russell/bindings r103279 - in /team/russell/bindings: binding...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Feb 10 15:27:02 CST 2008


Author: russell
Date: Sun Feb 10 15:27:02 2008
New Revision: 103279

URL: http://svn.digium.com/view/asterisk?view=rev&rev=103279
Log:
rename the "cdr" module to "ast_cdr"

Added:
    team/russell/bindings/bindings/ast_cdr.i
      - copied, changed from r103277, team/russell/bindings/bindings/cdr.i
    team/russell/bindings/res/res_python/cdr.c   (with props)
Removed:
    team/russell/bindings/bindings/cdr.i
    team/russell/bindings/bindings/cdr_wrap.c
    team/russell/bindings/bindings/swigpyrun.h
Modified:
    team/russell/bindings/bindings/   (props changed)
    team/russell/bindings/bindings/Makefile
    team/russell/bindings/bindings/examples/cdr_csv.py
    team/russell/bindings/bindings/examples/cdr_test_vars.py
    team/russell/bindings/res/   (props changed)
    team/russell/bindings/res/Makefile
    team/russell/bindings/res/res_python/res_python.h

Propchange: team/russell/bindings/bindings/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Feb 10 15:27:02 2008
@@ -1,0 +1,4 @@
+build
+ast_cdr.py
+ast_cdr.pyc
+ast_cdr_wrap.c

Modified: team/russell/bindings/bindings/Makefile
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/Makefile?view=diff&rev=103279&r1=103278&r2=103279
==============================================================================
--- team/russell/bindings/bindings/Makefile (original)
+++ team/russell/bindings/bindings/Makefile Sun Feb 10 15:27:02 2008
@@ -11,16 +11,21 @@
 # the GNU General Public License
 #
 
-PYTHON_MODS:=cdr.py
+PYTHON_MODS:=ast_cdr.py
 
-all: cdr_wrap.c
+all: ast_cdr_wrap.c swigpyrun.h
 
-cdr.i: ../include/asterisk/cdr.h
+ast_cdr.i: ../include/asterisk/cdr.h
 	$(AWK) -f get_swig $< >> $@
 
-cdr_wrap.c: cdr.i
+ast_cdr_wrap.c: ast_cdr.i
 ifneq ($(SWIG),:)
-	$(SWIG) -I$(ASTTOPDIR)/include -python cdr.i
+	$(SWIG) -I$(ASTTOPDIR)/include -python ast_cdr.i
+	$(SWIG) -I$(ASTTOPDIR)/include -python -external-runtime
+endif
+
+swigpyrun.h:
+ifneq ($(SWIG),:)
 	$(SWIG) -I$(ASTTOPDIR)/include -python -external-runtime
 endif
 
@@ -32,7 +37,7 @@
 	for x in $(PYTHON_MODS); do rm -f $(DESTDIR)$(PYTHON_DIR)/$$x $(DESTDIR)$(PYTHON_DIR)/$${x}c ; done
 
 clean:
-	rm -f _cdr.so cdr_wrap.c cdr.py cdr.pyc swigpyrun.h
+	rm -f _ast_cdr.so ast_cdr_wrap.c ast_cdr.py ast_cdr.pyc swigpyrun.h
 	rm -rf build
 
 distclean: clean

Copied: team/russell/bindings/bindings/ast_cdr.i (from r103277, team/russell/bindings/bindings/cdr.i)
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/ast_cdr.i?view=diff&rev=103279&p1=team/russell/bindings/bindings/cdr.i&r1=103277&p2=team/russell/bindings/bindings/ast_cdr.i&r2=103279
==============================================================================
--- team/russell/bindings/bindings/cdr.i (original)
+++ team/russell/bindings/bindings/ast_cdr.i Sun Feb 10 15:27:02 2008
@@ -1,4 +1,4 @@
-%module cdr
+%module ast_cdr
 
 %{
 #include "asterisk.h"

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=103279&r1=103278&r2=103279
==============================================================================
--- team/russell/bindings/bindings/examples/cdr_csv.py (original)
+++ team/russell/bindings/bindings/examples/cdr_csv.py Sun Feb 10 15:27:02 2008
@@ -6,7 +6,7 @@
 in python using the cdr_python module.
 '''
 
-import cdr
+import ast_cdr
 import string
 import time
 

Modified: team/russell/bindings/bindings/examples/cdr_test_vars.py
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/bindings/examples/cdr_test_vars.py?view=diff&rev=103279&r1=103278&r2=103279
==============================================================================
--- team/russell/bindings/bindings/examples/cdr_test_vars.py (original)
+++ team/russell/bindings/bindings/examples/cdr_test_vars.py Sun Feb 10 15:27:02 2008
@@ -8,7 +8,7 @@
  - get the value of a variable by name
 '''
 
-import cdr
+import ast_cdr
 
 def ast_post_cdr(record):
 	print 'CDR record for channel: ' + record.channel

Propchange: team/russell/bindings/res/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Feb 10 15:27:02 2008
@@ -8,3 +8,4 @@
 *.s
 *.so
 modules.link
+python_ast_cdr_wrap.c

Modified: team/russell/bindings/res/Makefile
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/Makefile?view=diff&rev=103279&r1=103278&r2=103279
==============================================================================
--- team/russell/bindings/res/Makefile (original)
+++ team/russell/bindings/res/Makefile Sun Feb 10 15:27:02 2008
@@ -48,16 +48,16 @@
 
 ael/pval.o: ael/pval.c
 
-res_python.so: res_python.o python_cdr_wrap.o res_python/cdr.o
+res_python.so: res_python.o python_ast_cdr_wrap.o res_python/cdr.o
 
 res_python/cdr.o: ASTCFLAGS+=$(PYTHON_INCLUDE)
 
-python_cdr_wrap.o: ASTCFLAGS+=$(PYTHON_INCLUDE)
+python_ast_cdr_wrap.o: ASTCFLAGS+=$(PYTHON_INCLUDE)
 
-python_cdr_wrap.c: ../bindings/cdr_wrap.c
+python_ast_cdr_wrap.c: ../bindings/ast_cdr_wrap.c
 	@cp $< $@
 
 clean::
 	rm -f snmp/*.o
 	rm -f ael/*.o
-	rm -f python_cdr_wrap.c
+	rm -f python_ast_cdr_wrap.c

Added: team/russell/bindings/res/res_python/cdr.c
URL: http://svn.digium.com/view/asterisk/team/russell/bindings/res/res_python/cdr.c?view=auto&rev=103279
==============================================================================
--- team/russell/bindings/res/res_python/cdr.c (added)
+++ team/russell/bindings/res/res_python/cdr.c Sun Feb 10 15:27:02 2008
@@ -1,0 +1,239 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2008, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Python bindings for CDR logging
+ *
+ * \author Russell Bryant <russell at digium.com>
+ *
+ * \arg See also \ref AstCDR
+ *
+ * \ingroup cdr_drivers
+ */
+
+#include "asterisk/cdr.h"
+#include "asterisk/cli.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/module.h"
+
+#include "res_python.h"
+#include "../bindings/swigpyrun.h"
+
+static const char config_file[] = "bindings.conf";
+
+static const char cdr_mod_name[] = "python";
+
+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 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;
+}
+
+void print_python_cdr_settings(int *fd)
+{
+	ao2_callback(py_cdr_mods, 0, print_py_cdr_mod, fd);
+}
+
+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);
+}
+
+int python_cdr_reload(void)
+{
+	struct ast_config *cfg;
+	struct ast_flags cfg_flags = { 0 };
+	struct ast_variable *var;
+
+	/* Unlink everything in the container */
+	ao2_callback(py_cdr_mods, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, NULL, NULL);
+
+	if (!(cfg = ast_config_load(config_file, cfg_flags)))
+		return -1;
+
+	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);
+
+	return 0;
+
+}
+
+int python_cdr_unload(void)
+{
+	ast_cdr_unregister(cdr_mod_name);
+	
+	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;
+}
+
+int python_cdr_load(void)
+{
+	int res;
+
+	if (!(py_cdr_mods = ao2_container_alloc(PY_CDR_MODS_BUCKETS, hash_zero, match_all)))
+		return AST_MODULE_LOAD_DECLINE;
+
+	init_ast_cdr(); /* init ast_cdr module from swig */
+
+	res = ast_cdr_register(cdr_mod_name, "Python bindings for CDR logging", python_log);
+
+	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+}

Propchange: team/russell/bindings/res/res_python/cdr.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/bindings/res/res_python/cdr.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/bindings/res/res_python/cdr.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=103279&r1=103278&r2=103279
==============================================================================
--- team/russell/bindings/res/res_python/res_python.h (original)
+++ team/russell/bindings/res/res_python/res_python.h Sun Feb 10 15:27:02 2008
@@ -24,7 +24,7 @@
 extern PyThreadState *main_thread_state;
 
 /*! defined by python_cdr_wrap.c */
-void init_cdr(void);
+void init_ast_cdr(void);
 
 int python_cdr_load(void);
 int python_cdr_unload(void);




More information about the asterisk-commits mailing list