3
# This plugin is adapted from the Python Console plugin and the IPython
5
# http://ipython.scipy.org/moin/Cookbook/EmbeddingInGTK
6
# Copyright (C) 2009 Brian Parma
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License along
19
# with this program; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
from xl import settings
28
try: # xl doesn't exist outside of exaile
29
from xl.nls import gettext as _
32
from gettext import gettext as _
33
print 'Running outside of Exaile...'
36
import ipython_view as ip
38
import __builtin__, site
40
# FIXME: make font, colors into settings?
50
class Quitter(object):
51
"""Simple class to handle exit, similar to Python 2.5's.
53
This Quitter is used to circumvent IPython's circumvention
54
of the builtin Quitter, since it prevents exaile form closing."""
56
def __init__(self,exit,name):
61
return 'Type %s() to exit.' % self.name
65
self.exit() # Passed in exit function
66
site.setquit() # Restore default builtins
70
class IPyConsole(gtk.Window):
72
A gtk Window with an embedded IPython Console.
74
def __init__(self, namespace):
75
gtk.Window.__init__(self)
77
self.set_title(_("IPython Console - Exaile"))
78
self.set_size_request(750,550)
79
self.set_resizable(True)
81
sw = gtk.ScrolledWindow()
82
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
84
ipv = ip.IPythonView()
86
# so it's exposed in the shell
89
# change display to emulate dark gnome-terminal
90
console_font = settings.get_option('plugin/ipconsole/font', FONT)
92
ipv.modify_font(pango.FontDescription(console_font))
93
ipv.set_wrap_mode(gtk.WRAP_CHAR)
94
ipv.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('black'))
95
ipv.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse('lavender'))
96
ipv.IP.magic_colors('Linux') # IPython color scheme
98
# or white background?
99
# ipv.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('white'))
100
# ipv.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse('black'))
101
# ipv.IP.magic_colors('LightBG') # IPython color scheme
103
opacity = settings.get_option('plugin/ipconsole/opacity', 80.0)
105
if opacity < 100: self.set_opacity(float(opacity) / 100.0) # add a little transparency :)
106
ipv.updateNamespace(namespace) # expose exaile (passed in)
107
ipv.updateNamespace({'self':self}) # Expose self to IPython
109
# prevent exit and quit - freezes window? does bad things
110
ipv.updateNamespace({'exit':None,
113
# This is so when exaile calls exit(), IP doesn't prompt and prevent
115
__builtin__.exit = Quitter(ipv.IP.magic_Exit, 'exit')
116
__builtin__.quit = Quitter(ipv.IP.magic_Exit, 'quit')
126
self.connect('delete_event',lambda x,y:False)
134
MENU_ITEM = gtk.MenuItem(_('Show IPython Console'))
135
MENU_ITEM.connect('activate', show_console,exaile)
137
exaile.gui.xml.get_widget('tools_menu').append(MENU_ITEM)
139
# return True # bad! crashes compiz
141
def on_setting_change(event, settings, option):
142
if option == 'plugin/ipconsole/opacity' and PLUGIN:
143
value = settings.get_option(option, 80.0)
144
value = float(value) / 100.0
145
PLUGIN.set_opacity(value)
147
if option == 'plugin/ipconsole/font' and PLUGIN:
148
value = settings.get_option(option, FONT)
149
PLUGIN.ipv.modify_font(pango.FontDescription(value))
151
def __enb(evt, exaile, nothing):
152
gobject.idle_add(_enable, exaile)
153
event.add_callback(on_setting_change, 'option_set')
157
Called when plugin is enabled, or when exaile is loaded with the plugin
159
Wait for exaile to fully load, then call _enable with idle priority.
162
event.add_callback(__enb, "gui_loaded")
164
__enb(None, exaile, None)
168
Called when the plugin is disabled
170
global PLUGIN, MENU_ITEM
179
def show_console(widget, exaile):
181
Display window when the menu item is clicked.
185
PLUGIN = IPyConsole({'exaile': exaile})
186
PLUGIN.connect('destroy', console_destroyed)
189
def console_destroyed(*args):
191
Called when the window is closed.
197
if __name__ == '__main__':
199
If run outside of exaile.
202
con.connect('destroy', gtk.main_quit)