~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to plugins/ipconsole/__init__.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
# This plugin is adapted from the Python Console plugin and the IPython 
4
 
# cookbook at:
5
 
#   http://ipython.scipy.org/moin/Cookbook/EmbeddingInGTK
6
 
# Copyright (C) 2009 Brian Parma
7
 
#
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.
12
 
#
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.
17
 
#
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.
21
 
 
22
 
import sys
23
 
import gtk
24
 
import gobject
25
 
import ipconsoleprefs
26
 
from xl import settings
27
 
 
28
 
try:    # xl doesn't exist outside of exaile
29
 
    from xl.nls import gettext as _
30
 
    from xl import event
31
 
except:
32
 
    from gettext import gettext as _
33
 
    print 'Running outside of Exaile...'
34
 
    
35
 
 
36
 
import ipython_view as ip
37
 
import pango
38
 
import __builtin__, site
39
 
 
40
 
# FIXME: make font, colors into settings?
41
 
 
42
 
FONT = "Luxi Mono 10"
43
 
 
44
 
PLUGIN                  = None
45
 
MENU_ITEM               = None
46
 
 
47
 
def get_prefs_pane():
48
 
    return ipconsoleprefs
49
 
 
50
 
class Quitter(object):
51
 
    """Simple class to handle exit, similar to Python 2.5's.
52
 
 
53
 
       This Quitter is used to circumvent IPython's circumvention 
54
 
       of the builtin Quitter, since it prevents exaile form closing."""
55
 
    
56
 
    def __init__(self,exit,name):
57
 
        self.exit = exit
58
 
        self.name = name
59
 
      
60
 
    def __repr__(self):
61
 
        return 'Type %s() to exit.' % self.name
62
 
        __str__ = __repr__
63
 
 
64
 
    def __call__(self):
65
 
        self.exit()         # Passed in exit function
66
 
        site.setquit()      # Restore default builtins
67
 
        exit()              # Call builtin
68
 
 
69
 
 
70
 
class IPyConsole(gtk.Window):
71
 
    """
72
 
        A gtk Window with an embedded IPython Console.
73
 
    """
74
 
    def __init__(self, namespace):
75
 
        gtk.Window.__init__(self)
76
 
        
77
 
        self.set_title(_("IPython Console - Exaile"))
78
 
        self.set_size_request(750,550)
79
 
        self.set_resizable(True)
80
 
        
81
 
        sw = gtk.ScrolledWindow()
82
 
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
83
 
 
84
 
        ipv = ip.IPythonView()
85
 
 
86
 
        # so it's exposed in the shell
87
 
        self.ipv = ipv
88
 
 
89
 
        # change display to emulate dark gnome-terminal
90
 
        console_font = settings.get_option('plugin/ipconsole/font', FONT)
91
 
 
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
97
 
 
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
102
 
 
103
 
        opacity = settings.get_option('plugin/ipconsole/opacity', 80.0)
104
 
 
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
108
 
 
109
 
        # prevent exit and quit - freezes window? does bad things
110
 
        ipv.updateNamespace({'exit':None,
111
 
                             'quit':None})
112
 
                             
113
 
        # This is so when exaile calls exit(), IP doesn't prompt and prevent
114
 
        # it from closing
115
 
        __builtin__.exit = Quitter(ipv.IP.magic_Exit, 'exit')
116
 
        __builtin__.quit = Quitter(ipv.IP.magic_Exit, 'quit')
117
 
        
118
 
        ipv.show()
119
 
        
120
 
        sw.add(ipv)
121
 
        sw.show()
122
 
        
123
 
        self.add(sw)
124
 
        self.show()
125
 
        
126
 
        self.connect('delete_event',lambda x,y:False)
127
 
 
128
 
def _enable(exaile):
129
 
    """
130
 
        Enable plugin.
131
 
            Create menu item.
132
 
    """
133
 
    global MENU_ITEM
134
 
    MENU_ITEM = gtk.MenuItem(_('Show IPython Console'))
135
 
    MENU_ITEM.connect('activate', show_console,exaile)
136
 
 
137
 
    exaile.gui.xml.get_widget('tools_menu').append(MENU_ITEM)
138
 
    MENU_ITEM.show()
139
 
#    return True    # bad! crashes compiz
140
 
 
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)
146
 
 
147
 
    if option == 'plugin/ipconsole/font' and PLUGIN:
148
 
        value = settings.get_option(option, FONT)
149
 
        PLUGIN.ipv.modify_font(pango.FontDescription(value))
150
 
 
151
 
def __enb(evt, exaile, nothing):
152
 
    gobject.idle_add(_enable, exaile)
153
 
    event.add_callback(on_setting_change, 'option_set')
154
 
 
155
 
def enable(exaile):
156
 
    """
157
 
        Called when plugin is enabled, or when exaile is loaded with the plugin
158
 
        on by default.
159
 
            Wait for exaile to fully load, then call _enable with idle priority.
160
 
    """
161
 
    if exaile.loading:
162
 
        event.add_callback(__enb, "gui_loaded")
163
 
    else:
164
 
        __enb(None, exaile, None)
165
 
 
166
 
def disable(exaile):
167
 
    """
168
 
        Called when the plugin is disabled
169
 
    """
170
 
    global PLUGIN, MENU_ITEM
171
 
    if PLUGIN:
172
 
        PLUGIN.destroy()
173
 
        PLUGIN = None
174
 
    if MENU_ITEM:
175
 
        MENU_ITEM.hide()
176
 
        MENU_ITEM.destroy()
177
 
        MENU_ITEM = None
178
 
 
179
 
def show_console(widget, exaile):
180
 
    """
181
 
        Display window when the menu item is clicked.
182
 
    """
183
 
    global PLUGIN
184
 
    if PLUGIN is None:
185
 
        PLUGIN = IPyConsole({'exaile': exaile})
186
 
        PLUGIN.connect('destroy', console_destroyed)
187
 
    PLUGIN.present()
188
 
 
189
 
def console_destroyed(*args):
190
 
    """
191
 
        Called when the window is closed.
192
 
    """
193
 
    global PLUGIN
194
 
    PLUGIN = None
195
 
 
196
 
 
197
 
if __name__ == '__main__':
198
 
    """
199
 
        If run outside of exaile.
200
 
    """
201
 
    con = IPyConsole({})
202
 
    con.connect('destroy', gtk.main_quit)
203
 
    con.show()
204
 
    gtk.main()
205