4c0cbfd3cd1cf837cb2a82d0f924ac4b87d78a56
[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      ** NOTE! The following LGPL license applies to the tevent
6      ** library. This does NOT imply that all of Samba is released
7      ** under the LGPL
8
9    This library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public
11    License as published by the Free Software Foundation; either
12    version 3 of the License, or (at your option) any later version.
13
14    This library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Lesser General Public License for more details.
18
19    You should have received a copy of the GNU Lesser General Public
20    License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "replace.h"
24 #include <Python.h>
25
26 #ifndef Py_RETURN_NONE
27 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
28 #endif
29
30 #include <tevent.h>
31 #include <stdbool.h>
32 #include <tevent_util.h>
33
34 typedef struct {
35         PyObject_HEAD
36         struct tevent_context *ev_ctx;
37 } PyTEventContextObject;
38
39 PyAPI_DATA(PyTypeObject) PyTEventContext;
40
41 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
42 {
43     char *name;
44
45     if (!PyArg_ParseTuple(args, "s", &name))
46         return NULL;
47     tevent_set_default_backend(name);
48     Py_RETURN_NONE;
49 }
50
51 static PyObject *py_backend_list(PyObject *self)
52 {
53     const char **backends = tevent_backend_list(NULL);
54     PyObject *ret;
55     int i, len;
56
57     len = ev_str_list_length(backends);
58     ret = PyList_New(len);
59     for (i = 0; i < len; i++)
60         PyList_SetItem(ret, i, PyString_FromString(backends[i]));
61     talloc_free(backends);
62
63     return ret;
64 }
65
66 static PyMethodDef tevent_methods[] = {
67     { "set_default_backend", (PyCFunction)py_set_default_backend, 
68         METH_VARARGS, "set_default_backend(name) -> None" },
69     { "backend_list", (PyCFunction)py_backend_list,
70         METH_NOARGS, "backend_list() -> list" },
71     { NULL },
72 };
73
74 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
75 {
76     const char *kwnames[] = { "name", NULL };
77     char *name = NULL;
78     struct tevent_context *ev_ctx;
79     PyTEventContextObject *ret;
80     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
81                                      discard_const_p(char *, kwnames),
82                                      &name))
83         return NULL;
84
85     if (name == NULL)
86         ev_ctx = tevent_context_init(NULL);
87     else
88         ev_ctx = tevent_context_init_byname(NULL, name);
89
90     ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
91     ret->ev_ctx = ev_ctx;
92     return (PyObject *)ret;
93 }
94
95 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
96 {
97     return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
98 }
99
100 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
101 {
102     return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
103 }
104
105 static PyMethodDef py_event_ctx_methods[] = {
106     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
107         "S.loop_once() -> int" },
108     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
109         "S.loop_wait() -> int" },
110     { NULL }
111 };
112
113 static void py_event_ctx_dealloc(PyTEventContextObject * self)
114 {
115         talloc_free(self->ev_ctx);
116         self->ob_type->tp_free(self);
117 }
118
119
120 PyTypeObject PyTEventContext = {
121     .tp_name = "TEventContext",
122     .tp_methods = py_event_ctx_methods,
123     .tp_basicsize = sizeof(PyTEventContextObject),
124     .tp_dealloc = (destructor)py_event_ctx_dealloc,
125     .tp_flags = Py_TPFLAGS_DEFAULT,
126     .tp_new = py_event_ctx_new,
127 };
128
129 void inittevent(void)
130 {
131     PyObject *m;
132
133     if (PyType_Ready(&PyTEventContext) < 0)
134         return;
135
136     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
137     if (m == NULL)
138         return;
139
140     Py_INCREF(&PyTEventContext);
141     PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
142 }
143