File: planner-python-plugin.c

package info (click to toggle)
planner 0.14.6-7
  • links: PTS
  • area: main
  • in suites: buster
  • size: 14,600 kB
  • sloc: ansic: 67,239; sh: 10,242; xml: 3,928; sql: 886; makefile: 665; python: 3
file content (179 lines) | stat: -rw-r--r-- 4,706 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2004 Xavier Ordoquy <xordoquy@wanadoo.fr>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <Python.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <pygobject.h>
#include "planner-window.h"
#include "planner-plugin.h"

struct _PlannerPluginPriv {
	GHashTable    *scripts;
};

typedef struct {
	gchar    *filename;
	PyObject *globals;
} PlannerPythonEnv;

static PlannerPythonEnv *planner_python_env_new  (const gchar      *name);
static void              planner_python_env_free (PlannerPythonEnv *env);
static void              python_plugin_execute   (const gchar      *filename,
						  PlannerWindow    *window,
						  GHashTable       *scripts);
void                     plugin_init             (PlannerPlugin    *plugin);
void                     plugin_exit             (PlannerPlugin    *plugin);



static PlannerPythonEnv *
planner_python_env_new (const gchar *filename)
{
	PlannerPythonEnv *env;
	PyObject         *pDict, *pMain;

	env = g_new0 (PlannerPythonEnv,1);
	env->filename = g_strdup (filename);

	pDict = PyImport_GetModuleDict ();
	pMain = PyDict_GetItemString (pDict, "__main__");
	pDict = PyModule_GetDict (pMain);
	env->globals = PyDict_Copy (pDict);

	return env;
}

static void
planner_python_env_free (PlannerPythonEnv *env)
{
	g_free (env->filename);
	PyDict_Clear (env->globals);
	Py_DECREF (env->globals);
	g_free (env);
}

static void
python_plugin_execute (const gchar   *filename,
		       PlannerWindow *window,
		       GHashTable    *scripts)
{
	PlannerPythonEnv *env;
	FILE             *fp;
	PyObject         *pModule;
	PyObject         *py_object;

	env = planner_python_env_new (filename);

	pModule = PyRun_String ("import pygtk\n"
				"pygtk.require('2.0')\n"
				"import gtk\n"
				"import planner\n",
				Py_file_input, env->globals, env->globals);
	if (!pModule) {
		PyErr_Print ();
		planner_python_env_free (env);
		return;
	}

	pModule = PyImport_ImportModuleEx ("plannerui", env->globals, env->globals, Py_None);
	if (!pModule) {
		PyErr_Print ();
		planner_python_env_free (env);
		return;
	}

	py_object = pygobject_new (G_OBJECT (window));
	PyDict_SetItemString (env->globals, "window", py_object);
	Py_DECREF (py_object);

	py_object = pygobject_new (G_OBJECT (planner_window_get_application (window)));
	PyDict_SetItemString (env->globals, "application", py_object);
	Py_DECREF (py_object);

	fp = fopen (filename,"r");
	if (fp) {
		if (PyRun_File (fp, (gchar *) filename, Py_file_input, env->globals, env->globals) == NULL) {
			PyErr_Print ();
		}
		fclose (fp);
		g_hash_table_insert (scripts, env->filename, env);
	} else {
		planner_python_env_free (env);

		/* FIXME: do the free */
		g_warning ("Could not open python script: %s", filename);
	}
}

G_MODULE_EXPORT void
plugin_init (PlannerPlugin *plugin)
{
	PlannerPluginPriv *priv;
	GDir              *dir;
	gchar             *dirname, *full_filename;
	const gchar       *filename;

	priv = g_new0 (PlannerPluginPriv, 1);
	plugin->priv = priv;

	priv->scripts = g_hash_table_new (g_str_hash, g_str_equal);

	Py_Initialize ();
	pygobject_init (-1, -1, -1);

	/* Look in ~/.gnome2/planner/python/  and run the scripts that we find */
	dirname = g_build_filename (g_get_home_dir(), ".gnome2", "planner", "python", NULL);
	dir = g_dir_open (dirname, 0, NULL);
	if (dir == NULL) {
		g_free (dirname);
		return;
	}

	filename = g_dir_read_name (dir);
	while (filename != NULL) {
		if (g_str_has_suffix (filename, ".py")) {
			full_filename = g_build_filename (dirname, filename, NULL);
			python_plugin_execute (full_filename, plugin->main_window, priv->scripts);
			g_free (full_filename);
		}

		filename = g_dir_read_name (dir);
	}

	g_free (dirname);
	g_dir_close (dir);
}

G_MODULE_EXPORT void
plugin_exit (PlannerPlugin *plugin)
{
	PlannerPluginPriv *priv;

	priv = plugin->priv;

	/* FIXME: free everything in the hash table */

	Py_Finalize ();

	g_free (priv);
}