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

Revision 16508, 9.1 kB (checked in by piotras, 6 months ago)

Defined NO_IMPORT_PYGOBJECT...

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 static PyObject *
27 pymidgard_object_class_factory(PyGObject *self, PyObject *args)
28 {
29         PyObject *pvalue = NULL;
30         const gchar *classname;
31         const gchar *guid;
32         guint id;
33         MgdObject *mobject = NULL;
34
35         if(!PyArg_ParseTuple(args, "s|O", &classname, &pvalue))
36                 return NULL;
37
38         MidgardConnection *mgd =
39                 _py_midgard_connection_singleton_get();
40
41         if(pvalue == NULL) {
42
43                 mobject = midgard_object_new(mgd, classname, NULL);
44
45         } else {
46
47                 GValue gval = gvalue_from_pyobject(pvalue);
48                 pyg_value_from_pyobject(&gval, pvalue);
49                
50                 if(G_IS_VALUE(&gval))
51                         mobject = midgard_object_new(mgd, classname, &gval);
52                        
53                 g_value_unset(&gval);
54         }
55
56         if(mobject == NULL)
57                 PyErr_SetString(PyExc_SystemError, "Failed to initialize new object");
58
59         /* TODO , check if this might be optimized */
60         GValue gval = {0, };
61         g_value_init(&gval, G_TYPE_OBJECT);
62         g_value_set_object(&gval, G_OBJECT(mobject));
63         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);   
64        
65         return pval;
66 }
67        
68 static PyObject *
69 pymidgard_object_class_get_primary_property(PyGObject *self, PyObject *args)
70 {
71         //CHECK_MGD;
72         const gchar *classname;
73
74         if(!PyArg_ParseTuple(args, "s", &classname))
75                 return NULL;
76        
77         GObjectClass *klass = g_type_class_peek(g_type_from_name(classname));
78         if(klass == NULL)
79                 return NULL;
80
81         const gchar *pname =
82                 midgard_object_class_get_primary_property(MIDGARD_OBJECT_CLASS(klass));
83
84         if(pname == NULL)
85                 Py_RETURN_NONE;
86
87         return Py_BuildValue("s", pname);
88 }
89
90 static PyObject *
91 pymidgard_object_class_get_property_parent(PyGObject *self, PyObject *args)
92 {
93         //CHECK_MGD;
94         const gchar *classname;
95
96         if(!PyArg_ParseTuple(args, "s", &classname))
97                 return NULL;
98        
99         GObjectClass *klass = g_type_class_peek(g_type_from_name(classname));
100         if(klass == NULL)
101                 return NULL;
102
103         const gchar *pname =
104                 midgard_object_class_get_property_parent(MIDGARD_OBJECT_CLASS(klass));
105
106         if(pname == NULL)
107                 Py_RETURN_NONE;
108
109         return Py_BuildValue("s", pname);
110 }
111
112 static PyObject *
113 pymidgard_object_class_get_property_up(PyGObject *self, PyObject *args)
114 {
115         //CHECK_MGD;
116         const gchar *classname;
117
118         if(!PyArg_ParseTuple(args, "s", &classname))
119                 return NULL;
120        
121         GObjectClass *klass = g_type_class_peek(g_type_from_name(classname));
122         if(klass == NULL)
123                 return NULL;
124
125         const gchar *pname =
126                 midgard_object_class_get_property_up(MIDGARD_OBJECT_CLASS(klass));
127
128         if(pname == NULL)
129                 Py_RETURN_NONE;
130
131         return Py_BuildValue("s", pname);
132 }
133
134 static PyObject *
135 pymidgard_object_class_list_children(PyGObject *self, PyObject *args)
136 {
137         //CHECK_MGD;
138         const gchar *classname;
139
140         if(!PyArg_ParseTuple(args, "s", &classname))
141                 return NULL;
142
143         GObjectClass *klass = g_type_class_peek(g_type_from_name(classname));
144         if(klass == NULL)
145                 return NULL;
146
147         MidgardObjectClass **klases =
148                 midgard_object_class_list_children(MIDGARD_OBJECT_CLASS(klass));
149
150         guint i = 0;
151         if(klases == NULL) {
152
153                 PyObject *list = PyTuple_New(i);
154                 return list;
155         }
156
157         while(klases[i] != NULL)
158                 i++;
159        
160         PyObject *list = PyTuple_New(i);
161         i = 0;
162         while(klases[i] != NULL) {
163        
164                 const gchar *cname = G_OBJECT_CLASS_NAME(klases[i]);
165                 PyObject *strname = PyString_FromString(cname);
166                 PyTuple_SetItem(list, i, strname);
167                 i++;
168         }
169
170         return list;
171 }
172
173 static PyObject *
174 pymidgard_object_class_get_object_by_guid(PyGObject *self, PyObject *args)
175 {
176         const gchar *guid;
177        
178         if(!PyArg_ParseTuple(args, "s", &guid))
179                 return NULL;
180
181         MidgardConnection *mgd =
182                 _py_midgard_connection_singleton_get();
183
184         MgdObject *mobject = midgard_object_class_get_object_by_guid(mgd, guid);
185
186         /* TODO , check if this migh tbe optimized */
187         GValue gval = {0, };
188         g_value_init(&gval, G_TYPE_OBJECT);
189         g_value_set_object(&gval, G_OBJECT(mobject));
190         PyObject *pvalue = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
191
192         return pvalue;
193 }
194
195 static PyObject *
196 pymidgard_object_class_get_object_by_path(PyGObject *self, PyObject *args)
197 {
198         const gchar *path;
199         const gchar *classname;
200        
201         if(!PyArg_ParseTuple(args, "ss", &classname, &path))
202                 return NULL;
203
204         MidgardConnection *mgd =
205                 _py_midgard_connection_singleton_get();
206
207         MgdObject *mobject =
208                 midgard_object_class_get_object_by_path(mgd, classname, path);
209
210         /* TODO , check if this might be optimized */
211         GValue gval = {0, };
212         g_value_init(&gval, G_TYPE_OBJECT);
213         g_value_set_object(&gval, G_OBJECT(mobject));
214         PyObject *pvalue = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
215
216         return pvalue;
217 }
218
219 static PyObject *
220 pymidgard_object_class_is_multilang(PyGObject *self, PyObject *args)
221 {       
222         const gchar *classname;
223
224         if(!PyArg_ParseTuple(args, "s", &classname))
225                 return NULL;
226
227         MidgardConnection *mgd =
228                 _py_midgard_connection_singleton_get();
229        
230         MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(classname);
231
232         if(!klass)
233                 Py_RETURN_NONE; /* FIXME */
234
235         if(midgard_object_class_is_multilang(klass))
236                 Py_RETURN_TRUE;
237
238         Py_RETURN_FALSE;
239 }
240
241 static PyObject *
242 pymidgard_object_class_undelete(PyGObject *self, PyObject *args)
243 {       
244         const gchar *guid;
245
246         if(!PyArg_ParseTuple(args, "s", &guid))
247                 return NULL;
248
249         MidgardConnection *mgd =
250                 _py_midgard_connection_singleton_get();
251
252         if(midgard_object_class_undelete(mgd, guid))
253                 Py_RETURN_TRUE;
254
255         Py_RETURN_FALSE;
256 }
257
258 static PyMethodDef pymidgard_object_class_methods[] = {
259         { "factory",
260                 (PyCFunction)pymidgard_object_class_factory, METH_VARARGS | METH_STATIC},
261         { "get_primary_property",
262                 (PyCFunction)pymidgard_object_class_get_primary_property, METH_VARARGS | METH_STATIC},
263         { "get_property_parent",
264                 (PyCFunction)pymidgard_object_class_get_property_parent, METH_VARARGS | METH_STATIC},
265         { "get_property_up",
266                 (PyCFunction)pymidgard_object_class_get_property_up, METH_VARARGS | METH_STATIC},
267         { "list_children",
268                 (PyCFunction)pymidgard_object_class_list_children, METH_VARARGS | METH_STATIC},
269         { "get_object_by_guid",
270                 (PyCFunction)pymidgard_object_class_get_object_by_guid, METH_VARARGS | METH_STATIC},
271         { "is_multilang",
272                 (PyCFunction)pymidgard_object_class_is_multilang, METH_VARARGS | METH_STATIC},
273         { "get_object_by_path",
274                 (PyCFunction)pymidgard_object_class_get_object_by_path, METH_VARARGS | METH_STATIC},
275         { "undelete",
276                 (PyCFunction)pymidgard_object_class_undelete, METH_VARARGS | METH_STATIC},
277
278         { NULL, NULL, 0 }
279 };
280
281 PyTypeObject G_GNUC_INTERNAL Pymidgard_object_class_Type = {
282     PyObject_HEAD_INIT(NULL)
283     0,                                 /* ob_size */
284     "object_class",                   /* tp_name */
285     sizeof(PyGObject),          /* tp_basicsize */
286     0,                                 /* tp_itemsize */
287     /* methods */
288     (destructor)0,        /* tp_dealloc */
289     (printfunc)0,                      /* tp_print */
290     (getattrfunc)0,       /* tp_getattr */
291     (setattrfunc)0,       /* tp_setattr */
292     (cmpfunc)0,           /* tp_compare */
293     (reprfunc)0,             /* tp_repr */
294     (PyNumberMethods*)0,     /* tp_as_number */
295     (PySequenceMethods*)0, /* tp_as_sequence */
296     (PyMappingMethods*)0,   /* tp_as_mapping */
297     (hashfunc)0,             /* tp_hash */
298     (ternaryfunc)0,          /* tp_call */
299     (reprfunc)0,              /* tp_str */
300     (getattrofunc)0,     /* tp_getattro */
301     (setattrofunc)0,     /* tp_setattro */
302     (PyBufferProcs*)0,  /* tp_as_buffer */
303     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
304     NULL,                        /* Documentation string */
305     (traverseproc)0,     /* tp_traverse */
306     (inquiry)0,             /* tp_clear */
307     (richcmpfunc)0,   /* tp_richcompare */
308     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
309     (getiterfunc)0,          /* tp_iter */
310     (iternextfunc)0,     /* tp_iternext */
311     pymidgard_object_class_methods, /* tp_methods */
312     (struct PyMemberDef*)0,              /* tp_members */
313     (struct PyGetSetDef*)0,  /* tp_getset */
314     NULL,                              /* tp_base */
315     NULL,                              /* tp_dict */
316     (descrgetfunc)0,    /* tp_descr_get */
317     (descrsetfunc)0,    /* tp_descr_set */
318     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
319     (initproc)0,             /* tp_init */
320     (allocfunc)0,           /* tp_alloc */
321     (newfunc)0,               /* tp_new */
322     (freefunc)0,             /* tp_free */
323     (inquiry)0              /* tp_is_gc */
324 };
325
326 void py_midgard_object_class_register_class(
327                 PyObject *d, gpointer pygobject_type)
328 {
329         pygobject_register_class(d,
330                         "object_class",
331                         MIDGARD_TYPE_OBJECT,
332                         &Pymidgard_object_class_Type,
333                         Py_BuildValue("(O)", pygobject_type));
334 }
Note: See TracBrowser for help on using the browser.