3
# Python console plugin for Exaile media player
4
# Copyright (C) 2007 Johannes Sasongko
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
from cStringIO import StringIO
23
from gettext import gettext as _
26
PLUGIN_NAME = "Python Console"
27
PLUGIN_VERSION = '0.1.0'
28
PLUGIN_AUTHORS = ["Johannes Sasongko <sasongko@gmail.com>"]
29
PLUGIN_DESCRIPTION = r"""Provides a Python console that can be used to
33
PLUGIN_ENABLED = False
35
class PyConsole(gtk.Window):
36
def __init__(self, dict):
37
gtk.Window.__init__(self)
40
self.buffer = StringIO()
42
self.set_title(_("Python Console - Exaile"))
43
self.set_border_width(12)
44
self.set_default_size(450, 250)
46
vbox = gtk.VBox(False, 12)
49
sw = gtk.ScrolledWindow()
51
sw.set_shadow_type(gtk.SHADOW_IN)
52
self.text_view = tv = gtk.TextView()
54
tv.set_editable(False)
55
tv.set_wrap_mode = gtk.WRAP_CHAR
56
self.text_buffer = buff = tv.get_buffer()
57
self.end_mark = buff.create_mark(None, buff.get_end_iter(), False)
59
hbox = gtk.HBox(False, 6)
60
vbox.pack_start(hbox, False)
61
label = gtk.Label('>>>')
62
hbox.pack_start(label, False)
63
self.entry = entry = gtk.Entry()
64
hbox.pack_start(entry)
65
entry.connect('activate', self.entry_activated)
70
def entry_activated(self, entry, user_data=None):
72
Called when the user presses Return on the GtkEntry.
74
self.execute(entry.get_text())
75
entry.select_region(0, -1)
77
def execute(self, code):
79
Executes some Python code.
82
pycode = compile(code, '<console>', 'single')
84
sys.stdout = self.buffer
85
exec pycode in self.dict
87
result = self.buffer.getvalue()
88
# Can't simply close and recreate later because help() stores and
90
self.buffer.truncate(0)
92
exc = traceback.format_exception(*sys.exc_info())
93
del exc[1] # Remove our function.
95
result = '>>> %s\n%s' % (code, result)
96
self.text_buffer.insert(self.text_buffer.get_end_iter(), result)
97
# Can't use iter, won't scroll correctly.
98
self.text_view.scroll_to_mark(self.end_mark, 0)
105
MENU_ITEM = gtk.MenuItem('Show Python Console')
106
MENU_ITEM.connect('activate', show_console)
107
APP.xml.get_widget('tools_menu').get_submenu().append(MENU_ITEM)
112
global PLUGIN, MENU_ITEM
121
def show_console(*args):
124
PLUGIN = PyConsole({'exaile': APP})
125
PLUGIN.connect('destroy', console_destroyed)
128
def console_destroyed(*args):
133
if __name__ == '__main__':
134
console = PyConsole({})
135
console.connect('destroy', gtk.main_quit)
139
# vi: et ts=4 sts=4 sw=4