Merge branch 'master' of ssh://git.samba.org/data/git/samba into tevent-standalone
[ira/wip.git] / lib / tevent / pytevent.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <Python.h>
20 #include <tevent.h>
21 #include <stdbool.h>
22 #include <../talloc/pytalloc.h>
23 #include <tevent_util.h>
24
25 PyAPI_DATA(PyTypeObject) PyEventContext;
26
27 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
28 {
29     char *name;
30
31     if (!PyArg_ParseTuple(args, "s", &name))
32         return NULL;
33     event_set_default_backend(name);
34     return Py_None;
35 }
36
37 static PyObject *py_backend_list(PyObject *self)
38 {
39     const char **backends = event_backend_list(NULL);
40     PyObject *ret;
41     int i, len;
42
43     len = ev_str_list_length(backends);
44     ret = PyList_New(len);
45     for (i = 0; i < len; i++)
46         PyList_SetItem(ret, i, PyString_FromString(backends[i]));
47     talloc_free(backends);
48
49     return ret;
50 }
51
52 static PyMethodDef tevent_methods[] = {
53     { "set_default_backend", (PyCFunction)py_set_default_backend, 
54         METH_VARARGS, "set_default_backend(name) -> None" },
55     { "backend_list", (PyCFunction)py_backend_list,
56         METH_NOARGS, "backend_list() -> list" },
57     { NULL },
58 };
59
60 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
61 {
62     const char *kwnames[] = { "name", NULL };
63     char *name = NULL;
64     struct event_context *ev_ctx;
65     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", (char **)kwnames, &name))
66         return NULL;
67
68     if (name == NULL)
69         ev_ctx = event_context_init(NULL);
70     else
71         ev_ctx = event_context_init_byname(NULL, name);
72
73     return py_talloc_import(&PyEventContext, ev_ctx);
74 }
75
76 static PyObject *py_event_ctx_loop_once(py_talloc_Object *self)
77 {
78     return PyInt_FromLong(event_loop_once(self->ptr));
79 }
80
81 static PyObject *py_event_ctx_loop_wait(py_talloc_Object *self)
82 {
83     return PyInt_FromLong(event_loop_wait(self->ptr));
84 }
85
86 static PyMethodDef py_event_ctx_methods[] = {
87     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
88         "S.loop_once() -> int" },
89     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
90         "S.loop_wait() -> int" },
91     { NULL }
92 };
93
94 PyTypeObject PyEventContext = {
95     .tp_name = "EventContext",
96     .tp_methods = py_event_ctx_methods,
97     .tp_basicsize = sizeof(py_talloc_Object),
98     .tp_dealloc = py_talloc_dealloc,
99     .tp_new = py_event_ctx_new,
100 };
101
102 void inittevent(void)
103 {
104     PyObject *m;
105
106     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
107     if (m == NULL)
108         return;
109 }
110