lib/tevent: change to LGPLv3+
[samba.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", (char **)kwnames, &name))
81         return NULL;
82
83     if (name == NULL)
84         ev_ctx = tevent_context_init(NULL);
85     else
86         ev_ctx = tevent_context_init_byname(NULL, name);
87
88     ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
89     ret->ev_ctx = ev_ctx;
90     return (PyObject *)ret;
91 }
92
93 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
94 {
95     return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
96 }
97
98 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
99 {
100     return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
101 }
102
103 static PyMethodDef py_event_ctx_methods[] = {
104     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
105         "S.loop_once() -> int" },
106     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
107         "S.loop_wait() -> int" },
108     { NULL }
109 };
110
111 static void py_event_ctx_dealloc(PyTEventContextObject * self)
112 {
113         talloc_free(self->ev_ctx);
114         self->ob_type->tp_free(self);
115 }
116
117
118 PyTypeObject PyTEventContext = {
119     .tp_name = "TEventContext",
120     .tp_methods = py_event_ctx_methods,
121     .tp_basicsize = sizeof(PyTEventContextObject),
122     .tp_dealloc = (destructor)py_event_ctx_dealloc,
123     .tp_flags = Py_TPFLAGS_DEFAULT,
124     .tp_new = py_event_ctx_new,
125 };
126
127 void inittevent(void)
128 {
129     PyObject *m;
130
131     if (PyType_Ready(&PyTEventContext) < 0)
132         return;
133
134     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
135     if (m == NULL)
136         return;
137
138     Py_INCREF(&PyTEventContext);
139     PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
140 }
141