r1467@localhost: muntyan | 2005-12-22 00:43:45 -0600
Started separating python stuffmaster
parent
782c55eec2
commit
1a174a6414
|
@ -23,20 +23,8 @@ mooapp_sources = \
|
|||
$(mooapp)/mooappoutput.c \
|
||||
$(mooapp)/mooappoutput.h
|
||||
|
||||
mooapp_python_sources = \
|
||||
$(mooapp)/mooapp-python.h \
|
||||
$(mooapp)/moopythonconsole.c \
|
||||
$(mooapp)/moopythonconsole.h \
|
||||
$(mooapp)/moopython.c \
|
||||
$(mooapp)/moopython.h
|
||||
|
||||
if MOO_USE_PYTHON
|
||||
mooapp_sources += $(mooapp_python_sources)
|
||||
endif MOO_USE_PYTHON
|
||||
|
||||
mooapp_extra_dist = \
|
||||
$(mooapp)/gpl \
|
||||
$(mooapp_python_sources)
|
||||
$(mooapp)/gpl
|
||||
|
||||
$(mooapp)/mooappabout-glade.h: $(mooapp_srcdir)/glade/mooappabout.glade $(XML2H)
|
||||
mkdir -p $(mooapp)
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* mooapp/mooapp-python.h
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifndef MOOAPP_MOOAPP_PYTHON_H
|
||||
#define MOOAPP_MOOAPP_PYTHON_H
|
||||
|
||||
#include "mooapp/mooapp.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
void moo_app_python_execute_file (GtkWindow *parent);
|
||||
gboolean moo_app_python_run_string (MooApp *app,
|
||||
const char *string);
|
||||
gboolean moo_app_python_run_file (MooApp *app,
|
||||
const char *filename);
|
||||
|
||||
GtkWidget *moo_app_get_python_console (MooApp *app);
|
||||
void moo_app_show_python_console (MooApp *app);
|
||||
void moo_app_hide_python_console (MooApp *app);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MOOAPP_MOOAPP_PYTHON_H */
|
|
@ -16,11 +16,7 @@
|
|||
#endif
|
||||
|
||||
#ifdef MOO_USE_PYTHON
|
||||
#include <Python.h>
|
||||
#include "mooapp/moopythonconsole.h"
|
||||
#include "mooapp/mooapp-python.h"
|
||||
#include "mooapp/moopython.h"
|
||||
#include "moopython/mooplugin-python.h"
|
||||
#include "moopython/moopython.h"
|
||||
#endif
|
||||
|
||||
#define WANT_MOO_APP_CMD_CHARS
|
||||
|
@ -36,6 +32,7 @@
|
|||
#include "mooutils/moodialogs.h"
|
||||
#include "mooutils/moostock.h"
|
||||
#include "mooutils/mooutils-misc.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#ifdef VERSION
|
||||
|
@ -52,9 +49,6 @@ gboolean initmoo (void);
|
|||
static MooApp *moo_app_instance = NULL;
|
||||
static MooAppInput *moo_app_input = NULL;
|
||||
static MooAppOutput *moo_app_output = NULL;
|
||||
#ifdef MOO_USE_PYTHON
|
||||
static MooPython *moo_app_python = NULL;
|
||||
#endif
|
||||
|
||||
|
||||
struct _MooAppPrivate {
|
||||
|
@ -679,13 +673,14 @@ moo_app_get_rc_file_name (MooApp *app)
|
|||
|
||||
|
||||
#ifdef MOO_USE_PYTHON
|
||||
void moo_app_python_execute_file (G_GNUC_UNUSED GtkWindow *parent_window)
|
||||
void
|
||||
moo_app_python_execute_file (G_GNUC_UNUSED GtkWindow *parent_window)
|
||||
{
|
||||
GtkWidget *parent;
|
||||
const char *filename = NULL;
|
||||
FILE *file;
|
||||
|
||||
g_return_if_fail (moo_app_python != NULL);
|
||||
g_return_if_fail (moo_python_running ());
|
||||
|
||||
parent = parent_window ? GTK_WIDGET (parent_window) : NULL;
|
||||
if (!filename)
|
||||
|
@ -694,21 +689,25 @@ void moo_app_python_execute_file (G_GNUC_UNUSED GtkWindow *parent_
|
|||
"Choose Python Script to Execute",
|
||||
"python_exec_file", NULL);
|
||||
|
||||
if (!filename) return;
|
||||
if (!filename)
|
||||
return;
|
||||
|
||||
file = fopen (filename, "r");
|
||||
|
||||
if (!file)
|
||||
{
|
||||
moo_error_dialog (parent, "Could not open file", NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
PyObject *res = (PyObject*)moo_python_run_file (moo_app_python, file, filename);
|
||||
MooPyObject *res = moo_python_run_file (file, filename);
|
||||
|
||||
fclose (file);
|
||||
|
||||
if (res)
|
||||
Py_XDECREF (res);
|
||||
moo_Py_DECREF (res);
|
||||
else
|
||||
PyErr_Print ();
|
||||
moo_PyErr_Print ();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -718,26 +717,27 @@ moo_app_python_run_file (MooApp *app,
|
|||
const char *filename)
|
||||
{
|
||||
FILE *file;
|
||||
PyObject *res;
|
||||
MooPyObject *res;
|
||||
|
||||
g_return_val_if_fail (MOO_IS_APP (app), FALSE);
|
||||
g_return_val_if_fail (filename != NULL, FALSE);
|
||||
g_return_val_if_fail (moo_app_python != NULL, FALSE);
|
||||
g_return_val_if_fail (moo_python_running (), FALSE);
|
||||
|
||||
file = fopen (filename, "r");
|
||||
g_return_val_if_fail (file != NULL, FALSE);
|
||||
|
||||
res = moo_python_run_file (moo_app_python, file, filename);
|
||||
res = moo_python_run_file (file, filename);
|
||||
|
||||
fclose (file);
|
||||
|
||||
if (res)
|
||||
{
|
||||
Py_XDECREF (res);
|
||||
moo_Py_DECREF (res);
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_Print ();
|
||||
moo_PyErr_Print ();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
@ -747,54 +747,27 @@ gboolean
|
|||
moo_app_python_run_string (MooApp *app,
|
||||
const char *string)
|
||||
{
|
||||
PyObject *res;
|
||||
MooPyObject *res;
|
||||
|
||||
g_return_val_if_fail (MOO_IS_APP (app), FALSE);
|
||||
g_return_val_if_fail (string != NULL, FALSE);
|
||||
g_return_val_if_fail (moo_app_python != NULL, FALSE);
|
||||
g_return_val_if_fail (moo_python_running (), FALSE);
|
||||
|
||||
res = moo_python_run_string (moo_app_python, string, FALSE);
|
||||
res = moo_python_run_string (string, FALSE);
|
||||
|
||||
if (res)
|
||||
{
|
||||
Py_XDECREF (res);
|
||||
moo_Py_DECREF (res);
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_Print ();
|
||||
moo_PyErr_Print ();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GtkWidget*
|
||||
moo_app_get_python_console (MooApp *app)
|
||||
{
|
||||
g_return_val_if_fail (MOO_IS_APP (app), NULL);
|
||||
g_return_val_if_fail (moo_app_python != NULL, NULL);
|
||||
return moo_python_get_console (moo_app_python);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_app_show_python_console (MooApp *app)
|
||||
{
|
||||
g_return_if_fail (MOO_IS_APP (app));
|
||||
g_return_if_fail (moo_app_python != NULL);
|
||||
gtk_window_present (GTK_WINDOW (moo_app_get_python_console (app)));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_app_hide_python_console (MooApp *app)
|
||||
{
|
||||
g_return_if_fail (MOO_IS_APP (app));
|
||||
g_return_if_fail (moo_app_python != NULL);
|
||||
gtk_widget_hide (moo_app_get_python_console (app));
|
||||
}
|
||||
|
||||
|
||||
static guint strv_length (char **argv)
|
||||
{
|
||||
guint len = 0;
|
||||
|
@ -1062,10 +1035,9 @@ start_python (G_GNUC_UNUSED MooApp *app)
|
|||
{
|
||||
G_GNUC_UNUSED char **plugin_dirs;
|
||||
|
||||
moo_app_python = moo_python_new (app->priv->use_python_console);
|
||||
moo_python_start (moo_app_python,
|
||||
strv_length (app->priv->argv),
|
||||
moo_python_start (strv_length (app->priv->argv),
|
||||
app->priv->argv);
|
||||
|
||||
#ifdef MOO_USE_PYGTK
|
||||
if (initmoo ())
|
||||
{
|
||||
|
@ -1076,16 +1048,10 @@ start_python (G_GNUC_UNUSED MooApp *app)
|
|||
}
|
||||
else
|
||||
{
|
||||
PyErr_Print ();
|
||||
moo_PyErr_Print ();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
moo_app_python = moo_python_get_instance ();
|
||||
if (moo_app_python)
|
||||
g_object_ref (moo_app_python);
|
||||
}
|
||||
#endif /* !MOO_USE_PYTHON */
|
||||
}
|
||||
|
||||
|
@ -1217,12 +1183,8 @@ static void moo_app_quit_real (MooApp *app)
|
|||
}
|
||||
|
||||
#ifdef MOO_USE_PYTHON
|
||||
if (moo_app_python)
|
||||
{
|
||||
moo_python_shutdown (moo_app_python);
|
||||
g_object_unref (moo_app_python);
|
||||
moo_app_python = NULL;
|
||||
}
|
||||
if (moo_python_running ())
|
||||
moo_python_shutdown ();
|
||||
#endif
|
||||
|
||||
list = g_slist_copy (app->priv->terminals);
|
||||
|
|
|
@ -1,369 +0,0 @@
|
|||
/*
|
||||
* mooapp/moopython.c
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <Python.h>
|
||||
#ifdef MOO_USE_PYGTK
|
||||
#define NO_IMPORT_PYGOBJECT
|
||||
#include <pygobject.h>
|
||||
#endif /* MOO_USE_PYGTK */
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include "mooapp/moopython.h"
|
||||
#include "mooapp/moopythonconsole.h"
|
||||
#include "mooutils/moocompat.h"
|
||||
|
||||
|
||||
static MooPython *instance = NULL;
|
||||
|
||||
MooPython *moo_python_get_instance (void)
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
static void moo_python_class_init (MooPythonClass *klass);
|
||||
static void moo_python_init (MooPython *python);
|
||||
static GObject *moo_python_constructor (GType type,
|
||||
guint n_construct_properties,
|
||||
GObjectConstructParam *construct_properties);
|
||||
static void moo_python_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec);
|
||||
static void moo_python_finalize (GObject *object);
|
||||
|
||||
static void init_logger (MooPython *python);
|
||||
|
||||
|
||||
/* MOO_TYPE_PYTHON */
|
||||
G_DEFINE_TYPE (MooPython, moo_python, G_TYPE_OBJECT);
|
||||
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_USE_CONSOLE
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
moo_python_class_init (MooPythonClass *klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
gobject_class->constructor = moo_python_constructor;
|
||||
gobject_class->finalize = moo_python_finalize;
|
||||
gobject_class->set_property = moo_python_set_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_USE_CONSOLE,
|
||||
g_param_spec_boolean ("use-console",
|
||||
"use-console",
|
||||
"use-console",
|
||||
TRUE,
|
||||
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_init (MooPython *python)
|
||||
{
|
||||
g_assert (instance == NULL);
|
||||
instance = python;
|
||||
|
||||
python->running = FALSE;
|
||||
python->log_in_func = NULL;
|
||||
python->log_out_func = NULL;
|
||||
python->log_err_func = NULL;
|
||||
}
|
||||
|
||||
|
||||
static GObject*
|
||||
moo_python_constructor (GType type,
|
||||
guint n_props,
|
||||
GObjectConstructParam *props)
|
||||
{
|
||||
GObject *object;
|
||||
MooPython *python;
|
||||
object = G_OBJECT_CLASS (moo_python_parent_class)->constructor (type, n_props, props);
|
||||
python = MOO_PYTHON (object);
|
||||
python->_console = moo_python_console_new (python, python->use_console);
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_finalize (GObject *object)
|
||||
{
|
||||
instance = NULL;
|
||||
G_OBJECT_CLASS (moo_python_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
MooPython *python = MOO_PYTHON (object);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_USE_CONSOLE:
|
||||
python->use_console = g_value_get_boolean (value);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_python_start (MooPython *python,
|
||||
int argc,
|
||||
char **argv)
|
||||
{
|
||||
g_return_if_fail (MOO_IS_PYTHON (python));
|
||||
g_return_if_fail (!python->running);
|
||||
|
||||
if (argc)
|
||||
Py_SetProgramName (argv[0]);
|
||||
|
||||
Py_Initialize ();
|
||||
|
||||
if (argc)
|
||||
PySys_SetArgv (argc, argv);
|
||||
|
||||
python->main_mod = PyImport_AddModule ((char*)"__main__");
|
||||
Py_XINCREF ((PyObject*) python->main_mod);
|
||||
|
||||
init_logger (python);
|
||||
|
||||
python->running = TRUE;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_python_shutdown (MooPython *python)
|
||||
{
|
||||
g_return_if_fail (MOO_IS_PYTHON (python));
|
||||
g_return_if_fail (python->running);
|
||||
|
||||
Py_XDECREF ((PyObject*) python->main_mod);
|
||||
/* TODO: Py_Finalize is crash if shutdown() was initiated from python,
|
||||
like with app.quit()
|
||||
It also aborts nobody knows why
|
||||
*/
|
||||
#if 0
|
||||
Py_Finalize ();
|
||||
#endif
|
||||
|
||||
moo_python_set_log_func (python, NULL, NULL, NULL, NULL);
|
||||
|
||||
python->running = FALSE;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
moo_python_run_simple_string (MooPython *python,
|
||||
const char *cmd)
|
||||
{
|
||||
g_return_val_if_fail (MOO_IS_PYTHON (python), -1);
|
||||
g_return_val_if_fail (cmd != NULL, -1);
|
||||
return PyRun_SimpleString (cmd);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
moo_python_run_simple_file (MooPython *python,
|
||||
gpointer fp,
|
||||
const char *filename)
|
||||
{
|
||||
g_return_val_if_fail (MOO_IS_PYTHON (python), -1);
|
||||
g_return_val_if_fail (fp != NULL && filename != NULL, -1);
|
||||
return PyRun_SimpleFile ((FILE*) fp, filename);
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
moo_python_run_string (MooPython *python,
|
||||
const char *str,
|
||||
gboolean silent)
|
||||
{
|
||||
PyObject *dict;
|
||||
g_return_val_if_fail (MOO_IS_PYTHON (python), NULL);
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
if (!silent && python->log_in_func)
|
||||
python->log_in_func (str, -1, python->log_data);
|
||||
dict = PyModule_GetDict ((PyObject*) python->main_mod);
|
||||
return PyRun_String (str, Py_file_input, dict, dict);
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
moo_python_run_file (MooPython *python,
|
||||
gpointer fp,
|
||||
const char *filename)
|
||||
{
|
||||
PyObject *dict;
|
||||
g_return_val_if_fail (MOO_IS_PYTHON (python), NULL);
|
||||
g_return_val_if_fail (fp != NULL && filename != NULL, NULL);
|
||||
dict = PyModule_GetDict ((PyObject*) python->main_mod);
|
||||
return PyRun_File ((FILE*) fp, filename, Py_file_input, dict, dict);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_python_set_log_func (MooPython *python,
|
||||
MooPythonLogFunc in,
|
||||
MooPythonLogFunc out,
|
||||
MooPythonLogFunc err,
|
||||
gpointer data)
|
||||
{
|
||||
g_return_if_fail (MOO_IS_PYTHON (python));
|
||||
python->log_in_func = in;
|
||||
python->log_out_func = out;
|
||||
python->log_err_func = err;
|
||||
python->log_data = data;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_python_write_log (MooPython *python,
|
||||
int kind,
|
||||
const char *text,
|
||||
int len)
|
||||
{
|
||||
char *freeme = NULL;
|
||||
const char *write_text;
|
||||
|
||||
g_return_if_fail (MOO_IS_PYTHON (python));
|
||||
g_return_if_fail (kind == 2 || kind == 3);
|
||||
g_return_if_fail (text != NULL);
|
||||
|
||||
if (!len)
|
||||
return;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
freeme = g_strndup (text, len);
|
||||
write_text = freeme;
|
||||
}
|
||||
else
|
||||
{
|
||||
write_text = text;
|
||||
}
|
||||
|
||||
if (kind == 2)
|
||||
{
|
||||
if (python->log_out_func)
|
||||
python->log_out_func (write_text, -1, python->log_data);
|
||||
else
|
||||
fprintf (stdout, "%s", write_text);
|
||||
}
|
||||
|
||||
if (kind == 3)
|
||||
{
|
||||
if (python->log_err_func)
|
||||
python->log_err_func (write_text, -1, python->log_data);
|
||||
else
|
||||
fprintf (stderr, "%s", write_text);
|
||||
}
|
||||
|
||||
g_free (freeme);
|
||||
}
|
||||
|
||||
|
||||
MooPython *
|
||||
moo_python_new (gboolean use_console)
|
||||
{
|
||||
return g_object_new (MOO_TYPE_PYTHON, "use-console", use_console, NULL);
|
||||
}
|
||||
|
||||
|
||||
#define WRITE_LOG "_write_log"
|
||||
|
||||
static PyObject*
|
||||
write_log (G_GNUC_UNUSED PyObject *self,
|
||||
PyObject* args)
|
||||
{
|
||||
int kind;
|
||||
const char *string;
|
||||
|
||||
if (PyArg_ParseTuple(args, (char*)"is:" WRITE_LOG, &kind, &string))
|
||||
{
|
||||
moo_python_write_log (instance, kind, string, -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_critical (WRITE_LOG ": incorrect parameters passed");
|
||||
PyErr_Clear ();
|
||||
}
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
init_logger (MooPython *python)
|
||||
{
|
||||
PyObject *fun, *res;
|
||||
const char *script;
|
||||
static PyMethodDef meth =
|
||||
{(char*)WRITE_LOG, write_log, METH_VARARGS, (char*)WRITE_LOG};
|
||||
|
||||
fun = PyCFunction_New (&meth, python->main_mod);
|
||||
g_return_if_fail (fun != NULL);
|
||||
|
||||
if (PyModule_AddObject (python->main_mod, (char*)WRITE_LOG, fun))
|
||||
{
|
||||
Py_DECREF (fun);
|
||||
return;
|
||||
}
|
||||
|
||||
script =
|
||||
"import sys\n"
|
||||
"class __Logger:\n"
|
||||
" def __init__ (self, t):\n"
|
||||
" self.kind = t\n"
|
||||
" def write (self, data):\n"
|
||||
" " WRITE_LOG " (self.kind, data)\n"
|
||||
"sys.stdout = __Logger (2)\n"
|
||||
"sys.stderr = __Logger (3)\n";
|
||||
|
||||
res = (PyObject*) moo_python_run_string (python, script, TRUE);
|
||||
|
||||
if (res)
|
||||
{
|
||||
Py_DECREF (res);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_critical ("%s: error in Logger initialization", G_STRLOC);
|
||||
g_critical ("%s", script);
|
||||
PyErr_Print ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gpointer
|
||||
moo_python_get_console (MooPython *python)
|
||||
{
|
||||
g_return_val_if_fail (MOO_IS_PYTHON (python), NULL);
|
||||
return python->_console;
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
/*
|
||||
* mooapp/moopython.h
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifndef MOOPYTHON_H
|
||||
#define MOOPYTHON_H
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
#define MOO_TYPE_PYTHON (moo_python_get_type ())
|
||||
#define MOO_PYTHON(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_PYTHON, MooPython))
|
||||
#define MOO_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_PYTHON, MooPythonClass))
|
||||
#define MOO_IS_PYTHON(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MOO_TYPE_PYTHON))
|
||||
#define MOO_IS_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_PYTHON))
|
||||
#define MOO_PYTHON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_PYTHON, MooPythonClass))
|
||||
|
||||
|
||||
typedef struct _MooPython MooPython;
|
||||
typedef struct _MooPythonClass MooPythonClass;
|
||||
|
||||
typedef void (*MooPythonLogFunc) (const char *text,
|
||||
int len,
|
||||
gpointer data);
|
||||
|
||||
struct _MooPython
|
||||
{
|
||||
GObject parent;
|
||||
|
||||
gboolean running;
|
||||
void *main_mod;
|
||||
MooPythonLogFunc log_in_func;
|
||||
MooPythonLogFunc log_out_func;
|
||||
MooPythonLogFunc log_err_func;
|
||||
void *log_data;
|
||||
|
||||
gboolean use_console;
|
||||
struct _MooPythonConsole *_console;
|
||||
};
|
||||
|
||||
struct _MooPythonClass
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
GType moo_python_get_type (void) G_GNUC_CONST;
|
||||
|
||||
MooPython *moo_python_get_instance (void);
|
||||
MooPython *moo_python_new (gboolean use_python_console);
|
||||
|
||||
void moo_python_start (MooPython *python,
|
||||
int argc,
|
||||
char **argv);
|
||||
void moo_python_shutdown (MooPython *python);
|
||||
gboolean moo_python_running (MooPython *python);
|
||||
|
||||
void moo_python_set_log_func (MooPython *python,
|
||||
MooPythonLogFunc in,
|
||||
MooPythonLogFunc out,
|
||||
MooPythonLogFunc err,
|
||||
gpointer data);
|
||||
void moo_python_write_log (MooPython *python,
|
||||
int kind,
|
||||
const char *text,
|
||||
int len);
|
||||
|
||||
int moo_python_run_simple_string(MooPython *python,
|
||||
const char *cmd);
|
||||
int moo_python_run_simple_file (MooPython *python,
|
||||
gpointer fp,
|
||||
const char *filename);
|
||||
|
||||
gpointer moo_python_run_string (MooPython *python,
|
||||
const char *str,
|
||||
gboolean silent);
|
||||
gpointer moo_python_run_file (MooPython *python,
|
||||
gpointer fp,
|
||||
const char *filename);
|
||||
|
||||
gpointer moo_python_get_console (MooPython *python);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MOOPYTHON_H */
|
|
@ -1,465 +0,0 @@
|
|||
/*
|
||||
* mooapp/moopythonconsole.c
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#include <Python.h>
|
||||
#include <gdk/gdkkeysyms.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include "mooapp/moopython.h"
|
||||
#include "mooapp/moopythonconsole.h"
|
||||
#include "mooutils/moostock.h"
|
||||
#include "mooutils/moocompat.h"
|
||||
#include "mooutils/mooutils-misc.h"
|
||||
#include "mooutils/mooentry.h"
|
||||
|
||||
|
||||
#define MAX_HISTORY 500
|
||||
|
||||
static void moo_python_console_class_init (MooPythonConsoleClass *klass);
|
||||
static GObject *moo_python_console_constructor (GType type,
|
||||
guint n_construct_properties,
|
||||
GObjectConstructParam *construct_properties);
|
||||
static void moo_python_console_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec);
|
||||
static void moo_python_console_init (MooPythonConsole *console);
|
||||
static void moo_python_console_finalize (GObject *object);
|
||||
|
||||
static void create_gui (MooPythonConsole *self);
|
||||
static void entry_activate (MooPythonConsole *self);
|
||||
static gboolean key_press_event (GtkEntry *entry,
|
||||
GdkEventKey *event,
|
||||
MooPythonConsole *self);
|
||||
|
||||
static void entry_commit (MooPythonConsole *self);
|
||||
static void history_next (MooPythonConsole *self);
|
||||
static void history_prev (MooPythonConsole *self);
|
||||
|
||||
static void write_in (const char *text,
|
||||
int len,
|
||||
MooPythonConsole *self);
|
||||
static void write_out (const char *text,
|
||||
int len,
|
||||
MooPythonConsole *self);
|
||||
static void write_err (const char *text,
|
||||
int len,
|
||||
MooPythonConsole *self);
|
||||
|
||||
static void queue_free (GQueue *que);
|
||||
static guint queue_len (GQueue *que);
|
||||
static gpointer queue_nth (GQueue *que,
|
||||
guint n);
|
||||
|
||||
|
||||
/* MOO_TYPE_PYTHON_CONSOLE */
|
||||
G_DEFINE_TYPE (MooPythonConsole, moo_python_console, GTK_TYPE_WINDOW);
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_PYTHON,
|
||||
PROP_REDIRECT_OUTPUT
|
||||
};
|
||||
|
||||
static void
|
||||
moo_python_console_class_init (MooPythonConsoleClass *klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->constructor = moo_python_console_constructor;
|
||||
gobject_class->finalize = moo_python_console_finalize;
|
||||
gobject_class->set_property = moo_python_console_set_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PYTHON,
|
||||
g_param_spec_object ("python",
|
||||
"python",
|
||||
"python",
|
||||
MOO_TYPE_PYTHON,
|
||||
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_REDIRECT_OUTPUT,
|
||||
g_param_spec_boolean ("redirect-output",
|
||||
"redirect-output",
|
||||
"redirect-output",
|
||||
TRUE,
|
||||
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_console_init (MooPythonConsole *console)
|
||||
{
|
||||
console->history = g_queue_new ();
|
||||
console->current = 0;
|
||||
}
|
||||
|
||||
|
||||
static GObject*
|
||||
moo_python_console_constructor (GType type,
|
||||
guint n_props,
|
||||
GObjectConstructParam *props)
|
||||
{
|
||||
MooPythonConsole *console;
|
||||
PangoFontDescription *font;
|
||||
GtkTextIter iter;
|
||||
|
||||
GObject *obj =
|
||||
G_OBJECT_CLASS (moo_python_console_parent_class)->constructor (type, n_props, props);
|
||||
|
||||
console = MOO_PYTHON_CONSOLE (obj);
|
||||
|
||||
create_gui (console);
|
||||
moo_window_set_icon_from_stock (GTK_WINDOW (console), MOO_STOCK_APP);
|
||||
|
||||
g_signal_connect (console, "delete-event",
|
||||
G_CALLBACK (gtk_widget_hide_on_delete),
|
||||
NULL);
|
||||
|
||||
if (console->redirect_output)
|
||||
moo_python_set_log_func (console->python,
|
||||
(MooPythonLogFunc) write_in,
|
||||
(MooPythonLogFunc) write_out,
|
||||
(MooPythonLogFunc) write_err,
|
||||
console);
|
||||
|
||||
g_signal_connect_swapped (console->entry, "activate",
|
||||
G_CALLBACK (entry_activate),
|
||||
console);
|
||||
g_signal_connect (console->entry, "key-press-event",
|
||||
G_CALLBACK (key_press_event),
|
||||
console);
|
||||
|
||||
console->buf = gtk_text_view_get_buffer (console->textview);
|
||||
gtk_text_buffer_get_end_iter (console->buf, &iter);
|
||||
console->end = gtk_text_buffer_create_mark (console->buf, NULL, &iter, FALSE);
|
||||
console->in_tag = gtk_text_buffer_create_tag (console->buf, "input", "foreground", "blue", NULL);
|
||||
console->out_tag = gtk_text_buffer_create_tag (console->buf, "output", "foreground", "black", NULL);
|
||||
console->err_tag = gtk_text_buffer_create_tag (console->buf, "error", "foreground", "red", NULL);
|
||||
|
||||
font = pango_font_description_from_string ("Courier New 10");
|
||||
if (font) {
|
||||
gtk_widget_modify_font (GTK_WIDGET (console->textview), font);
|
||||
gtk_widget_modify_font (console->entry, font);
|
||||
pango_font_description_free (font);
|
||||
}
|
||||
|
||||
console->current = 0;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_console_finalize (GObject *object)
|
||||
{
|
||||
MooPythonConsole *console = MOO_PYTHON_CONSOLE (object);
|
||||
queue_free (console->history);
|
||||
G_OBJECT_CLASS (moo_python_console_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
moo_python_console_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
MooPythonConsole *console = MOO_PYTHON_CONSOLE (object);
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_PYTHON:
|
||||
console->python = g_value_get_object (value);
|
||||
break;
|
||||
|
||||
case PROP_REDIRECT_OUTPUT:
|
||||
console->redirect_output = g_value_get_boolean (value);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
entry_activate (MooPythonConsole *self)
|
||||
{
|
||||
return entry_commit (self);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
entry_commit (MooPythonConsole *self)
|
||||
{
|
||||
char *s;
|
||||
PyObject *res;
|
||||
|
||||
s = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry)));
|
||||
gtk_entry_set_text (GTK_ENTRY (self->entry), "");
|
||||
|
||||
if (!s || !s[0])
|
||||
{
|
||||
g_free (s);
|
||||
return;
|
||||
}
|
||||
|
||||
res = (PyObject*) moo_python_run_string (self->python, s, FALSE);
|
||||
|
||||
if (res && res != Py_None)
|
||||
{
|
||||
PyObject* str = PyObject_Str (res);
|
||||
|
||||
if (str)
|
||||
{
|
||||
write_out (PyString_AsString(str), -1, self);
|
||||
Py_DECREF (str);
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_Print ();
|
||||
}
|
||||
}
|
||||
else if (!res)
|
||||
{
|
||||
PyErr_Print ();
|
||||
}
|
||||
|
||||
Py_XDECREF (res);
|
||||
|
||||
if (g_queue_is_empty (self->history) || strcmp (s, g_queue_peek_head (self->history)))
|
||||
g_queue_push_tail (self->history, s);
|
||||
else
|
||||
g_free (s);
|
||||
|
||||
if (queue_len (self->history) >= MAX_HISTORY)
|
||||
g_free (g_queue_pop_head (self->history));
|
||||
|
||||
self->current = queue_len (self->history);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
write_in (const char *text_to_write,
|
||||
int len,
|
||||
MooPythonConsole *self)
|
||||
{
|
||||
char *text;
|
||||
char **lines, **l;
|
||||
GString *s;
|
||||
GtkTextIter end;
|
||||
|
||||
if (!text_to_write || !text_to_write[0] || len == 0)
|
||||
return;
|
||||
|
||||
if (len > 0)
|
||||
text = g_strndup (text_to_write, len);
|
||||
else
|
||||
text = (char*)text_to_write;
|
||||
|
||||
s = g_string_new ("");
|
||||
lines = g_strsplit (text, "\n", 0);
|
||||
for (l = lines; *l != NULL; ++l)
|
||||
if (**l)
|
||||
g_string_append_printf (s, ">>> %s\n", *l);
|
||||
|
||||
gtk_text_buffer_get_end_iter (self->buf, &end);
|
||||
gtk_text_buffer_insert_with_tags (self->buf, &end,
|
||||
s->str, s->len,
|
||||
self->in_tag, NULL);
|
||||
gtk_text_buffer_get_end_iter (self->buf, &end);
|
||||
gtk_text_buffer_place_cursor (self->buf, &end);
|
||||
gtk_text_view_scroll_mark_onscreen (self->textview, self->end);
|
||||
|
||||
g_strfreev (lines);
|
||||
g_string_free (s, TRUE);
|
||||
if (text != text_to_write)
|
||||
g_free (text);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
write_out (const char *text,
|
||||
int len,
|
||||
MooPythonConsole *self)
|
||||
{
|
||||
GtkTextIter end;
|
||||
gtk_text_buffer_get_end_iter (self->buf, &end);
|
||||
gtk_text_buffer_insert_with_tags (self->buf, &end, text, len, self->out_tag, NULL);
|
||||
gtk_text_view_scroll_mark_onscreen (self->textview, self->end);
|
||||
}
|
||||
|
||||
static void
|
||||
write_err (const char *text,
|
||||
int len,
|
||||
MooPythonConsole *self)
|
||||
{
|
||||
GtkTextIter end;
|
||||
gtk_text_buffer_get_end_iter (self->buf, &end);
|
||||
gtk_text_buffer_insert_with_tags (self->buf, &end, text, len, self->err_tag, NULL);
|
||||
gtk_text_view_scroll_mark_onscreen (self->textview, self->end);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
key_press_event (G_GNUC_UNUSED GtkEntry *entry,
|
||||
GdkEventKey *event,
|
||||
MooPythonConsole *self)
|
||||
{
|
||||
if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK))
|
||||
return FALSE;
|
||||
|
||||
switch (event->keyval) {
|
||||
case GDK_Tab:
|
||||
return TRUE;
|
||||
|
||||
case GDK_Down:
|
||||
history_next (self);
|
||||
return TRUE;
|
||||
|
||||
case GDK_Up:
|
||||
history_prev (self);
|
||||
return TRUE;
|
||||
|
||||
case GDK_Return:
|
||||
entry_commit (self);
|
||||
return TRUE;
|
||||
|
||||
default:
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
history_prev (MooPythonConsole *self)
|
||||
{
|
||||
const char *s = "";
|
||||
guint hist_size = queue_len (self->history);
|
||||
|
||||
self->current -= 1;
|
||||
if (self->current == (guint)-1)
|
||||
self->current = hist_size;
|
||||
if (self->current != hist_size)
|
||||
s = queue_nth (self->history, self->current);
|
||||
|
||||
gtk_entry_set_text (GTK_ENTRY (self->entry), s);
|
||||
}
|
||||
|
||||
static void
|
||||
history_next (MooPythonConsole *self)
|
||||
{
|
||||
const char *s = "";
|
||||
guint hist_size = queue_len (self->history);
|
||||
|
||||
self->current += 1;
|
||||
if (self->current == hist_size + 1)
|
||||
self->current = 0;
|
||||
if (self->current != hist_size)
|
||||
s = queue_nth (self->history, self->current);
|
||||
|
||||
gtk_entry_set_text (GTK_ENTRY (self->entry), s);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
create_gui (MooPythonConsole *self)
|
||||
{
|
||||
GtkWidget *vbox1;
|
||||
GtkWidget *scrolledwindow1;
|
||||
GtkWidget *alignment1;
|
||||
GtkWidget *hseparator1;
|
||||
|
||||
gtk_window_set_title (GTK_WINDOW (self), "Python");
|
||||
gtk_window_set_default_size (GTK_WINDOW (self), 400, 300);
|
||||
|
||||
vbox1 = gtk_vbox_new (FALSE, 0);
|
||||
gtk_widget_show (vbox1);
|
||||
gtk_container_add (GTK_CONTAINER (self), vbox1);
|
||||
|
||||
scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
|
||||
gtk_widget_show (scrolledwindow1);
|
||||
gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
|
||||
GTK_WIDGET_UNSET_FLAGS (scrolledwindow1, GTK_CAN_FOCUS);
|
||||
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_ETCHED_IN);
|
||||
|
||||
self->textview = GTK_TEXT_VIEW (gtk_text_view_new ());
|
||||
gtk_widget_show (GTK_WIDGET (self->textview));
|
||||
gtk_container_add (GTK_CONTAINER (scrolledwindow1), GTK_WIDGET (self->textview));
|
||||
GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (self->textview), GTK_CAN_FOCUS);
|
||||
gtk_text_view_set_editable (self->textview, FALSE);
|
||||
gtk_text_view_set_wrap_mode (self->textview, GTK_WRAP_WORD);
|
||||
|
||||
alignment1 = gtk_alignment_new (0.5, 0.5, 1, 1);
|
||||
gtk_widget_show (alignment1);
|
||||
gtk_box_pack_start (GTK_BOX (vbox1), alignment1, FALSE, FALSE, 0);
|
||||
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment1), 3, 3, 0, 0);
|
||||
|
||||
hseparator1 = gtk_hseparator_new ();
|
||||
gtk_widget_show (hseparator1);
|
||||
gtk_container_add (GTK_CONTAINER (alignment1), hseparator1);
|
||||
|
||||
self->entry = moo_entry_new ();
|
||||
gtk_widget_show (self->entry);
|
||||
gtk_box_pack_start (GTK_BOX (vbox1), self->entry, FALSE, FALSE, 0);
|
||||
GTK_WIDGET_SET_FLAGS (self->entry, GTK_CAN_DEFAULT);
|
||||
|
||||
gtk_widget_grab_focus (self->entry);
|
||||
gtk_widget_grab_default (self->entry);
|
||||
}
|
||||
|
||||
|
||||
MooPythonConsole*
|
||||
moo_python_console_new (struct _MooPython *python,
|
||||
gboolean redirect_output)
|
||||
{
|
||||
return g_object_new (MOO_TYPE_PYTHON_CONSOLE,
|
||||
"python", python,
|
||||
"redirect-output", redirect_output,
|
||||
NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
queue_free (GQueue *que)
|
||||
{
|
||||
GList *l;
|
||||
if (!que) return;
|
||||
for (l = que->head; l != NULL; l = l->next)
|
||||
g_free (l->data);
|
||||
g_queue_free (que);
|
||||
}
|
||||
|
||||
|
||||
static guint
|
||||
queue_len (GQueue *que)
|
||||
{
|
||||
GList *l;
|
||||
guint len = 0;
|
||||
g_return_val_if_fail (que != NULL, 0);
|
||||
for (l = que->head; l != NULL; l = l->next)
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
static gpointer
|
||||
queue_nth (GQueue *que,
|
||||
guint n)
|
||||
{
|
||||
GList *l;
|
||||
guint i;
|
||||
for (i = 0, l = que->head; i < n; ++i, l = l->next) ;
|
||||
return l->data;
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* mooapp/moopythonconsole.h
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifndef GGAPPYCONSOLE_H
|
||||
#define GGAPPYCONSOLE_H
|
||||
|
||||
#include <gtk/gtktextview.h>
|
||||
#include <gtk/gtkentry.h>
|
||||
#include <gtk/gtkwindow.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
#define MOO_TYPE_PYTHON_CONSOLE (moo_python_console_get_type ())
|
||||
#define MOO_PYTHON_CONSOLE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_PYTHON_CONSOLE, MooPythonConsole))
|
||||
#define MOO_PYTHON_CONSOLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_PYTHON_CONSOLE, MooPythonConsoleClass))
|
||||
#define MOO_IS_PYTHON_CONSOLE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MOO_TYPE_PYTHON_CONSOLE))
|
||||
#define MOO_IS_PYTHON_CONSOLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_PYTHON_CONSOLE))
|
||||
#define MOO_PYTHON_CONSOLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_PYTHON_CONSOLE, MooPythonConsoleClass))
|
||||
|
||||
|
||||
typedef struct _MooPythonConsole MooPythonConsole;
|
||||
typedef struct _MooPythonConsoleClass MooPythonConsoleClass;
|
||||
|
||||
struct _MooPythonConsole
|
||||
{
|
||||
GtkWindow parent;
|
||||
|
||||
gboolean redirect_output;
|
||||
GtkWidget *entry;
|
||||
GtkTextTag *in_tag;
|
||||
GtkTextTag *out_tag;
|
||||
GtkTextTag *err_tag;
|
||||
GtkTextBuffer *buf;
|
||||
GtkTextView *textview;
|
||||
GtkTextMark *end;
|
||||
GQueue *history;
|
||||
guint current;
|
||||
struct _MooPython *python;
|
||||
};
|
||||
|
||||
struct _MooPythonConsoleClass
|
||||
{
|
||||
GtkWindowClass parent_class;
|
||||
};
|
||||
|
||||
GType moo_python_console_get_type (void) G_GNUC_CONST;
|
||||
MooPythonConsole *moo_python_console_new (struct _MooPython *python,
|
||||
gboolean redirect_output);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* GGAPPYCONSOLE_H */
|
|
@ -5,12 +5,14 @@
|
|||
moopython = $(moo_prefix)/moopython
|
||||
moopython_srcdir = $(srcdir)/$(moopython)
|
||||
|
||||
mooplugin_python_sources = \
|
||||
moopython_sources = \
|
||||
$(moopython)/moopython.c \
|
||||
$(moopython)/moopython.h \
|
||||
$(moopython)/mooplugin-python.c \
|
||||
$(moopython)/mooplugin-python.h
|
||||
|
||||
if MOO_USE_PYGTK
|
||||
moo_sources += $(mooplugin_python_sources)
|
||||
moo_sources += $(moopython_sources)
|
||||
endif
|
||||
|
||||
moo_extra_dist += $(mooplugin_python_sources)
|
||||
moo_extra_dist += $(moopython_sources)
|
||||
|
|
|
@ -0,0 +1,265 @@
|
|||
/*
|
||||
* mooapp/moopython.c
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <Python.h>
|
||||
#ifdef MOO_USE_PYGTK
|
||||
#define NO_IMPORT_PYGOBJECT
|
||||
#include <pygobject.h>
|
||||
#endif
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include "moopython/moopython.h"
|
||||
#include "mooutils/moocompat.h"
|
||||
|
||||
|
||||
static gboolean running;
|
||||
MooPyAPI *_moo_py_api = NULL;
|
||||
static MooPyAPI moo_py_api;
|
||||
static PyObject *main_mod;
|
||||
static MooPythonLogFunc log_in_func;
|
||||
static MooPythonLogFunc log_out_func;
|
||||
static MooPythonLogFunc log_err_func;
|
||||
static gpointer log_func_data;
|
||||
|
||||
|
||||
gboolean
|
||||
moo_python_running (void)
|
||||
{
|
||||
return running;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
moo_python_shutdown (void)
|
||||
{
|
||||
g_return_if_fail (running);
|
||||
|
||||
Py_XDECREF (main_mod);
|
||||
_moo_py_api = NULL;
|
||||
running = FALSE;
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject*
|
||||
run_string (const char *str,
|
||||
gboolean silent)
|
||||
{
|
||||
PyObject *dict;
|
||||
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
|
||||
if (!silent && log_in_func)
|
||||
log_in_func (str, -1, log_func_data);
|
||||
|
||||
dict = PyModule_GetDict (main_mod);
|
||||
return (MooPyObject*) PyRun_String (str, Py_file_input, dict, dict);
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject*
|
||||
run_file (gpointer fp,
|
||||
const char *filename)
|
||||
{
|
||||
PyObject *dict;
|
||||
|
||||
g_return_val_if_fail (fp != NULL && filename != NULL, NULL);
|
||||
|
||||
dict = PyModule_GetDict (main_mod);
|
||||
return (MooPyObject*) PyRun_File (fp, filename, Py_file_input, dict, dict);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
set_log_func (MooPythonLogFunc in,
|
||||
MooPythonLogFunc out,
|
||||
MooPythonLogFunc err,
|
||||
gpointer data)
|
||||
{
|
||||
log_in_func = in;
|
||||
log_out_func = out;
|
||||
log_err_func = err;
|
||||
log_func_data = data;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
write_log (int kind,
|
||||
const char *text,
|
||||
int len)
|
||||
{
|
||||
char *freeme = NULL;
|
||||
const char *write_text;
|
||||
|
||||
g_return_if_fail (kind == 2 || kind == 3);
|
||||
g_return_if_fail (text != NULL);
|
||||
|
||||
if (!len)
|
||||
return;
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
freeme = g_strndup (text, len);
|
||||
write_text = freeme;
|
||||
}
|
||||
else
|
||||
{
|
||||
write_text = text;
|
||||
}
|
||||
|
||||
if (kind == 2)
|
||||
{
|
||||
if (log_out_func)
|
||||
log_out_func (write_text, -1, log_func_data);
|
||||
else
|
||||
fprintf (stdout, "%s", write_text);
|
||||
}
|
||||
|
||||
if (kind == 3)
|
||||
{
|
||||
if (log_err_func)
|
||||
log_err_func (write_text, -1, log_func_data);
|
||||
else
|
||||
fprintf (stderr, "%s", write_text);
|
||||
}
|
||||
|
||||
g_free (freeme);
|
||||
}
|
||||
|
||||
|
||||
#define WRITE_LOG "_write_log"
|
||||
|
||||
static PyObject*
|
||||
write_log_meth (G_GNUC_UNUSED PyObject *self,
|
||||
PyObject* args)
|
||||
{
|
||||
int kind;
|
||||
const char *string;
|
||||
|
||||
if (PyArg_ParseTuple(args, (char*)"is:" WRITE_LOG, &kind, &string))
|
||||
{
|
||||
write_log (kind, string, -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_critical (WRITE_LOG ": incorrect parameters passed");
|
||||
PyErr_Clear ();
|
||||
}
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
init_logger (void)
|
||||
{
|
||||
PyObject *fun, *res;
|
||||
const char *script;
|
||||
static PyMethodDef meth = {(char*) WRITE_LOG, write_log_meth, METH_VARARGS, (char*) WRITE_LOG};
|
||||
|
||||
fun = PyCFunction_New (&meth, main_mod);
|
||||
g_return_if_fail (fun != NULL);
|
||||
|
||||
if (PyModule_AddObject (main_mod, (char*) WRITE_LOG, fun))
|
||||
{
|
||||
Py_DECREF (fun);
|
||||
return;
|
||||
}
|
||||
|
||||
script =
|
||||
"import sys\n"
|
||||
"class __Logger:\n"
|
||||
" def __init__ (self, t):\n"
|
||||
" self.kind = t\n"
|
||||
" def write (self, data):\n"
|
||||
" " WRITE_LOG " (self.kind, data)\n"
|
||||
"sys.stdout = __Logger (2)\n"
|
||||
"sys.stderr = __Logger (3)\n";
|
||||
|
||||
res = (PyObject*) run_string (script, TRUE);
|
||||
|
||||
if (res)
|
||||
{
|
||||
Py_DECREF (res);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_critical ("%s: error in Logger initialization", G_STRLOC);
|
||||
g_critical ("%s", script);
|
||||
PyErr_Print ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static MooPyObject*
|
||||
incref (MooPyObject *obj)
|
||||
{
|
||||
if (obj)
|
||||
{
|
||||
Py_INCREF ((PyObject*) obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
static void
|
||||
decref (MooPyObject *obj)
|
||||
{
|
||||
if (obj)
|
||||
{
|
||||
Py_DECREF ((PyObject*) obj);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
err_print (void)
|
||||
{
|
||||
PyErr_Print ();
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
moo_python_start (int argc,
|
||||
char **argv)
|
||||
{
|
||||
g_return_val_if_fail (!running, FALSE);
|
||||
|
||||
if (argc)
|
||||
Py_SetProgramName (argv[0]);
|
||||
|
||||
Py_Initialize ();
|
||||
|
||||
if (argc)
|
||||
PySys_SetArgv (argc, argv);
|
||||
|
||||
main_mod = PyImport_AddModule ((char*)"__main__");
|
||||
Py_XINCREF ((PyObject*) main_mod);
|
||||
|
||||
_moo_py_api = &moo_py_api;
|
||||
|
||||
moo_py_api.incref = incref;
|
||||
moo_py_api.decref = decref;
|
||||
moo_py_api.err_print = err_print;
|
||||
moo_py_api.set_log_func = set_log_func;
|
||||
moo_py_api.run_string = run_string;
|
||||
moo_py_api.run_file = run_file;
|
||||
|
||||
init_logger ();
|
||||
|
||||
running = TRUE;
|
||||
return TRUE;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* moopython/moopython.h
|
||||
*
|
||||
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* See COPYING file that comes with this distribution.
|
||||
*/
|
||||
|
||||
#ifndef __MOO_PYTHON_H__
|
||||
#define __MOO_PYTHON_H__
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
typedef struct _MooPyAPI MooPyAPI;
|
||||
typedef struct _MooPyObject MooPyObject;
|
||||
|
||||
typedef void (*MooPythonLogFunc) (const char *text,
|
||||
int len,
|
||||
gpointer data);
|
||||
|
||||
struct _MooPyAPI {
|
||||
MooPyObject* (*incref) (MooPyObject *obj);
|
||||
void (*decref) (MooPyObject *obj);
|
||||
void (*err_print) (void);
|
||||
|
||||
void (*set_log_func)(MooPythonLogFunc in,
|
||||
MooPythonLogFunc out,
|
||||
MooPythonLogFunc err,
|
||||
gpointer data);
|
||||
MooPyObject* (*run_string) (const char *str,
|
||||
gboolean silent);
|
||||
MooPyObject* (*run_file) (gpointer fp,
|
||||
const char *filename);
|
||||
};
|
||||
|
||||
|
||||
extern MooPyAPI *_moo_py_api;
|
||||
|
||||
|
||||
gboolean moo_python_start (int argc,
|
||||
char **argv);
|
||||
void moo_python_shutdown (void);
|
||||
gboolean moo_python_running (void);
|
||||
|
||||
void _moo_python_plugin_init (char **dirs);
|
||||
void _moo_python_plugin_deinit (void);
|
||||
void _moo_python_plugin_reload (void);
|
||||
|
||||
|
||||
#define moo_Py_INCREF _moo_py_api->incref
|
||||
#define moo_Py_DECREF _moo_py_api->decref
|
||||
#define moo_PyErr_Print _moo_py_api->err_print
|
||||
|
||||
#define moo_python_set_log_func _moo_py_api->set_log_func
|
||||
#define moo_python_run_string _moo_py_api->run_string
|
||||
#define moo_python_run_file _moo_py_api->run_file
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __MOO_PYTHON_H__ */
|
|
@ -8,20 +8,6 @@
|
|||
(gtype-id "MOO_TYPE_APP")
|
||||
)
|
||||
|
||||
;; (define-object Python
|
||||
;; (in-module "Moo")
|
||||
;; (parent "GObject")
|
||||
;; (c-name "MooPython")
|
||||
;; (gtype-id "MOO_TYPE_PYTHON")
|
||||
;; )
|
||||
;;
|
||||
;; (define-object PythonConsole
|
||||
;; (in-module "Moo")
|
||||
;; (parent "GtkWindow")
|
||||
;; (c-name "MooPythonConsole")
|
||||
;; (gtype-id "MOO_TYPE_PYTHON_CONSOLE")
|
||||
;; )
|
||||
|
||||
|
||||
;; (define-boxed AppInfo
|
||||
;; (in-module "Moo")
|
||||
|
@ -40,54 +26,6 @@
|
|||
;; )
|
||||
|
||||
|
||||
;; From mooapp-python.h
|
||||
|
||||
(define-function python_execute_file
|
||||
(c-name "moo_app_python_execute_file")
|
||||
(return-type "none")
|
||||
(parameters
|
||||
'("GtkWindow*" "parent")
|
||||
)
|
||||
)
|
||||
|
||||
(define-method python_run_string
|
||||
(of-object "MooApp")
|
||||
(c-name "moo_app_python_run_string")
|
||||
(return-type "gboolean")
|
||||
(parameters
|
||||
'("const-char*" "string")
|
||||
)
|
||||
)
|
||||
|
||||
(define-method python_run_file
|
||||
(of-object "MooApp")
|
||||
(c-name "moo_app_python_run_file")
|
||||
(return-type "gboolean")
|
||||
(parameters
|
||||
'("const-char*" "filename")
|
||||
)
|
||||
)
|
||||
|
||||
(define-method get_python_console
|
||||
(of-object "MooApp")
|
||||
(c-name "moo_app_get_python_console")
|
||||
(return-type "GtkWidget*")
|
||||
)
|
||||
|
||||
(define-method show_python_console
|
||||
(of-object "MooApp")
|
||||
(c-name "moo_app_show_python_console")
|
||||
(return-type "none")
|
||||
)
|
||||
|
||||
(define-method hide_python_console
|
||||
(of-object "MooApp")
|
||||
(c-name "moo_app_hide_python_console")
|
||||
(return-type "none")
|
||||
)
|
||||
|
||||
|
||||
|
||||
;; From mooapp.h
|
||||
|
||||
(define-function get_instance
|
||||
|
|
|
@ -5,7 +5,6 @@ headers
|
|||
#define NO_IMPORT_PYGOBJECT
|
||||
#include "pygobject.h"
|
||||
#include "mooapp/mooapp.h"
|
||||
#include "mooapp/mooapp-python.h"
|
||||
#include "moopython/pygtk/moo-pygtk.h"
|
||||
|
||||
%%
|
||||
|
|
Loading…
Reference in New Issue