root/trunk/midgard/apis/python/py_midgard_connection.c

Revision 17022, 9.8 kB (checked in by piotras, 4 months ago)

Removed set_debuglevel method in favour of set_loglevel one

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define NO_IMPORT_PYGOBJECT
24 #include "py_midgard.h"
25
26 PyTypeObject G_GNUC_INTERNAL Pymidgard_connection_Type;
27
28 #define CONNECTION_DEBUG(__name) \
29         CHECK_MGD; \
30         CLASS_METHOD_DEBUG(Pymidgard_connection_Type.tp_name, __name);
31
32 static MidgardConnection *_global_singleton = NULL;
33
34 void _py_midgard_connection_singleton_set(MidgardConnection *mgd)
35 {
36         _global_singleton = mgd;
37        
38         midgard_connection_set_loghandler(mgd, get_global_log_handler());
39 }
40
41 MidgardConnection *_py_midgard_connection_singleton_get(void)
42 {
43         return _global_singleton;
44 }
45
46 /* Overwrite constructor.
47  * Basically we do ( almost ) the same what is done in pygobject_init.
48  * The difference is that we do not need any argument(s) here, and we can
49  * easily create own singleton implementation */
50 static int
51 __connection_singleton_init(PyGObject *self, PyObject *args, PyObject *kwargs)
52 {
53         CONNECTION_DEBUG("__init__");
54         if(_py_midgard_connection_singleton_get()){
55
56                 /* Throw TypeError exception */
57                 PyErr_SetString(PyExc_TypeError,
58                                 "MidgardConnection singleton already initialized");
59                 return -1;
60         }
61
62         if (pygobject_constructv(self, 0, NULL))
63                 PyErr_SetString(PyExc_RuntimeError, "could not create object");
64
65         if(self->obj != NULL) {
66
67                 /* set global */
68                 _py_midgard_connection_singleton_set(MIDGARD_CONNECTION(self->obj));
69                 /* get module object and set python object */
70                 PyObject *module = PyImport_AddModule("_midgard");     
71                 PyObject *d = PyModule_GetDict(module);
72                 PyDict_DelItemString(d, "_connection");
73                 PyDict_SetItemString(d, "_connection", (PyObject *)self);
74         }
75
76         return (self->obj) ? 0 : -1;
77 }
78
79 static PyObject *
80 pymidgard_connection_open(PyGObject *self, PyObject *args)
81 {
82         CONNECTION_DEBUG("open");
83         gchar *name;
84        
85         if(!PyArg_ParseTuple(args, "s", &name))
86                 return NULL;
87
88
89         GError *error = NULL;
90
91         gboolean connected =
92                 midgard_connection_open(
93                                 MIDGARD_CONNECTION(self->obj),
94                                 (const gchar *)name, &error);
95         if(connected)
96                 Py_RETURN_TRUE;
97        
98         if(error) {
99                
100                 PyErr_SetString(PyExc_SystemError, error->message);
101                 g_error_free(error);
102                
103                 return NULL;
104         }
105
106         PyErr_SetString(PyExc_SystemError, "Unhandled midgard exception. FIXME!");
107         return NULL;
108 }
109
110 static PyObject *
111 pymidgard_connection_open_config(PyGObject *self, PyObject *args)
112 {
113         CONNECTION_DEBUG("open_config");
114         PyObject *config;
115                
116         if(!PyArg_ParseTuple(args, "O", &config))
117                 return NULL;
118
119         if(config == NULL)
120                 g_warning("config is NULL");
121
122         PyGObject *_config = (PyGObject *)config;
123
124         gboolean connected =
125                 midgard_connection_open_config(
126                                 MIDGARD_CONNECTION(self->obj),
127                                 MIDGARD_CONFIG(_config->obj));
128         if(connected)
129                 Py_RETURN_TRUE;
130        
131         Py_RETURN_FALSE;
132 }
133
134 static PyObject *
135 pymidgard_connection_set_sitegroup(PyGObject *self, PyObject *args)
136 {
137         CONNECTION_DEBUG("set_sitegroup");
138         gchar *name;
139        
140         if(!PyArg_ParseTuple(args, "s", &name))
141                 return NULL;
142        
143         if(midgard_connection_set_sitegroup(
144                                 MIDGARD_CONNECTION(self->obj), (const gchar *)name))
145                 Py_RETURN_TRUE;
146
147         Py_RETURN_FALSE;
148 }
149
150 static PyObject *
151 pymidgard_connection_get_sitegroup(PyGObject *self, PyObject *args)
152 {
153         CONNECTION_DEBUG("get_sitegroup");
154         if(!PyArg_ParseTuple(args, ""))
155                 return NULL;
156        
157         const gchar *name;
158         name = midgard_connection_get_sitegroup(MIDGARD_CONNECTION(self->obj));
159
160         return Py_BuildValue("s", name);
161 }
162
163 static PyObject *
164 pymidgard_connection_set_lang(PyGObject *self, PyObject *args)
165 {
166         CONNECTION_DEBUG("set_lang");
167         gchar *lang;
168        
169         if(!PyArg_ParseTuple(args, "s", &lang))
170                 return NULL;
171        
172         if(midgard_connection_set_lang(
173                                 MIDGARD_CONNECTION(self->obj), (const gchar *)lang))
174                 Py_RETURN_TRUE;
175
176         Py_RETURN_FALSE;
177 }
178
179 static PyObject *
180 pymidgard_connection_get_lang(PyGObject *self, PyObject *args)
181 {
182         CONNECTION_DEBUG("get_lang");
183         if(!PyArg_ParseTuple(args, ""))
184                 return NULL;
185        
186         const gchar *lang;
187         lang = midgard_connection_get_lang(MIDGARD_CONNECTION(self->obj));
188
189         return Py_BuildValue("s", lang);
190 }
191
192 static PyObject *
193 pymidgard_connection_set_default_lang(PyGObject *self, PyObject *args)
194 {
195         CONNECTION_DEBUG("set_default_lang");
196         gchar *lang;
197        
198         if(!PyArg_ParseTuple(args, "s", &lang))
199                 return NULL;
200        
201         if(midgard_connection_set_default_lang(
202                                 MIDGARD_CONNECTION(self->obj), (const gchar *)lang))
203                 Py_RETURN_TRUE;
204
205         Py_RETURN_FALSE;
206 }
207
208 static PyObject *
209 pymidgard_connection_get_default_lang(PyGObject *self, PyObject *args)
210 {
211         CONNECTION_DEBUG("get_default_lang");
212         if(!PyArg_ParseTuple(args, ""))
213                 return NULL;
214        
215         const gchar *lang;
216         lang = midgard_connection_get_default_lang(MIDGARD_CONNECTION(self->obj));
217
218         return Py_BuildValue("s", lang);
219 }
220
221 static PyObject *
222 pymidgard_connection_set_loglevel(PyGObject *self, PyObject *args)
223 {
224         CONNECTION_DEBUG("set_loglevel");
225         const gchar *level;
226        
227         if(!PyArg_ParseTuple(args, "s", &level))
228                 return NULL;
229        
230         midgard_connection_set_loglevel(MIDGARD_CONNECTION(self->obj),
231                         (const gchar *)level, NULL);
232        
233         Py_RETURN_NONE;
234 }
235
236 static PyObject *
237 pymidgard_connection_get_error(PyGObject *self, PyObject *args)
238 {
239         CONNECTION_DEBUG("get_error");
240        
241         if(!PyArg_ParseTuple(args, ""))
242                 return NULL;
243        
244         gint errn =
245                 midgard_connection_get_error(MIDGARD_CONNECTION(self->obj));
246
247         return Py_BuildValue("i", (int)errn);
248 }
249
250 static PyObject *
251 pymidgard_connection_get_error_string(PyGObject *self, PyObject *args)
252 {
253         CONNECTION_DEBUG("get_error_string");
254        
255         if(!PyArg_ParseTuple(args, ""))
256                 return NULL;
257        
258         const gchar *error_string =
259                 midgard_connection_get_error_string(MIDGARD_CONNECTION(self->obj));
260
261         return Py_BuildValue("s", error_string);
262 }
263
264 static PyObject *
265 pymidgard_connection_get_user(PyGObject *self, PyObject *args)
266 {
267         CONNECTION_DEBUG("get_user");
268        
269         if(!PyArg_ParseTuple(args, ""))
270                 return NULL;
271        
272         MidgardUser *user =
273                 midgard_connection_get_user(MIDGARD_CONNECTION(self->obj));
274
275         if(user == NULL)
276                 Py_RETURN_NONE;
277        
278         GValue gval = {0, };
279         g_value_init(&gval, G_TYPE_OBJECT);
280         g_value_set_object(&gval, G_OBJECT(user));
281         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
282        
283         return pval;
284 }
285
286 static PyMethodDef pymidgard_connection_methods[] = {
287         { "open", (PyCFunction)pymidgard_connection_open, METH_VARARGS },
288         { "open_config", (PyCFunction)pymidgard_connection_open_config, METH_VARARGS },
289         { "set_sitegroup", (PyCFunction)pymidgard_connection_set_sitegroup, METH_VARARGS },
290         { "get_sitegroup", (PyCFunction)pymidgard_connection_get_sitegroup, METH_VARARGS },
291         { "set_lang", (PyCFunction)pymidgard_connection_set_lang, METH_VARARGS },
292         { "get_lang", (PyCFunction)pymidgard_connection_get_lang, METH_VARARGS },
293         { "set_default_lang", (PyCFunction)pymidgard_connection_set_default_lang, METH_VARARGS },
294         { "get_default_lang", (PyCFunction)pymidgard_connection_get_default_lang, METH_VARARGS },
295         { "set_loglevel", (PyCFunction)pymidgard_connection_set_loglevel, METH_VARARGS },
296         { "get_error", (PyCFunction)pymidgard_connection_get_error, METH_VARARGS },
297         { "get_error_string", (PyCFunction)pymidgard_connection_get_error_string, METH_VARARGS },
298         { "get_user", (PyCFunction)pymidgard_connection_get_user, METH_VARARGS },
299         { NULL, NULL, 0 }
300 };
301
302 PyTypeObject G_GNUC_INTERNAL Pymidgard_connection_Type = {
303         PyObject_HEAD_INIT(NULL)
304         0,                              /* ob_size */
305         "connection",                   /* tp_name */
306         sizeof(PyGObject),              /* tp_basicsize */
307         0,                              /* tp_itemsize */
308         /* methods */
309         (destructor)0,                  /* tp_dealloc */
310         (printfunc)0,                   /* tp_print */
311         (getattrfunc)0,                 /* tp_getattr */
312         (setattrfunc)0,                 /* tp_setattr */
313         (cmpfunc)0,                     /* tp_compare */
314         (reprfunc)0,                    /* tp_repr */
315         (PyNumberMethods*)0,            /* tp_as_number */
316         (PySequenceMethods*)0,          /* tp_as_sequence */
317         (PyMappingMethods*)0,           /* tp_as_mapping */
318         (hashfunc)0,                    /* tp_hash */
319         (ternaryfunc)0,                 /* tp_call */
320         (reprfunc)0,                    /* tp_str */
321         _py_midgard_get_object_attribute, /* tp_getattro */
322         _py_midgard_set_object_attribute, /* tp_setattro */
323         (PyBufferProcs*)0,              /* tp_as_buffer */
324         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
325         "MidgardConnection singleton class",            /* Documentation string */
326         (traverseproc)0,                /* tp_traverse */
327         (inquiry)0,                     /* tp_clear */
328         (richcmpfunc)0,                 /* tp_richcompare */
329         offsetof(PyGObject, weakreflist),       /* tp_weaklistoffset */
330         (getiterfunc)0,                 /* tp_iter */
331         (iternextfunc)0,                /* tp_iternext */
332         pymidgard_connection_methods,   /* tp_methods */
333         (struct PyMemberDef*)0,         /* tp_members */
334         (struct PyGetSetDef*)0,         /* tp_getset */
335         NULL,                           /* tp_base */
336         NULL,                           /* tp_dict */
337         (descrgetfunc)0,                /* tp_descr_get */   
338         (descrsetfunc)0,                /* tp_descr_set */
339         offsetof(PyGObject, inst_dict),         /* tp_dictoffset */
340         (initproc)__connection_singleton_init,  /* tp_init */
341         (allocfunc)0,                   /* tp_alloc */
342         (newfunc)0,                     /* tp_new */
343         (freefunc)0,                    /* tp_free */
344         (inquiry)0                      /* tp_is_gc */
345 };
346
347 void py_midgard_connection_register_class(
348                 PyObject *d, gpointer pygobject_type)
349 {
350         pygobject_register_class(d,
351                         "connection",
352                         MIDGARD_TYPE_CONNECTION,
353                         &Pymidgard_connection_Type,
354                         Py_BuildValue("(O)", pygobject_type));
355
356         //pyg_set_object_has_new_constructor(MIDGARD_TYPE_CONNECTION);
357 }
Note: See TracBrowser for help on using the browser.