summaryrefslogtreecommitdiff
path: root/examples/ide/pyide.py
blob: 9b6ea5246c41c13bda0580f53c7750dc1601c43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import gtkcons, gtkdb, gtkprof, edit, dialogs
import os, sys, string

# select a good VT emulator
for vt in 'Eterm', 'nxterm', 'xterm-color', 'xterm', 'rxvt':
    for dirname in string.split(os.environ['PATH'], os.pathsep):
        fullname = os.path.join(dirname, vt)
        if os.path.exists(fullname):
            VT_CMD = fullname + ' -geometry 80x6 -e '
            break
    else:
        continue
    break
else:
    VT_CMD=''  # this is not ideal

ui_string = """<ui>
<menubar>
  <menu action='FileMenu'>
    <menuitem action='FileNew'/>
    <menuitem action='FileOpen'/>
    <separator/>
    <menuitem action='FileExit'/>
  </menu>
  <menu action='EditMenu'>
    <menuitem action='EditCopy'/>
    <menuitem action='EditPaste'/>
    <menuitem action='EditClear'/>
  </menu>
  <placeholder name='OtherMenus'/>
  <menu action='HelpMenu' position='bot'>
    <menuitem action='HelpAbout'/>
  </menu>
</menubar>
</ui>
"""
pythonmenu_uistring = """<ui>
<menubar>
 <placeholder name='OtherMenus'>
  <menu name='PythonMenu' action='PythonMenu'>
    <menuitem action='PythonReload'/>
    <menuitem action='PythonRun'/>
    <menuitem action='PythonDebug'/>
    <menuitem action='PythonProfile'/>
  </menu>
 </placeholder>
</menubar>
</ui>
"""

class Application(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.connect("destroy", self.quit)
        self.connect("delete_event", self.quit)
        self.set_title("Python")
        self.set_size_request(475, 325)
        self.main_box = gtk.VBox()
        self.add(self.main_box)
        self.main_box.show()
        hdlbox = gtk.HandleBox()
        self.main_box.pack_start(hdlbox, expand=False)
        hdlbox.show()
        actions = [
            ('FileMenu', None, '_File'),
            ('FileNew', gtk.STOCK_NEW, None, None, None, self.file_new),
            ('FileOpen', gtk.STOCK_OPEN, None, None, None, self.file_open),
            ('FileExit', gtk.STOCK_QUIT, None, None, None, self.file_exit),
            ('EditMenu', None, '_Edit'),
            ('EditCopy', gtk.STOCK_COPY, None, None, None, self.edit_copy),
            ('EditPaste', gtk.STOCK_PASTE, None, None, None, self.edit_paste),
            ('EditClear', gtk.STOCK_REMOVE, 'C_lear', None,  None,
             self.edit_clear),
            ('HelpMenu', gtk.STOCK_HELP),
            ('HelpAbout', None, 'A_bout', None, None, self.help_about),
            ]
        python_actions = [
            ('PythonMenu', None, '_Python'),
            ('PythonReload', None, '_Reload Module...', None, None,
             self.python_reload),
            ('PythonRun', None, 'R_un...', None, None, self.python_run),
            ('PythonDebug', None, '_Debug...', None, None, self.python_debug),
            ('PythonProfile', None, 'Pro_file...', None, None,
             self.python_prof),
            ]
        self.ag = gtk.ActionGroup('ide')
        self.ag.add_actions(actions)
        self.ag.add_actions(python_actions)
        self.ui = gtk.UIManager()
        self.ui.insert_action_group(self.ag, 0)
        self.ui.add_ui_from_string(ui_string)
        self.ui.add_ui_from_string(pythonmenu_uistring)
        self.add_accel_group(self.ui.get_accel_group())
        hdlbox.add(self.ui.get_widget('/menubar'))
        #self.ui.get_widget('/menubar').show()
        self.interp = gtkcons.Console(
            namespace={'__builtins__': __builtins__,
                       '__name__': '__main__',
                       '__doc__': None}, quit_cb=self.quit)
        self.main_box.pack_start(self.interp)
        self.interp.show()
        self.interp.init()
        self.editwins = []
        return

    def quit(self, *args):
        for win in self.editwins:
            if win.chk_save(): return
            win.hide()
            win.destroy()
        gtk.main_quit()
        return

    def reload_file(self, fname):
        if not os.path.isfile(fname):
            gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                              gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                              fname + " was not found.")
            return
        dir = os.path.dirname(fname)
        base = os.path.basename(fname)
        if dir not in sys.path: sys.path.insert(0, dir)
        if   string.lower(base[-3:]) == '.py':  base = base[:-3]
        elif string.lower(base[-4:]) == '.pyc': base = base[:-4]
        if not sys.modules.has_key(base):
            self.interp.run('import ' + base)
        else:
            self.interp.run('import ' + base)
            self.interp.run('reload(' + base + ')')
        return

    # execute a python script normally or with the debugger or profiler
    def run_script(self, fname):
        if not fname or not os.path.exists(fname):
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Invalid filename "+fname)
            dlg.run()
            return
        args = dialogs.InputBox("Arguments",
                                "Enter any command line arguments", self)
        if args == None: return
        os.system(VT_CMD+'python "'+fname+'" ' + args + ' &')
        return
    def debug_script(self, fname):
        if not fname or not os.path.exists(fname):
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Invalid filename "+fname)
            dlg.run()
            return
        args = dialogs.InputBox("Arguments",
                                "Enter any command line arguments", self)
        if args == None: return
        os.system(VT_CMD+'python '+gtkdb.__file__+' "'+fname+'" ' +
                  args + ' &')
        return
    def profile_script(self, fname):
        if not fname or not os.path.exists(fname):
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Invalid filename "+fname)
            dlg.run()
            return
        args = dialogs.InputBox("Arguments",
                                "Enter any command line arguments", self)
        if args == None: return
        os.system(VT_CMD+'python '+gtkprof.__file__+' "'+fname+'" ' +
                  args + ' &')
        return

    def add_py_menu(self, ew):
        python_actions = [
            ('PythonMenu', None, '_Python'),
            ('PythonReload', None, '_Reload Module'),
            ('PythonRun', None, 'R_un...', None, None,
             lambda w, ew=ew: self.run_script(ew.fname)),
            ('PythonDebug', None, '_Debug...', None, None,
             lambda w, ew=ew: self.debug_script(ew.fname)),
            ('PythonProfile', None, 'Pro_file...', None, None,
             lambda w, ew=ew: self.profile_script(ew.fname)),
            ]
        ew.ag.add_actions(python_actions)
        ew.ui.add_ui_from_string(pythonmenu_uistring)
        return

    def file_new(self, mi=None):
        ew = edit.EditWindow(quit_cb=self.rem_editwin)
        self.editwins.append(ew)
        self.add_py_menu(ew)
        ew.show()
        ew.set_size_request(0,0)
        return
    def file_open(self, mi=None):
        fname = dialogs.OpenFile('Open', self)
        if fname:
            ew = edit.EditWindow(quit_cb=self.rem_editwin)
            ew.load_file(fname)
            self.editwins.append(ew)
            self.add_py_menu(ew)
            ew.show()
            ew.set_size_request(0,0)
        return
    def rem_editwin(self, win=None, event=None):
        for i in range(len(self.editwins)):
            if self.editwins[i] == win:
                del self.editwins[i]
                break
        return
    def file_exit(self, mi=None):
        self.quit()
        return
    def edit_copy(self, mi=None):
        self.interp.text.copy_clipboard(0)
        return
    def edit_paste(self, mi=None):
        self.interp.line.paste_clipboard(0)
        return
    def edit_clear(self, mi=None):
        self.interp.line.delete_selection()
        return
    def python_reload(self, mi=None):
        print "python_reload"
        return
    def python_run(self, mi=None):
        fname = dialogs.OpenFile("Run", self)
        if fname:
            self.run_script(fname)
        return
    def python_debug(self, mi=None):
        fname = dialogs.OpenFile("Debug", self)
        if fname:
            self.debug_script(fname)
        return
    def python_prof(self, mi=None):
        fname = dialogs.OpenFile("Profile", self)
        if fname:
            self.profile_script(fname)
        return
    def help_about(self, mi=None):
        dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                                "Copyright (C)\n" \
                                "1998 James Henstridge\n" \
                                "2004 John Finlay\n" \
                                "This program is covered by the GPL>=2")
        dlg.run()
        dlg.hide()
        return

if __name__ == '__main__':
    app = Application()
    app.show()
    app.set_size_request(0,0)
    gtk.main()