Back to index

gcompris  8.2.2
Functions | Variables
python.c File Reference
#include <Python.h>
#include <pygobject.h>
#include "gcompris/gcompris.h"
#include "py-gcompris-board.h"
#include "py-mod-gcompris.h"
#include "py-gcompris-profile.h"

Go to the source code of this file.

Functions

static void pythonboard_init (GcomprisBoard *agcomprisBoard)
static void pythonboard_start (GcomprisBoard *agcomprisBoard)
static void pythonboard_pause (gboolean pause)
static void pythonboard_end (void)
static gboolean pythonboard_is_our_board (GcomprisBoard *agcomprisBoard)
static gint pythonboard_key_press (guint keyval, gchar *commit_str, gchar *preedit_str)
static void pythonboard_ok (void)
static void pythonboard_set_level (guint level)
static void pythonboard_config (void)
static void pythonboard_repeat (void)
static void pythongc_board_config_start (GcomprisBoard *agcomprisBoard, GcomprisProfile *aProfile)
static void pythongc_board_config_stop (void)
GList * get_pythonboards_list ()

Variables

static GcomprisBoard * gcomprisBoard = NULL
static PyObject * python_gcomprisBoard = NULL
static PyObject * python_board_module = NULL
static PyObject * python_board_instance = NULL
static GcomprisBoard * gcomprisBoard_config = NULL
static PyObject * python_gcomprisBoard_config = NULL
static PyObject * python_board_config_module = NULL
static PyObject * python_board_config_instance = NULL
static gboolean pythonboard_is_ready = FALSE
static BoardPlugin menu_bp
static BoardPlugin menu_bp_no_config
static GList * config_boards = NULL
static gboolean python_run_by_config = FALSE

Function Documentation

Definition at line 121 of file python.c.

{
  GList *pythonboards_list = NULL;
  GList *boards_list = gc_menu_get_boards();
  GList *list;
  GcomprisBoard *board;

  for (list = boards_list; list != NULL; list = list->next){
    board = (GcomprisBoard *) list->data;
    if (g_ascii_strncasecmp(board->type, "python", 6)==0)
      pythonboards_list = g_list_append(pythonboards_list, board);
  }

  return pythonboards_list;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void pythonboard_config ( void  ) [static]

Definition at line 500 of file python.c.

                                    {
  PyObject* result = NULL;
  result = PyObject_CallMethod(python_board_instance, "config", NULL);
  if( result != NULL){
    Py_DECREF(result);
  } else {
    PyErr_Print();
  }
}
static void pythonboard_end ( void  ) [static]

Definition at line 406 of file python.c.

                                  {
  PyObject* result = NULL;

  if(python_gcomprisBoard!=NULL){
    result = PyObject_CallMethod(python_board_instance, "end", NULL);
    if( result == NULL){
      PyErr_Print();
    } else {
      Py_DECREF(result);
    }
    Py_XDECREF(python_board_module);
    Py_XDECREF(python_board_instance);
    Py_XDECREF(python_gcomprisBoard);
    Py_Finalize();
  }
}
static void pythonboard_init ( GcomprisBoard *  agcomprisBoard) [static]

Definition at line 138 of file python.c.

                                                {
  PyObject* main_module;
  PyObject* globals;
  gchar* execstr;
  gchar* userplugindir;

  char* board_file_name;
  char* boardclass;
  gchar *boarddir;
  PyObject* module_dict;
  PyObject* py_boardclass;

  GcomprisProperties *properties = gc_prop_get();

  if (pythonboard_is_ready)
    return ;

  /* Initialize the python interpreter */
  Py_Initialize();

  static char *python_args[]={ "" };
  PySys_SetArgv( 1, python_args);

  pythonboard_is_ready = TRUE;

  main_module = PyImport_AddModule("__main__"); /* Borrowed reference */
  globals = PyModule_GetDict(main_module); /* Borrowed reference */

  if(globals==NULL){
    g_warning("! Python disabled: Cannot get info from the python interpreter.\n");
    pythonboard_is_ready = FALSE;
  } else {
    /* Add the python plugins dir to the python's search path */
#ifndef DISABLE_USER_PLUGIN_DIR
    userplugindir = g_strconcat(g_get_home_dir(), "/.gcompris/Plugins/", NULL);
    execstr = g_strdup_printf("import sys; sys.path.append('%s/python'); sys.path.append('%s')",
                           userplugindir, properties->package_python_plugin_dir);
    g_free(userplugindir);
#else
    execstr = g_strdup_printf("import sys; sys.path.append('%s')",properties->package_python_plugin_dir );
#endif

    g_warning("Executing %s\n", execstr);
    if(PyRun_SimpleString(execstr)!=0){
      pythonboard_is_ready = FALSE;
      g_warning("! Python disabled: Cannot add plugins dir into search path\n");
    } else {
      /* Try to import pygtk modules */
      g_free(execstr);
      execstr = g_strdup("import gtk; import gtk.gdk");
      if(PyRun_SimpleString(execstr)!=0){
       pythonboard_is_ready = FALSE;
       g_warning("! Python disabled: Cannot import pygtk modules\n");
      } else {
       /* Try to import gcompris modules */

       /* Load the gcompris modules */
       python_gcompris_module_init();

       g_free(execstr);
       execstr = g_strdup("import gcompris; import gcompris.bonus; "
                        "import gcompris.score; import gcompris.sound;"
                        "import gcompris.skin; import gcompris.timer;"
                        "import gcompris.utils; import gcompris.anim");
       if(PyRun_SimpleString(execstr)!=0){
         pythonboard_is_ready = FALSE;
         g_warning("! Python disabled: Cannot import gcompris modules\n");
       } else {
         GList *python_boards;
         GList *list;

         /* Get the list of python boards */
         python_boards = get_pythonboards_list();

         /* Search in the list each one with a config entry */
         for(list = python_boards; list != NULL; list = list->next) {
           GcomprisBoard *board = (GcomprisBoard *) list->data;

           /* Python is now initialized we create some usefull variables */
           board_file_name = strchr(board->type, ':')+1;
           boardclass = g_strdup_printf("Gcompris_%s", board_file_name);

           /* Test if board come with -L option */

           g_warning("board_dir: %s package_data_dir %s",
                    board->board_dir,
                    properties->package_python_plugin_dir);

           if (strcmp(board->board_dir, properties->package_python_plugin_dir)!=0){
             boarddir = g_strdup_printf("sys.path.append('%s/')", board->board_dir);

             PyRun_SimpleString(boarddir);
             g_free(boarddir);
           }

           /* Insert the board module into the python's interpreter */
           python_board_module = PyImport_ImportModuleEx(board_file_name,
                                                   globals,
                                                   globals,
                                                   NULL);

           if(python_board_module!=NULL){
             /* Get the module dictionnary */
             module_dict = PyModule_GetDict(python_board_module);

             /* Get the python board class */
             py_boardclass = PyDict_GetItemString(module_dict, boardclass);

             if (PyObject_HasAttrString( py_boardclass, "config_start")) {
              config_boards = g_list_append(config_boards, board);
              g_warning("The board '%s' has a configuration entry",
                       board_file_name);
             }
           }

           g_free(boardclass);
         }

         g_list_free(python_boards);
       }
      }
    }
    g_free(execstr);

  }

  /* Finalize the python interpreter */
  Py_Finalize();

}

Here is the call graph for this function:

static gboolean pythonboard_is_our_board ( GcomprisBoard *  agcomprisBoard) [static]

Definition at line 426 of file python.c.

                                                                       {

  if (gcomprisBoard->plugin)
    return TRUE;

  if(pythonboard_is_ready) {
    if (gcomprisBoard!=NULL) {

      if (g_ascii_strncasecmp(gcomprisBoard->type, "python", 6)==0) {

       /* Set the plugin entry */
       if (g_list_find (config_boards, gcomprisBoard)){
         gcomprisBoard->plugin = &menu_bp;
       } else {
         gcomprisBoard->plugin = &menu_bp_no_config;
       }

       return TRUE;
      }
    }
  }
  gcomprisBoard->plugin=NULL;
  return FALSE;
}
static gint pythonboard_key_press ( guint  keyval,
gchar *  commit_str,
gchar *  preedit_str 
) [static]

Definition at line 454 of file python.c.

                                                                                       {
  PyObject* result = NULL;

  result = PyObject_CallMethod(python_board_instance, "key_press", "iss", keyval, commit_str, preedit_str);

  if (result==NULL) return FALSE;

  if (PyInt_Check(result) && (PyInt_AsLong(result)>0)){
    Py_DECREF(result);
    return TRUE;
  } else {
    Py_DECREF(result);
    return FALSE;
  }
}
static void pythonboard_ok ( void  ) [static]

Definition at line 473 of file python.c.

                                 {
  PyObject* result = NULL;
  result = PyObject_CallMethod(python_board_instance, "ok", NULL);
  if( result != NULL){
    Py_DECREF(result);
  } else {
    PyErr_Print();
  }
}
static void pythonboard_pause ( gboolean  pause) [static]

Definition at line 389 of file python.c.

                                              {
  PyObject* result = NULL;

  result = PyObject_CallMethod(python_board_instance, "pause", "i", pause);
  if( result != NULL){
    Py_DECREF(result);
  } else {
    PyErr_Print();
  }
}
static void pythonboard_repeat ( void  ) [static]

Definition at line 513 of file python.c.

                                     {
  PyObject* result = NULL;
  result = PyObject_CallMethod(python_board_instance, "repeat", NULL);
  if( result != NULL){
    Py_DECREF(result);
  } else {
    PyErr_Print();
  }
}
static void pythonboard_set_level ( guint  level) [static]

Definition at line 486 of file python.c.

                                               {
  PyObject* result = NULL;

  result = PyObject_CallMethod(python_board_instance, "set_level", "i", level);
  if( result != NULL){
    Py_DECREF(result);
  } else {
    PyErr_Print();
  }
}
static void pythonboard_start ( GcomprisBoard *  agcomprisBoard) [static]

Definition at line 279 of file python.c.

                                                 {
  PyObject* main_module;
  PyObject* py_function_result;
  PyObject* module_dict;
  PyObject* py_boardclass;
  PyObject* py_boardclass_args;
  PyObject* globals;
  static char *python_args[]={ "" };
  static char* python_prog_name="gcompris";
  char* boarddir;
  char* boardclass;
  char* board_file_name;
  gchar *userplugindir;

  GcomprisProperties *properties = gc_prop_get();

  if(agcomprisBoard!=NULL){
    /* Initialize the python interpreter */
    Py_SetProgramName(python_prog_name);
    Py_Initialize();

    PySys_SetArgv(1, python_args);

    init_pygobject();

    main_module = PyImport_AddModule("__main__");
    globals = PyModule_GetDict(main_module);

    if(globals==NULL){
      g_print("Cannot get info from the python interpreter. Seems there is a problem with this one.\n");
      return;
    } else {
      gcomprisBoard = agcomprisBoard;
    }

    /* Add the python plugins dir to the python's search path */
#ifndef DISABLE_USER_PLUGIN_DIR
    userplugindir = g_strconcat(g_get_home_dir(), "/.gcompris/Plugins/", NULL);
    boarddir = g_strdup_printf("import sys; sys.path.append('%s/python'); sys.path.append('%s')",
                            userplugindir,
                            properties->package_python_plugin_dir);
#else
    boarddir = g_strdup_printf("import sys; sys.path.append('%s')",properties->package_python_plugin_dir );
#endif

    PyRun_SimpleString(boarddir);
    g_free(boarddir);


    /* Test if board come with -L option */
    if (strcmp(gcomprisBoard->board_dir, properties->package_data_dir)!=0){
      boarddir = g_strdup_printf("sys.path.append('%s/../python/')", gcomprisBoard->board_dir);

      PyRun_SimpleString(boarddir);
      g_free(boarddir);
    }


#ifndef DISABLE_USER_PLUGIN_DIR
    g_free(userplugindir);
#endif

    /* Load the gcompris modules */
    python_gcompris_module_init();

    /* Python is now initialized we create some usefull variables */
    board_file_name = strchr(agcomprisBoard->type, ':')+1;
    boardclass = g_strdup_printf("Gcompris_%s", board_file_name);

    /* Insert the board module into the python's interpreter */
    python_board_module = PyImport_ImportModuleEx(board_file_name,
                                            globals,
                                            globals,
                                            NULL);

    if(python_board_module!=NULL){
      /* Get the module dictionnary */
      module_dict = PyModule_GetDict(python_board_module);

      /* Get the python board class */
      py_boardclass = PyDict_GetItemString(module_dict, boardclass);

      /* Create a python gcompris board */
      python_gcomprisBoard=gcompris_new_pyGcomprisBoardObject(agcomprisBoard);

      /* Create an instance of the board class */
      py_boardclass_args = PyTuple_New(1);
      Py_INCREF(python_gcomprisBoard);
      PyTuple_SetItem(py_boardclass_args, 0, python_gcomprisBoard);
      python_board_instance = PyInstance_New(py_boardclass, py_boardclass_args, NULL);
      Py_DECREF(py_boardclass_args);

      /* Call the function */
      py_function_result = PyObject_CallMethod(python_board_instance, "start", NULL);
      if( py_function_result != NULL){
       Py_DECREF(py_function_result);
      } else {
       PyErr_Print();
      }
    } else {
      PyErr_Print();
    }

    g_free(boardclass);
  }
}

Here is the call graph for this function:

static void pythongc_board_config_start ( GcomprisBoard *  agcomprisBoard,
GcomprisProfile aProfile 
) [static]

Definition at line 540 of file python.c.

{
  GcomprisProperties *properties = gc_prop_get();
  PyObject* py_function_result;
  PyObject* module_dict;
  PyObject* py_boardclass;
  PyObject* py_boardclass_args;
  PyObject* globals;
  static char *python_args[]={ "" };
  static char* python_prog_name="gcompris";
  char* boardclass;
  char* board_file_name;
  PyObject* main_module;
  char* boarddir;
  gchar *userplugindir;

  g_assert (agcomprisBoard != NULL);

  if(!Py_IsInitialized()){
    /* Initialize the python interpreter */
    Py_SetProgramName(python_prog_name);
    Py_Initialize();

    PySys_SetArgv(1, python_args);

    init_pygobject();

    main_module = PyImport_AddModule("__main__");
    globals = PyModule_GetDict(main_module);

    if(globals==NULL){
      g_print("Cannot get info from the python interpreter. Seems there is a problem with this one.\n");
      return;
    } else {
      gcomprisBoard_config = agcomprisBoard;
    }

    /* Add the python plugins dir to the python's search path */
#ifndef DISABLE_USER_PLUGIN_DIR
    userplugindir = g_strconcat(g_get_home_dir(), "/.gcompris/Plugins/", NULL);
    boarddir = g_strdup_printf("import sys; sys.path.append('%s/python'); sys.path.append('%s'); sys.path.append('%s')",
                            userplugindir,
                            properties->package_python_plugin_dir,
                            gcomprisBoard_config->board_dir);
#else
    boarddir = g_strdup_printf("import sys; sys.path.append('%s')", properties->package_python_plugin_dir );
#endif

    PyRun_SimpleString(boarddir);
    g_free(boarddir);

#ifndef DISABLE_USER_PLUGIN_DIR
    g_free(userplugindir);
#endif

    /* Load the gcompris modules */
    python_gcompris_module_init();

    python_run_by_config = TRUE;

  }
  else {
    main_module = PyImport_AddModule("__main__"); /* Borrowed reference */
    globals = PyModule_GetDict(main_module); /* Borrowed reference */
  }

  /* Python is now initialized we create some usefull variables */
  board_file_name = strchr(agcomprisBoard->type, ':')+1;
  boardclass = g_strdup_printf("Gcompris_%s", board_file_name);

  /* Insert the board module into the python's interpreter */
  python_board_config_module = PyImport_ImportModuleEx(board_file_name,
                                                 globals,
                                                 globals,
                                                 NULL);

  if(python_board_config_module!=NULL){
    /* Get the module dictionnary */
    module_dict = PyModule_GetDict(python_board_config_module);

    /* Get the python board class */
    py_boardclass = PyDict_GetItemString(module_dict, boardclass);

    /* Create a python gcompris board */
    python_gcomprisBoard_config=gcompris_new_pyGcomprisBoardObject(agcomprisBoard);
    /* Create an instance of the board class */
    py_boardclass_args = PyTuple_New(1);
    Py_INCREF(python_gcomprisBoard_config);
    PyTuple_SetItem(py_boardclass_args, 0, python_gcomprisBoard_config);
    python_board_config_instance = PyInstance_New(py_boardclass, py_boardclass_args, NULL);
    Py_DECREF(py_boardclass_args);

    py_function_result = PyObject_CallMethod(python_board_config_instance,
                                        "config_start",
                                        "O",
                                        gcompris_new_pyGcomprisProfileObject(aProfile));

    if( py_function_result != NULL){
      Py_DECREF(py_function_result);
    } else {
      PyErr_Print();
    }
  } else {
    PyErr_Print();
  }

  g_free(boardclass);
}

Here is the call graph for this function:

static void pythongc_board_config_stop ( void  ) [static]

Definition at line 657 of file python.c.

                                             {
  PyObject* result = NULL;

  if(python_gcomprisBoard_config!=NULL){
    result = PyObject_CallMethod(python_board_config_instance, "config_stop", NULL);
    if( result == NULL){
      PyErr_Print();
    } else {
      Py_DECREF(result);
    }
    Py_XDECREF(python_board_config_module);
    Py_XDECREF(python_board_config_instance);
    Py_XDECREF(python_gcomprisBoard_config);
    if (python_run_by_config){
      Py_Finalize();
      python_run_by_config = FALSE;
    }
  }
}

Variable Documentation

GList* config_boards = NULL [static]

Definition at line 118 of file python.c.

GcomprisBoard* gcomprisBoard = NULL [static]

Definition at line 27 of file python.c.

GcomprisBoard* gcomprisBoard_config = NULL [static]

Definition at line 33 of file python.c.

BoardPlugin menu_bp [static]
Initial value:
  {
    NULL,
    NULL,
    N_("Python Board"),
    N_("Special board that embeds python into gcompris."),
    "Olivier Samyn <osamyn@ulb.ac.be>",
    pythonboard_init,
    NULL,
    NULL,
    NULL,
    pythonboard_start,
    pythonboard_pause,
    pythonboard_end,
    pythonboard_is_our_board,
    pythonboard_key_press,
    pythonboard_ok,
    pythonboard_set_level,
    pythonboard_config,
    pythonboard_repeat,
    pythongc_board_config_start,
    pythongc_board_config_stop
  }

Definition at line 58 of file python.c.

Initial value:
  {
    NULL,
    NULL,
    N_("Python Board"),
    N_("Special board that embeds python into gcompris."),
    "Olivier Samyn <osamyn@ulb.ac.be>",
    pythonboard_init,
    NULL,
    NULL,
    NULL,
    pythonboard_start,
    pythonboard_pause,
    pythonboard_end,
    pythonboard_is_our_board,
    pythonboard_key_press,
    pythonboard_ok,
    pythonboard_set_level,
    pythonboard_config,
    pythonboard_repeat,
    NULL,
    NULL
  }

Definition at line 82 of file python.c.

PyObject* python_board_config_instance = NULL [static]

Definition at line 36 of file python.c.

PyObject* python_board_config_module = NULL [static]

Definition at line 35 of file python.c.

PyObject* python_board_instance = NULL [static]

Definition at line 31 of file python.c.

PyObject* python_board_module = NULL [static]

Definition at line 30 of file python.c.

PyObject* python_gcomprisBoard = NULL [static]

Definition at line 29 of file python.c.

PyObject* python_gcomprisBoard_config = NULL [static]

Definition at line 34 of file python.c.

gboolean python_run_by_config = FALSE [static]

Definition at line 537 of file python.c.

gboolean pythonboard_is_ready = FALSE [static]

Definition at line 54 of file python.c.