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

Revision 23623, 6.7 kB (checked in by piotras, 5 months ago)

Fix usability instead of readability.

Line 
1 /*
2  * Copyright (C) 2009 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_storage_Type;
27
28 #define STORAGE_DEBUG(__name) \
29         CHECK_MGD; \
30         CLASS_METHOD_DEBUG(Pymidgard_storage_Type.tp_name, __name);
31
32 #define __STORAGE_FUNC \
33         gchar *classname; \
34         if(!PyArg_ParseTuple(args, "s", &classname)) \
35                 return NULL; \
36         MidgardConnection *mgd = _py_midgard_connection_singleton_get(); \
37         if (!mgd) { \
38                 g_warning("Underlying midgard connection not found"); \
39                 return NULL; \
40         } \
41         MidgardDBObjectClass *dbklass = g_type_class_peek(g_type_from_name(classname)); \
42         if (!dbklass) { \
43                 g_warning("%s is no tregistered as midgard_dbobject derived class", classname); \
44                 return NULL; \
45         }
46
47 static PyObject *
48 pymidgard_storage_create_class_storage(PyGObject *self, PyObject *args)
49 {       
50         STORAGE_DEBUG("create_class_storage");
51        
52         __STORAGE_FUNC
53
54         if (midgard_storage_create_class_storage(mgd, dbklass))
55                 Py_RETURN_TRUE;
56
57         Py_RETURN_FALSE;
58 }
59
60 static PyObject *
61 pymidgard_storage_update_class_storage(PyGObject *self, PyObject *args)
62 {       
63         STORAGE_DEBUG("update_class_storage");
64        
65         __STORAGE_FUNC
66
67         if (midgard_storage_update_class_storage(mgd, dbklass))
68                 Py_RETURN_TRUE;
69
70         Py_RETURN_FALSE;
71 }
72
73 static PyObject *
74 pymidgard_storage_delete_class_storage(PyGObject *self, PyObject *args)
75 {       
76         STORAGE_DEBUG("delete_class_storage");
77        
78         __STORAGE_FUNC
79
80         if (midgard_storage_delete_class_storage(mgd, dbklass))
81                 Py_RETURN_TRUE;
82
83         Py_RETURN_FALSE;
84 }
85
86 static PyObject *
87 pymidgard_storage_class_storage_exists(PyGObject *self, PyObject *args)
88 {       
89         STORAGE_DEBUG("class_storage_exists");
90        
91         __STORAGE_FUNC
92
93         if (midgard_storage_class_storage_exists(mgd, dbklass))
94                 Py_RETURN_TRUE;
95
96         Py_RETURN_FALSE;
97 }
98
99 static PyObject *
100 pymidgard_storage_create_base_storage(PyGObject *self, PyObject *args)
101 {
102         STORAGE_DEBUG("create_base_storage");
103        
104         MidgardConnection *mgd =
105                 _py_midgard_connection_singleton_get();
106        
107         if (midgard_storage_create_base_storage(mgd))
108                 Py_RETURN_TRUE;
109
110         Py_RETURN_FALSE;
111
112 }
113
114 static int __py_midgard_parse_dbobject(PyObject *object)
115 {
116         PyObject *pclass = NULL, *pcname = NULL;
117         int ret = 0;
118        
119         pclass = PyObject_GetAttrString(object, "__class__");
120         if (!pclass) {
121
122                 PyErr_SetString(PyExc_TypeError, "Didn't find a class for given argument object.");
123                 return ret;
124         }
125
126         pcname = PyObject_GetAttrString(pclass, "__name__");
127         if(!pcname) {
128
129                 PyErr_SetString(PyExc_TypeError, "Didn't find a class name for given argument object.");
130                 return ret;
131         }
132
133         GType type = g_type_from_name(PyString_AS_STRING(pcname));
134
135         if(!type) {
136                
137                 PyErr_SetString(PyExc_TypeError, "Expected argument object registered in GType system.");
138                 return ret;
139         }
140
141         if(g_type_parent(type) != MIDGARD_TYPE_DBOBJECT) {
142
143                 if(g_type_parent(type) != MIDGARD_TYPE_OBJECT) {
144                
145                         PyErr_SetString(PyExc_TypeError, "Expected argument of type midgard_dbobject (or derived class).");
146                         ret = 0;
147                
148                 } else {
149
150                         ret = 1;
151                 }
152         }
153
154         if(ret == 1) {
155
156                 if(G_OBJECT(((PyGObject *)object)->obj) == NULL) {
157                        
158                         PyErr_SetString(PyExc_TypeError, "Can not find underlying GObject object.");
159                         ret = 0;
160                 }
161         }
162
163         return ret;
164 }
165
166 static PyMethodDef pymidgard_storage_methods[] = {
167         { "create_base_storage", (PyCFunction)pymidgard_storage_create_base_storage, METH_NOARGS | METH_STATIC},
168         { "create_class_storage", (PyCFunction)pymidgard_storage_create_class_storage, METH_VARARGS | METH_STATIC},
169         { "update_class_storage", (PyCFunction)pymidgard_storage_update_class_storage, METH_VARARGS | METH_STATIC},
170         { "delete_class_storage", (PyCFunction)pymidgard_storage_delete_class_storage, METH_VARARGS | METH_STATIC},
171         { "class_storage_exists", (PyCFunction)pymidgard_storage_class_storage_exists, METH_VARARGS | METH_STATIC},
172         { NULL, NULL, 0 }
173 };
174
175 PyTypeObject G_GNUC_INTERNAL Pymidgard_storage_Type = {
176     PyObject_HEAD_INIT(NULL)
177     0,                                 /* ob_size */
178     "storage",                   /* tp_name */
179     sizeof(PyGObject),          /* tp_basicsize */
180     0,                                 /* tp_itemsize */
181     /* methods */
182     (destructor)_py_midgard_gobject_destructor,        /* tp_dealloc */
183     (printfunc)0,                      /* tp_print */
184     (getattrfunc)0,       /* tp_getattr */
185     (setattrfunc)0,       /* tp_setattr */
186     (cmpfunc)0,           /* tp_compare */
187     (reprfunc)0,             /* tp_repr */
188     (PyNumberMethods*)0,     /* tp_as_number */
189     (PySequenceMethods*)0, /* tp_as_sequence */
190     (PyMappingMethods*)0,   /* tp_as_mapping */
191     (hashfunc)0,             /* tp_hash */
192     (ternaryfunc)0,          /* tp_call */
193     (reprfunc)0,              /* tp_str */
194     (getattrofunc)0,     /* tp_getattro */
195     (setattrofunc)0,     /* tp_setattro */
196     (PyBufferProcs*)0,  /* tp_as_buffer */
197     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
198     NULL,                        /* Documentation string */
199     (traverseproc)0,     /* tp_traverse */
200     (inquiry)0,             /* tp_clear */
201     (richcmpfunc)0,   /* tp_richcompare */
202     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
203     (getiterfunc)0,          /* tp_iter */
204     (iternextfunc)0,     /* tp_iternext */
205     pymidgard_storage_methods, /* tp_methods */
206     (struct PyMemberDef*)0,              /* tp_members */
207     (struct PyGetSetDef*)0,  /* tp_getset */
208     NULL,                              /* tp_base */
209     NULL,                              /* tp_dict */
210     (descrgetfunc)0,    /* tp_descr_get */
211     (descrsetfunc)0,    /* tp_descr_set */
212     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
213     (initproc)0,             /* tp_init */
214     (allocfunc)0,           /* tp_alloc */
215     (newfunc)0,               /* tp_new */
216     (freefunc)0,             /* tp_free */
217     (inquiry)0              /* tp_is_gc */
218 };
219
220 void py_midgard_storage_register_class(
221                 PyObject *d, gpointer pygobject_type)
222 {
223         pygobject_register_class(d,
224                         "storage",
225                         MIDGARD_TYPE_STORAGE,
226                         &Pymidgard_storage_Type,
227                         Py_BuildValue("(O)", pygobject_type));
228 }
Note: See TracBrowser for help on using the browser.