Back to index

gcompris  8.2.2
py-gcompris-class.c
Go to the documentation of this file.
00001 #include "py-gcompris-profile.h"
00002 #include <pygobject.h>
00003 
00004 staticforward PyTypeObject pyGcomprisClassType;
00005 
00006 //static char pyGcomprisClassType_doc[]= "Python GcomprisBoars structure binding";
00007 
00008 
00009 /* Special function created for the python plugin to be able to create
00010  * a pyGcomprisBoardObject form the existing GcomprisBoard structure
00011  */
00012 PyObject*
00013 gcompris_new_pyGcomprisClassObject(GcomprisClass* class)
00014 {
00015   pyGcomprisClassObject* theclass = NULL;
00016 
00017   theclass = PyObject_New(pyGcomprisClassObject, &pyGcomprisClassType);
00018   if (theclass!=NULL)
00019     theclass->cdata = class;
00020 
00021   return (PyObject*)theclass;
00022 }
00023 
00024 
00025 /* Free the python gcompris class */
00026 static void
00027 pyGcomprisClassType_dealloc(pyGcomprisClassObject *self)
00028 {
00029   self->cdata = NULL;
00030   PyObject_DEL(self);
00031 }
00032 
00033 
00034 /* Methods defined in the pyGcomprisClass class */
00035 static PyMethodDef pyGcomprisClassType_methods[] = {
00036         {NULL,          NULL}           /* sentinel */
00037 };
00038 
00039 
00040 /* Return the value of the members contained in the GcomprisClass structure */
00041 static PyObject *
00042 pyGcomprisClassType_getattr(pyGcomprisClassObject *self, char *name)
00043 {
00044     /* int */
00045     if(strcmp(name,"class_id")==0) return Py_BuildValue("i", self->cdata->class_id);
00046     /* int */
00047     if(strcmp(name,"name")==0) return Py_BuildValue("s", self->cdata->name);
00048     /* int */
00049     if(strcmp(name,"wholegroup_id")==0) return Py_BuildValue("i", self->cdata->wholegroup_id);
00050     /* int */
00051     if(strcmp(name,"description")==0) return Py_BuildValue("s", self->cdata->description);
00052 
00053     /* list */
00054     if(strcmp(name,"group_ids")==0){
00055       PyObject *pylist;
00056       GList *list;
00057 
00058       pylist = PyList_New(0);
00059       for (list = self->cdata->group_ids; list !=NULL; list = list->next)
00060        PyList_Append(pylist, Py_BuildValue("i", *((int *)list->data)));
00061 
00062       return pylist;
00063     }
00064     /* list */
00065     if(strcmp(name,"user_ids")==0){
00066       PyObject *pylist;
00067       GList *list;
00068 
00069       pylist = PyList_New(0);
00070       for (list = self->cdata->user_ids; list !=NULL; list = list->next)
00071        PyList_Append(pylist, Py_BuildValue("i", *((int *)list->data)));
00072 
00073       return pylist;
00074     }
00075 
00076   return Py_FindMethod(pyGcomprisClassType_methods, (PyObject *)self, name);
00077 }
00078 
00079 /* Set the value of a GcomprisClass structure member */
00080 static int
00081 pyGcomprisClassType_setattr(pyGcomprisClassObject *self, char *name, PyObject *v)
00082 {
00083   if (self->cdata==NULL) return -1;
00084   if (v==NULL) return -1;
00085 
00086   /*  if (strcmp(name,"level")==0){
00087     value = (int) PyInt_AsLong(v);
00088     if ( value < 0 ) return -1;
00089     self->cdata->level=value;
00090     return 0;
00091     } */
00092   /* members are supposed to be read only */
00093 
00094   return -1;
00095 }
00096 
00097 static PyTypeObject pyGcomprisClassType = {
00098 #if defined(WIN32)
00099   PyObject_HEAD_INIT(NULL)
00100 #else /* ! WIN32 */
00101   PyObject_HEAD_INIT(&PyType_Type)
00102 #endif
00103   0,                                        /*ob_size*/
00104   "pyGcomprisClass",                        /*tp_name*/
00105   sizeof(pyGcomprisClassObject),            /*tp_basicsize*/
00106   0,                                        /*tp_itemsize*/
00107   /* methods */
00108   (destructor)pyGcomprisClassType_dealloc,  /*tp_dealloc*/
00109   0,                                        /*tp_print*/
00110   (getattrfunc)pyGcomprisClassType_getattr, /*tp_getattr*/
00111   (setattrfunc)pyGcomprisClassType_setattr, /*tp_setattr*/
00112   0,                                        /*tp_compare*/
00113   0,                                        /*tp_repr*/
00114   0,                                        /*tp_as_number*/
00115   0,                                        /*tp_as_sequence*/
00116   0,                                        /*tp_as_mapping*/
00117   0,                                        /*tp_hash*/
00118 };