Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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 "replace.h"
20 #include <Python.h>
21
22 #ifndef Py_RETURN_NONE
23 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
24 #endif
25
26 #include <tevent.h>
27 #include <stdbool.h>
28 #include <tevent_util.h>
29
30 typedef struct {
31         PyObject_HEAD
32         struct tevent_context *ev_ctx;
33 } PyTEventContextObject;
34
35 PyAPI_DATA(PyTypeObject) PyTEventContext;
36
37 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
38 {
39     char *name;
40
41     if (!PyArg_ParseTuple(args, "s", &name))
42         return NULL;
43     tevent_set_default_backend(name);
44     Py_RETURN_NONE;
45 }
46
47 static PyObject *py_backend_list(PyObject *self)
48 {
49     const char **backends = tevent_backend_list(NULL);
50     PyObject *ret;
51     int i, len;
52
53     len = ev_str_list_length(backends);
54     ret = PyList_New(len);
55     for (i = 0; i < len; i++)
56         PyList_SetItem(ret, i, PyString_FromString(backends[i]));
57     talloc_free(backends);
58
59     return ret;
60 }
61
62 static PyMethodDef tevent_methods[] = {
63     { "set_default_backend", (PyCFunction)py_set_default_backend, 
64         METH_VARARGS, "set_default_backend(name) -> None" },
65     { "backend_list", (PyCFunction)py_backend_list,
66         METH_NOARGS, "backend_list() -> list" },
67     { NULL },
68 };
69
70 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
71 {
72     const char *kwnames[] = { "name", NULL };
73     char *name = NULL;
74     struct tevent_context *ev_ctx;
75     PyTEventContextObject *ret;
76     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", (char **)kwnames, &name))
77         return NULL;
78
79     if (name == NULL)
80         ev_ctx = tevent_context_init(NULL);
81     else
82         ev_ctx = tevent_context_init_byname(NULL, name);
83
84     ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
85     ret->ev_ctx = ev_ctx;
86     return (PyObject *)ret;
87 }
88
89 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
90 {
91     return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
92 }
93
94 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
95 {
96     return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
97 }
98
99 static PyMethodDef py_event_ctx_methods[] = {
100     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
101         "S.loop_once() -> int" },
102     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
103         "S.loop_wait() -> int" },
104     { NULL }
105 };
106
107 static void py_event_ctx_dealloc(PyTEventContextObject * self)
108 {
109         talloc_free(self->ev_ctx);
110         self->ob_type->tp_free(self);
111 }
112
113
114 PyTypeObject PyTEventContext = {
115     .tp_name = "TEventContext",
116     .tp_methods = py_event_ctx_methods,
117     .tp_basicsize = sizeof(PyTEventContextObject),
118     .tp_dealloc = (destructor)py_event_ctx_dealloc,
119     .tp_flags = Py_TPFLAGS_DEFAULT,
120     .tp_new = py_event_ctx_new,
121 };
122
123 void inittevent(void)
124 {
125     PyObject *m;
126
127     if (PyType_Ready(&PyTEventContext) < 0)
128         return;
129
130     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
131     if (m == NULL)
132         return;
133
134     Py_INCREF(&PyTEventContext);
135     PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
136 }
137