static void gplp_func_exec_action (GOPluginService *service, GnmAction const *action, WorkbookControl *wbc, GOErrorInfo **ret_error) { ServiceLoaderDataUI *loader_data; PyObject *fn, *ret; g_return_if_fail (_PyGObject_API != NULL); GO_INIT_RET_ERROR_INFO (ret_error); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); fn = PyDict_GetItemString (loader_data->ui_actions, action->id); if (fn == NULL) { *ret_error = go_error_info_new_printf (_("Unknown action: %s"), action->id); return; } else if (!PyCallable_Check (fn)) { *ret_error = go_error_info_new_printf ( _("Not a valid function for action: %s"), action->id); return; } ret = PyObject_CallFunction (fn, (char *) "N", py_new_Gui_object (WBC_GTK (wbc))); if (ret == NULL) { *ret_error = go_error_info_new_str (py_exc_to_string ()); PyErr_Clear (); } else { Py_DECREF (ret); } }
static GnmValue * call_python_function_nodes (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv) { GOPluginService *service; ServiceLoaderDataFunctionGroup *loader_data; PyObject *python_fn; GnmFunc const * fndef; GnmValue **values; gint i; GnmValue *ret_value; g_return_val_if_fail (ei != NULL, NULL); g_return_val_if_fail (ei->func_call != NULL, NULL); fndef = ei->func_call->func; service = (GOPluginService *) gnm_func_get_user_data (fndef); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); python_fn = PyDict_GetItemString (loader_data->python_fn_info_dict, (gchar *) gnm_func_get_name (fndef, FALSE)); values = g_new (GnmValue *, argc); for (i = 0; i < argc; i++) { values[i] = gnm_expr_eval (argv[i], ei->pos, GNM_EXPR_EVAL_PERMIT_NON_SCALAR); } ret_value = call_python_function (python_fn, ei->pos, argc, (GnmValue const * const *)values); for (i = 0; i < argc; i++) { value_release (values[i]); } g_free (values); return ret_value; }
static GnmValue * call_python_function_args (GnmFuncEvalInfo *ei, GnmValue const * const *args) { GOPluginService *service; ServiceLoaderDataFunctionGroup *loader_data; PyObject *fn_info_tuple; PyObject *python_fn; GnmFunc const * fndef; gint min_n_args, max_n_args, n_args; g_return_val_if_fail (ei != NULL, NULL); g_return_val_if_fail (ei->func_call != NULL, NULL); g_return_val_if_fail (args != NULL, NULL); fndef = ei->func_call->func; service = (GOPluginService *) gnm_func_get_user_data (fndef); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); fn_info_tuple = PyDict_GetItemString (loader_data->python_fn_info_dict, (gchar *) gnm_func_get_name (fndef, FALSE)); g_assert (fn_info_tuple != NULL); python_fn = PyTuple_GetItem (fn_info_tuple, 2); function_def_count_args (fndef, &min_n_args, &max_n_args); for (n_args = min_n_args; n_args < max_n_args && args[n_args] != NULL; n_args++) { ; } return call_python_function (python_fn, ei->pos, n_args, args); }
static void gplp_func_file_save (G_GNUC_UNUSED GOFileSaver const *fs, GOPluginService *service, GOIOContext *io_context, gconstpointer wb_view, GsfOutput *output) { ServiceLoaderDataFileSaver *saver_data; PyObject *py_workbook; PyObject *save_result = NULL; PyObject *output_wrapper; g_return_if_fail (GO_IS_PLUGIN_SERVICE_FILE_SAVER (service)); g_return_if_fail (output != NULL); g_return_if_fail (_PyGObject_API != NULL); saver_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); py_workbook = py_new_Workbook_object (wb_view_get_workbook (wb_view)); output_wrapper = pygobject_new (G_OBJECT (output)); if (output_wrapper != NULL) { /* wrapping adds a reference */ g_object_unref (output); save_result = PyObject_CallFunction (saver_data->python_func_file_save, (char *) "NO", py_workbook, output_wrapper); Py_DECREF (output_wrapper); } if (save_result != NULL) { Py_DECREF (save_result); } else { go_io_error_string (io_context, py_exc_to_string ()); gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); } }
static gboolean gplp_func_desc_load (GOPluginService *service, char const *name, GnmFuncDescriptor *res) { ServiceLoaderDataFunctionGroup *loader_data; PyObject *fn_info_obj; g_return_val_if_fail (GNM_IS_PLUGIN_SERVICE_FUNCTION_GROUP (service), FALSE); g_return_val_if_fail (name != NULL, FALSE); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); fn_info_obj = PyDict_GetItemString (loader_data->python_fn_info_dict, (gchar *) name); if (fn_info_obj == NULL) { gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); return FALSE; } if (PyTuple_Check (fn_info_obj)) { PyObject *python_args; PyObject *python_fn; if (PyTuple_Size (fn_info_obj) == 3 && (python_args = PyTuple_GetItem (fn_info_obj, 0)) != NULL && PyString_Check (python_args) && (python_fn = PyTuple_GetItem (fn_info_obj, 2)) != NULL && PyCallable_Check (python_fn)) { res->arg_spec = PyString_AsString (python_args); res->help = python_function_get_gnumeric_help ( loader_data->python_fn_info_dict, python_fn, name); res->fn_args = &call_python_function_args; res->fn_nodes = NULL; res->linker = NULL; res->impl_status = GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC; res->test_status = GNM_FUNC_TEST_STATUS_UNKNOWN; return TRUE; } gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); return FALSE; } if (PyCallable_Check (fn_info_obj)) { res->arg_spec = ""; res->help = python_function_get_gnumeric_help ( loader_data->python_fn_info_dict, fn_info_obj, name); res->fn_args = NULL; res->fn_nodes = &call_python_function_nodes; res->linker = NULL; res->impl_status = GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC; res->test_status = GNM_FUNC_TEST_STATUS_UNKNOWN; return TRUE; } gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); return FALSE; }
static void gplp_unload_service_function_group (GOPluginLoader *loader, GOPluginService *service, GOErrorInfo **ret_error) { ServiceLoaderDataFunctionGroup *loader_data; g_return_if_fail (GNM_IS_PYTHON_PLUGIN_LOADER (loader)); g_return_if_fail (GNM_IS_PLUGIN_SERVICE_FUNCTION_GROUP (service)); GO_INIT_RET_ERROR_INFO (ret_error); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); Py_DECREF (loader_data->python_fn_info_dict); }
static void go_plugin_service_resource_read_xml (GOPluginService *service, xmlNode *tree, GOErrorInfo **ret_error) { GOPluginServiceResource *sr = GO_PLUGIN_SERVICE_RESOURCE (service); char *data = NULL; gsize length; xmlChar *file; GO_INIT_RET_ERROR_INFO (ret_error); sr->id = xml2c (go_xml_node_get_cstr (tree, "id")); if (!sr->id) goto error; file = go_xml_node_get_cstr (tree, "file"); if (file) { char *absfile; gboolean ok; if (!g_path_is_absolute (CXML2C (file))) { char const *dir = go_plugin_get_dir_name (go_plugin_service_get_plugin (service)); absfile = g_build_filename (dir, CXML2C (file), NULL); } else absfile = g_strdup (CXML2C (file)); xmlFree (file); ok = g_file_get_contents (absfile, &data, &length, NULL); g_free (absfile); if (!ok) goto error; } else { data = xml2c (go_xml_node_get_cstr (tree, "data")); length = strlen (data); } if (!data) goto error; /* No encoding case */ sr->value = g_string_sized_new (length); g_string_append_len (sr->value, data, length); g_free (data); return; error: *ret_error = go_error_info_new_str (_("Invalid resource service")); g_free (data); }
static void gplp_func_file_open (GOFileOpener const *fo, GOPluginService *service, GOIOContext *io_context, gpointer wb_view, GsfInput *input) { ServiceLoaderDataFileOpener *loader_data; Sheet *sheet, *old_sheet; PyObject *open_result = NULL; PyObject *input_wrapper; printf("gplp_func_file_open(start)\n"); g_return_if_fail (GO_IS_PLUGIN_SERVICE_FILE_OPENER (service)); g_return_if_fail (input != NULL); g_return_if_fail (_PyGObject_API != NULL); old_sheet = wb_view_cur_sheet (wb_view); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); sheet = sheet_new (wb_view_get_workbook (wb_view), _("Some name"), gnm_sheet_get_max_cols (old_sheet), gnm_sheet_get_max_rows (old_sheet)); input_wrapper = pygobject_new (G_OBJECT (input)); if (input_wrapper != NULL) { /* wrapping adds a reference */ g_object_unref (G_OBJECT (input)); open_result = PyObject_CallFunction (loader_data->python_func_file_open, (char *) "NO", py_new_Sheet_object (sheet), input_wrapper); Py_DECREF (input_wrapper); } if (open_result != NULL) { Py_DECREF (open_result); workbook_sheet_attach (wb_view_get_workbook (wb_view), sheet); } else { go_io_error_string (io_context, py_exc_to_string ()); gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); g_object_unref (sheet); } printf("gplp_func_file_open(end)\n"); }
static gboolean gplp_func_file_probe (GOFileOpener const *fo, GOPluginService *service, GsfInput *input, GOFileProbeLevel pl) { ServiceLoaderDataFileOpener *loader_data; PyObject *probe_result = NULL; PyObject *input_wrapper; gboolean result; printf("gplp_func_file_probe(start)\n"); g_return_val_if_fail (GO_IS_PLUGIN_SERVICE_FILE_OPENER (service), FALSE); g_return_val_if_fail (input != NULL, FALSE); g_return_val_if_fail (_PyGObject_API != NULL, FALSE); loader_data = g_object_get_data (G_OBJECT (service), "loader_data"); SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service)); input_wrapper = pygobject_new (G_OBJECT (input)); if (input_wrapper == NULL) { g_warning ("%s", py_exc_to_string ()); gnm_python_clear_error_if_needed (SERVICE_GET_LOADER (service)->py_object); } if (input_wrapper != NULL && loader_data->python_func_file_probe != NULL) { /* wrapping adds a reference */ g_object_unref (G_OBJECT (input)); probe_result = PyObject_CallFunction (loader_data->python_func_file_probe, (char *) "O", input_wrapper); Py_DECREF (input_wrapper); } if (probe_result != NULL) { result = PyObject_IsTrue (probe_result); Py_DECREF (probe_result); } else { PyErr_Clear (); result = FALSE; } printf("gplp_func_file_probe(end)\n"); return result; }