summaryrefslogtreecommitdiff
path: root/src/plugins/pythonconsole
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2008-07-30 09:38:51 +0000
committerBastien Nocera <hadess@src.gnome.org>2008-07-30 09:38:51 +0000
commit25b3fff12e88d0f95075a1c070e2a39bd1c7b277 (patch)
tree443f1de9ebdcbac1a8f6a832a27e7dee3e2008c3 /src/plugins/pythonconsole
parent5225d2e98b6fd9bb695915a1a9c579518e2cff5b (diff)
downloadtotem-25b3fff12e88d0f95075a1c070e2a39bd1c7b277.tar.gz
Patch from Andrés de Barbará <adebarbara@gmail.com> to add a Python
2008-07-30 Bastien Nocera <hadess@hadess.net> * configure.in: * src/plugins/pythonconsole/Makefile.am: * src/plugins/pythonconsole/console.py: * src/plugins/pythonconsole/pythonconsole.py: * src/plugins/pythonconsole/pythonconsole.totem-plugin.in: * totem.spec.in: Patch from Andrés de Barbará <adebarbara@gmail.com> to add a Python console to Totem (Closes: #542470) svn path=/trunk/; revision=5528
Diffstat (limited to 'src/plugins/pythonconsole')
-rw-r--r--src/plugins/pythonconsole/Makefile.am13
-rw-r--r--src/plugins/pythonconsole/console.py311
-rw-r--r--src/plugins/pythonconsole/pythonconsole.py146
-rw-r--r--src/plugins/pythonconsole/pythonconsole.totem-plugin.in9
4 files changed, 479 insertions, 0 deletions
diff --git a/src/plugins/pythonconsole/Makefile.am b/src/plugins/pythonconsole/Makefile.am
new file mode 100644
index 000000000..f755b422b
--- /dev/null
+++ b/src/plugins/pythonconsole/Makefile.am
@@ -0,0 +1,13 @@
+plugindir = $(PLUGINDIR)/pythonconsole
+plugin_PYTHON = pythonconsole.py console.py
+
+plugin_in_files = pythonconsole.totem-plugin.in
+
+%.totem-plugin: %.totem-plugin.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; $(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache
+
+plugin_DATA = $(plugin_in_files:.totem-plugin.in=.totem-plugin)
+
+EXTRA_DIST = $(plugin_in_files) pythonconsole.py console.py
+
+CLEANFILES = $(plugin_DATA)
+DISTCLEANFILES = $(plugin_DATA)
diff --git a/src/plugins/pythonconsole/console.py b/src/plugins/pythonconsole/console.py
new file mode 100644
index 000000000..a2f7a3e7d
--- /dev/null
+++ b/src/plugins/pythonconsole/console.py
@@ -0,0 +1,311 @@
+# -*- coding: utf-8 -*-
+
+# console.py -- Console widget
+#
+# Copyright (C) 2006 - Steve Frécinaux
+#
+# 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, 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.
+
+# Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
+# Copyright (C), 1998 James Henstridge <james@daa.com.au>
+# Copyright (C), 2005 Adam Hooper <adamh@densi.com>
+# Bits from gedit Python Console Plugin
+# Copyrignt (C), 2005 Raphaël Slinckx
+#
+# The Totem project hereby grant permission for non-gpl compatible GStreamer
+# plugins to be used and distributed together with GStreamer and Totem. This
+# permission are above and beyond the permissions granted by the GPL license
+# Totem is covered by.
+#
+# Monday 7th February 2005: Christian Schaller: Add exception clause.
+# See license_change file for details.
+
+import string
+import sys
+import re
+import traceback
+import gobject
+import pango
+import gtk
+
+class PythonConsole(gtk.ScrolledWindow):
+ def __init__(self, namespace = {}, destroy_cb = None):
+ gtk.ScrolledWindow.__init__(self)
+
+ self.destroy_cb = destroy_cb
+ self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC);
+ self.set_shadow_type(gtk.SHADOW_IN)
+ self.view = gtk.TextView()
+ self.view.modify_font(pango.FontDescription('Monospace'))
+ self.view.set_editable(True)
+ self.view.set_wrap_mode(gtk.WRAP_WORD_CHAR)
+ self.add(self.view)
+ self.view.show()
+
+ buffer = self.view.get_buffer()
+ self.normal = buffer.create_tag("normal")
+ self.error = buffer.create_tag("error")
+ self.error.set_property("foreground", "red")
+ self.command = buffer.create_tag("command")
+ self.command.set_property("foreground", "blue")
+
+ self.__spaces_pattern = re.compile(r'^\s+')
+ self.namespace = namespace
+
+ self.block_command = False
+
+ # Init first line
+ buffer.create_mark("input-line", buffer.get_end_iter(), True)
+ buffer.insert(buffer.get_end_iter(), ">>> ")
+ buffer.create_mark("input", buffer.get_end_iter(), True)
+
+ # Init history
+ self.history = ['']
+ self.history_pos = 0
+ self.current_command = ''
+ self.namespace['__history__'] = self.history
+
+ # Set up hooks for standard output.
+ self.stdout = OutFile(self, sys.stdout.fileno(), self.normal)
+ self.stderr = OutFile(self, sys.stderr.fileno(), self.error)
+
+ # Signals
+ self.view.connect("key-press-event", self.__key_press_event_cb)
+ buffer.connect("mark-set", self.__mark_set_cb)
+
+
+ def __key_press_event_cb(self, view, event):
+ if event.keyval == gtk.keysyms.d and event.state == gtk.gdk.CONTROL_MASK:
+ self.destroy()
+
+ elif event.keyval == gtk.keysyms.Return and event.state == gtk.gdk.CONTROL_MASK:
+ # Get the command
+ buffer = view.get_buffer()
+ inp_mark = buffer.get_mark("input")
+ inp = buffer.get_iter_at_mark(inp_mark)
+ cur = buffer.get_end_iter()
+ line = buffer.get_text(inp, cur)
+ self.current_command = self.current_command + line + "\n"
+ self.history_add(line)
+
+ # Prepare the new line
+ cur = buffer.get_end_iter()
+ buffer.insert(cur, "\n... ")
+ cur = buffer.get_end_iter()
+ buffer.move_mark(inp_mark, cur)
+
+ # Keep indentation of precendent line
+ spaces = re.match(self.__spaces_pattern, line)
+ if spaces is not None:
+ buffer.insert(cur, line[spaces.start() : spaces.end()])
+ cur = buffer.get_end_iter()
+
+ buffer.place_cursor(cur)
+ gobject.idle_add(self.scroll_to_end)
+ return True
+
+ elif event.keyval == gtk.keysyms.Return:
+ # Get the marks
+ buffer = view.get_buffer()
+ lin_mark = buffer.get_mark("input-line")
+ inp_mark = buffer.get_mark("input")
+
+ # Get the command line
+ inp = buffer.get_iter_at_mark(inp_mark)
+ cur = buffer.get_end_iter()
+ line = buffer.get_text(inp, cur)
+ self.current_command = self.current_command + line + "\n"
+ self.history_add(line)
+
+ # Make the line blue
+ lin = buffer.get_iter_at_mark(lin_mark)
+ buffer.apply_tag(self.command, lin, cur)
+ buffer.insert(cur, "\n")
+
+ cur_strip = self.current_command.rstrip()
+
+ if cur_strip.endswith(":") \
+ or (self.current_command[-2:] != "\n\n" and self.block_command):
+ # Unfinished block command
+ self.block_command = True
+ com_mark = "... "
+ elif cur_strip.endswith("\\"):
+ com_mark = "... "
+ else:
+ # Eval the command
+ self.__run(self.current_command)
+ self.current_command = ''
+ self.block_command = False
+ com_mark = ">>> "
+
+ # Prepare the new line
+ cur = buffer.get_end_iter()
+ buffer.move_mark(lin_mark, cur)
+ buffer.insert(cur, com_mark)
+ cur = buffer.get_end_iter()
+ buffer.move_mark(inp_mark, cur)
+ buffer.place_cursor(cur)
+ gobject.idle_add(self.scroll_to_end)
+ return True
+
+ elif event.keyval == gtk.keysyms.KP_Down or event.keyval == gtk.keysyms.Down:
+ # Next entry from history
+ view.emit_stop_by_name("key_press_event")
+ self.history_down()
+ gobject.idle_add(self.scroll_to_end)
+ return True
+
+ elif event.keyval == gtk.keysyms.KP_Up or event.keyval == gtk.keysyms.Up:
+ # Previous entry from history
+ view.emit_stop_by_name("key_press_event")
+ self.history_up()
+ gobject.idle_add(self.scroll_to_end)
+ return True
+
+ elif event.keyval == gtk.keysyms.KP_Left or event.keyval == gtk.keysyms.Left or \
+ event.keyval == gtk.keysyms.BackSpace:
+ buffer = view.get_buffer()
+ inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
+ cur = buffer.get_iter_at_mark(buffer.get_insert())
+ return inp.compare(cur) == 0
+
+ elif event.keyval == gtk.keysyms.Home:
+ # Go to the begin of the command instead of the begin of the line
+ buffer = view.get_buffer()
+ inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
+ if event.state == gtk.gdk.SHIFT_MASK:
+ buffer.move_mark_by_name("insert", inp)
+ else:
+ buffer.place_cursor(inp)
+ return True
+
+ def __mark_set_cb(self, buffer, iter, name):
+ input = buffer.get_iter_at_mark(buffer.get_mark("input"))
+ pos = buffer.get_iter_at_mark(buffer.get_insert())
+ self.view.set_editable(pos.compare(input) != -1)
+
+ def get_command_line(self):
+ buffer = self.view.get_buffer()
+ inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
+ cur = buffer.get_end_iter()
+ return buffer.get_text(inp, cur)
+
+ def set_command_line(self, command):
+ buffer = self.view.get_buffer()
+ mark = buffer.get_mark("input")
+ inp = buffer.get_iter_at_mark(mark)
+ cur = buffer.get_end_iter()
+ buffer.delete(inp, cur)
+ buffer.insert(inp, command)
+ buffer.select_range(buffer.get_iter_at_mark(mark), buffer.get_end_iter())
+ self.view.grab_focus()
+
+ def history_add(self, line):
+ if line.strip() != '':
+ self.history_pos = len(self.history)
+ self.history[self.history_pos - 1] = line
+ self.history.append('')
+
+ def history_up(self):
+ if self.history_pos > 0:
+ self.history[self.history_pos] = self.get_command_line()
+ self.history_pos = self.history_pos - 1
+ self.set_command_line(self.history[self.history_pos])
+
+ def history_down(self):
+ if self.history_pos < len(self.history) - 1:
+ self.history[self.history_pos] = self.get_command_line()
+ self.history_pos = self.history_pos + 1
+ self.set_command_line(self.history[self.history_pos])
+
+ def scroll_to_end(self):
+ iter = self.view.get_buffer().get_end_iter()
+ self.view.scroll_to_iter(iter, 0.0)
+ return False
+
+ def write(self, text, tag = None):
+ buffer = self.view.get_buffer()
+ if tag is None:
+ buffer.insert(buffer.get_end_iter(), text)
+ else:
+ buffer.insert_with_tags(buffer.get_end_iter(), text, tag)
+ gobject.idle_add(self.scroll_to_end)
+
+ def eval(self, command, display_command = False):
+ buffer = self.view.get_buffer()
+ lin = buffer.get_mark("input-line")
+ buffer.delete(buffer.get_iter_at_mark(lin),
+ buffer.get_end_iter())
+
+ if isinstance(command, list) or isinstance(command, tuple):
+ for c in command:
+ if display_command:
+ self.write(">>> " + c + "\n", self.command)
+ self.__run(c)
+ else:
+ if display_command:
+ self.write(">>> " + c + "\n", self.command)
+ self.__run(command)
+
+ cur = buffer.get_end_iter()
+ buffer.move_mark_by_name("input-line", cur)
+ buffer.insert(cur, ">>> ")
+ cur = buffer.get_end_iter()
+ buffer.move_mark_by_name("input", cur)
+ self.view.scroll_to_iter(buffer.get_end_iter(), 0.0)
+
+ def __run(self, command):
+ sys.stdout, self.stdout = self.stdout, sys.stdout
+ sys.stderr, self.stderr = self.stderr, sys.stderr
+
+ try:
+ try:
+ r = eval(command, self.namespace, self.namespace)
+ if r is not None:
+ print `r`
+ except SyntaxError:
+ exec command in self.namespace
+ except:
+ if hasattr(sys, 'last_type') and sys.last_type == SystemExit:
+ self.destroy()
+ else:
+ traceback.print_exc()
+
+ sys.stdout, self.stdout = self.stdout, sys.stdout
+ sys.stderr, self.stderr = self.stderr, sys.stderr
+
+ def destroy(self):
+ pass
+ #gtk.ScrolledWindow.destroy(self)
+
+class OutFile:
+ """A fake output file object. It sends output to a TK test widget,
+ and if asked for a file number, returns one set on instance creation"""
+ def __init__(self, console, fn, tag):
+ self.fn = fn
+ self.console = console
+ self.tag = tag
+ def close(self): pass
+ def flush(self): pass
+ def fileno(self): return self.fn
+ def isatty(self): return 0
+ def read(self, a): return ''
+ def readline(self): return ''
+ def readlines(self): return []
+ def write(self, s): self.console.write(s, self.tag)
+ def writelines(self, l): self.console.write(l, self.tag)
+ def seek(self, a): raise IOError, (29, 'Illegal seek')
+ def tell(self): raise IOError, (29, 'Illegal seek')
+ truncate = tell
diff --git a/src/plugins/pythonconsole/pythonconsole.py b/src/plugins/pythonconsole/pythonconsole.py
new file mode 100644
index 000000000..cee78e348
--- /dev/null
+++ b/src/plugins/pythonconsole/pythonconsole.py
@@ -0,0 +1,146 @@
+# -*- coding: utf-8 -*-
+
+# pythonconsole.py -- plugin object
+#
+# Copyright (C) 2006 - Steve Frécinaux
+#
+# 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, 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.
+
+# Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
+# Copyright (C), 1998 James Henstridge <james@daa.com.au>
+# Copyright (C), 2005 Adam Hooper <adamh@densi.com>
+# Bits from gedit Python Console Plugin
+# Copyrignt (C), 2005 Raphaël Slinckx
+#
+# The Totem project hereby grant permission for non-gpl compatible GStreamer
+# plugins to be used and distributed together with GStreamer and Totem. This
+# permission are above and beyond the permissions granted by the GPL license
+# Totem is covered by.
+#
+# Monday 7th February 2005: Christian Schaller: Add exception clause.
+# See license_change file for details.
+
+from console import PythonConsole
+
+__all__ = ('PythonConsole', 'OutFile')
+
+import gtk
+import totem
+import gconf
+import gobject
+try:
+ import rpdb2
+ have_rpdb2 = True
+except:
+ have_rpdb2 = False
+
+ui_str = """
+<ui>
+ <menubar name="tmw-menubar">
+ <menu name="Python" action="Python">
+ <placeholder name="ToolsOps_5">
+ <menuitem name="PythonConsole" action="PythonConsole"/>
+ <menuitem name="PythonDebugger" action="PythonDebugger"/>
+ </placeholder>
+ </menu>
+ </menubar>
+</ui>
+"""
+
+class PythonConsolePlugin(totem.Plugin):
+ def __init__(self):
+ totem.Plugin.__init__(self)
+ self.window = None
+
+ def activate(self, totem_object):
+
+ data = dict()
+ manager = totem_object.get_ui_manager()
+
+ data['action_group'] = gtk.ActionGroup('Python')
+
+ action = gtk.Action('Python', 'Python', _('Python Console Menu'), None)
+ data['action_group'].add_action(action)
+
+ action = gtk.Action('PythonConsole', _('_Python Console'),
+ _("Show Totem's python console"),
+ 'gnome-mime-text-x-python')
+ action.connect('activate', self.show_console, totem_object)
+ data['action_group'].add_action(action)
+
+ action = gtk.Action('PythonDebugger', _('Python Debugger'),
+ _("Enable remote python debugging with rpdb2"),
+ None)
+ if have_rpdb2:
+ action.connect('activate', self.enable_debugging, totem_object)
+ else:
+ action.set_visible(False)
+ data['action_group'].add_action(action)
+
+ manager.insert_action_group(data['action_group'], 0)
+ data['ui_id'] = manager.add_ui_from_string(ui_str)
+ manager.ensure_update()
+
+ totem_object.set_data('PythonConsolePluginInfo', data)
+
+ def show_console(self, action, totem_object):
+ if not self.window:
+ console = PythonConsole(namespace = {'__builtins__' : __builtins__,
+ 'totem' : totem,
+ 'totem_object' : totem_object},
+ destroy_cb = self.destroy_console)
+
+ console.set_size_request(600, 400)
+ console.eval(_('print "You can access the totem object through ' \
+ '\'totem_object\' :\\n%s" % totem_object'), False)
+
+ self.window = gtk.Window()
+ self.window.set_title(_('Totem Python Console'))
+ self.window.add(console)
+ self.window.connect('destroy', self.destroy_console)
+ self.window.show_all()
+ else:
+ self.window.show_all()
+ self.window.grab_focus()
+
+ def enable_debugging(self, action, totem_object):
+ msg = _("After you press OK, Totem will wait until you connect to it with winpdb or rpdb2. If you have not set a debugger password in GConf, it will use the default password ('totem').")
+ dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK_CANCEL, msg)
+ if dialog.run() == gtk.RESPONSE_OK:
+ gconfclient = gconf.client_get_default()
+ password = gconfclient.get_string('/apps/totem/plugins/pythonconsole/rpdb2_password') or "totem"
+ def start_debugger(password):
+ rpdb2.start_embedded_debugger(password)
+ return False
+
+ gobject.idle_add(start_debugger, password)
+ dialog.destroy()
+
+ def destroy_console(self, *args):
+ self.window.destroy()
+ self.window = None
+
+ def deactivate(self, totem_object):
+ data = totem_object.get_data('PythonConsolePluginInfo')
+
+ manager = totem_object.get_ui_manager()
+ manager.remove_ui(data['ui_id'])
+ manager.remove_action_group(data['action_group'])
+ manager.ensure_update()
+
+ totem_object.set_data('PythonConsolePluginInfo', None)
+
+ if self.window is not None:
+ self.window.destroy()
diff --git a/src/plugins/pythonconsole/pythonconsole.totem-plugin.in b/src/plugins/pythonconsole/pythonconsole.totem-plugin.in
new file mode 100644
index 000000000..0bb8c84c2
--- /dev/null
+++ b/src/plugins/pythonconsole/pythonconsole.totem-plugin.in
@@ -0,0 +1,9 @@
+[Totem Plugin]
+Loader=python
+Module=pythonconsole
+IAge=1
+_Name=Python Console
+_Description=Interactive python console.
+Authors=Steve Frécinaux <steve@istique.net>
+Copyright=Copyright © 2006 Steve Frécinaux
+Website=http://www.gnome.org/projects/totem/