Back to index

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