~qioeujqioejqioe-deactivatedaccount/exaile/missing-signals

« back to all changes in this revision

Viewing changes to plugins/console.py

  • Committer: Adam Olsen
  • Date: 2007-08-31 19:36:41 UTC
  • Revision ID: arolsen@gmail.com-20070831193641-isghtp1fq2433m2m
Moving the plugins directory into the source directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Python console plugin for Exaile media player
 
4
# Copyright (C) 2007 Johannes Sasongko
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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.
 
19
 
 
20
import sys
 
21
import traceback
 
22
from cStringIO import StringIO
 
23
from gettext import gettext as _
 
24
import gtk
 
25
 
 
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
 
30
manipulate Exaile."""
 
31
 
 
32
PLUGIN_ICON = None
 
33
PLUGIN_ENABLED = False
 
34
 
 
35
class PyConsole(gtk.Window):
 
36
    def __init__(self, dict):
 
37
        gtk.Window.__init__(self)
 
38
        self.dict = dict
 
39
 
 
40
        self.buffer = StringIO()
 
41
 
 
42
        self.set_title(_("Python Console - Exaile"))
 
43
        self.set_border_width(12)
 
44
        self.set_default_size(450, 250)
 
45
 
 
46
        vbox = gtk.VBox(False, 12)
 
47
        self.add(vbox)
 
48
 
 
49
        sw = gtk.ScrolledWindow()
 
50
        vbox.pack_start(sw)
 
51
        sw.set_shadow_type(gtk.SHADOW_IN)
 
52
        self.text_view = tv = gtk.TextView()
 
53
        sw.add(tv)
 
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)
 
58
 
 
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)
 
66
 
 
67
        entry.grab_focus()
 
68
        vbox.show_all()
 
69
 
 
70
    def entry_activated(self, entry, user_data=None):
 
71
        """
 
72
            Called when the user presses Return on the GtkEntry.
 
73
        """
 
74
        self.execute(entry.get_text())
 
75
        entry.select_region(0, -1)
 
76
 
 
77
    def execute(self, code):
 
78
        """
 
79
            Executes some Python code.
 
80
        """
 
81
        try:
 
82
            pycode = compile(code, '<console>', 'single')
 
83
            stdout = sys.stdout
 
84
            sys.stdout = self.buffer
 
85
            exec pycode in self.dict
 
86
            sys.stdout = stdout
 
87
            result = self.buffer.getvalue()
 
88
            # Can't simply close and recreate later because help() stores and
 
89
            # reuses stdout.
 
90
            self.buffer.truncate(0) 
 
91
        except:
 
92
            exc = traceback.format_exception(*sys.exc_info())
 
93
            del exc[1] # Remove our function.
 
94
            result = ''.join(exc)
 
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)
 
99
 
 
100
PLUGIN = None
 
101
MENU_ITEM = None
 
102
 
 
103
def initialize():
 
104
    global MENU_ITEM
 
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)
 
108
    MENU_ITEM.show()
 
109
    return True
 
110
 
 
111
def destroy():
 
112
    global PLUGIN, MENU_ITEM
 
113
    if PLUGIN:
 
114
        PLUGIN.destroy()
 
115
        PLUGIN = None
 
116
    if MENU_ITEM:
 
117
        MENU_ITEM.hide()
 
118
        MENU_ITEM.destroy()
 
119
        MENU_ITEM = None
 
120
 
 
121
def show_console(*args):
 
122
    global PLUGIN
 
123
    if not PLUGIN:
 
124
        PLUGIN = PyConsole({'exaile': APP})
 
125
        PLUGIN.connect('destroy', console_destroyed)
 
126
    PLUGIN.present()
 
127
 
 
128
def console_destroyed(*args):
 
129
    global PLUGIN
 
130
    PLUGIN = None
 
131
 
 
132
 
 
133
if __name__ == '__main__':
 
134
    console = PyConsole({})
 
135
    console.connect('destroy', gtk.main_quit)
 
136
    console.show()
 
137
    gtk.main()
 
138
 
 
139
# vi: et ts=4 sts=4 sw=4