-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonIDE.py
More file actions
346 lines (252 loc) · 11.1 KB
/
pythonIDE.py
File metadata and controls
346 lines (252 loc) · 11.1 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#-------------------------------------------------------------------------------
# Name: pythonIDE
# Purpose: A very basic Python IDE for Python scripts, potentially a basis for a fuller IDE
#
# Author: Simon Cox
#
# Created: 17/10/2015
# Copyright: (c) Simon Cox 2015
# Licence: Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
#-------------------------------------------------------------------------------
import wx
import threading
import os
import subprocess
import sys
# Define result event
EVT_RESULT_ID = wx.NewId()
def EVT_RESULT(win, func):
"""Define Result Event."""
win.Connect(-1, -1, EVT_RESULT_ID, func)
class ResultEvent(wx.PyEvent):
"""Simple event to carry arbitrary result data."""
def __init__(self, data):
"""Init Result Event."""
wx.PyEvent.__init__(self)
self.SetEventType(EVT_RESULT_ID)
self.data = data
# Define finished event
EVT_FINISHED_ID = wx.NewId()
def EVT_FINISHED(win, func):
"""Define Result Event."""
win.Connect(-1, -1, EVT_FINISHED_ID, func)
class FinishedEvent(wx.PyEvent):
"""Simple event to carry arbitrary result data."""
def __init__(self):
"""Init Finished Event."""
wx.PyEvent.__init__(self)
self.SetEventType(EVT_FINISHED_ID)
class WorkerThread(threading.Thread):
"""Worker Thread Class."""
def __init__(self, notify_window, process):
"""Init Worker Thread Class."""
threading.Thread.__init__(self)
self._notify_window = notify_window
self.process = process
self.start()
def run(self):
"""Run Worker Thread."""
while True:
line = self.process.stdout.readline()
if line != '':
wx.PostEvent(self._notify_window, ResultEvent(line))
else:
break
wx.PostEvent(self._notify_window, FinishedEvent())
def abort(self):
"""abort worker thread."""
# Method for use by main thread to signal an abort
self.process.terminate()
class ScriptPanel(wx.Panel):
def __init__(self, parent, path=None):
wx.Panel.__init__(self, parent)
EVT_RESULT(self,self.OnResult)
EVT_FINISHED(self,self.OnFinished)
self.InitUI()
self.worker = None
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.path = path
self.parent = parent
if path:
file = open(path, 'r')
self.scripteditor.AppendText(file.read())
file.close()
def InitUI(self):
vbox = wx.BoxSizer(wx.VERTICAL)
self.scripteditor = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.scripteditor.SetBackgroundColour('#ededed')
vbox.Add(self.scripteditor, 1, wx.EXPAND | wx.ALL, 0)
self.scriptoutput = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.scriptoutput.SetBackgroundColour('#ededed')
vbox.Add(self.scriptoutput, 1, wx.EXPAND | wx.ALL, 0)
self.SetSizer(vbox)
def RunScript(self, event):
"""Start Computation."""
# Trigger the worker thread unless it's already busy
if not self.worker:
if self.path:
pythonfile = self.path
else:
tempfile = open('tempfile.py','w')
tempfile.write(self.scripteditor.GetValue())
tempfile.close()
pythonfile = os.path.join(os.getcwd(), 'tempfile.py')
process = subprocess.Popen([sys.executable,'-u',pythonfile, '2>&1'], stdout=subprocess.PIPE)
self.worker = WorkerThread(self, process)
def StopScript(self, event):
"""Stop Computation."""
# Flag the worker thread to stop if running
if self.worker:
self.worker.abort()
def OnResult(self, event):
"""Show Result status."""
self.scriptoutput.AppendText(event.data)
def OnFinished(self, event):
self.worker = None
def OnSave(self, event):
print self.path
if self.path:
f = open(self.path, 'w')
f.write(self.scripteditor.GetValue())
f.close()
else:
self.OnSaveAs(event)
def OnSaveAs(self, event):
saveFileDialog = wx.FileDialog(self, "Save Python file", "", "", "Python files (*.py)|*.py", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if saveFileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed idea...
f = open(saveFileDialog.GetPath(), 'w')
f.write(self.scripteditor.GetValue())
f.close()
self.path = saveFileDialog.GetPath()
self.parent.SetPageText(self.parent.GetSelection(),os.path.split(self.path)[1])
def OnClose(self, event):
if self.worker:
self.worker.abort()
self.parent.DeletePage(self.parent.GetSelection())
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, None, title=title)
# Here we create a panel and a notebook on the panel
p = wx.Panel(self)
self.notebook = wx.Notebook(p)
# create the page windows as children of the notebook
page1 = ScriptPanel(self.notebook)
page2 = ScriptPanel(self.notebook)
page3 = ScriptPanel(self.notebook)
# add the pages to the notebook with the label to show on the tab
self.notebook.AddPage(page1, "Page 1")
self.notebook.AddPage(page2, "Page 2")
self.notebook.AddPage(page3, "Page 3")
# finally, put the notebook in a sizer for the panel to manage
# the layout
sizer = wx.BoxSizer()
sizer.Add(self.notebook, 1, wx.EXPAND | wx.ALL)
p.SetSizer(sizer)
self.createMenubar()
self.createToolbar()
self.Maximize(True)
self.Show()
def createMenubar(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
fitem1 = fileMenu.Append(wx.ID_OPEN, 'Open', 'Open script')
self.Bind(wx.EVT_MENU, self.OnOpen, fitem1)
fitem2 = fileMenu.Append(wx.ID_SAVE, 'Save', 'Save script')
self.Bind(wx.EVT_MENU, self.OnSave, fitem2)
fitem3 = fileMenu.Append(wx.ID_SAVEAS, 'Save as', 'Save script as')
self.Bind(wx.EVT_MENU, self.OnSaveAs, fitem3)
fitem4 = fileMenu.Append(wx.ID_CLOSE, 'Close', 'Close script')
self.Bind(wx.EVT_MENU, self.OnClose, fitem4)
fitem5 = fileMenu.Append(wx.ID_NEW, 'New', 'New script')
self.Bind(wx.EVT_MENU, self.CreateTab, fitem5)
fitem6 = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
self.Bind(wx.EVT_MENU, self.OnQuit, fitem6)
runMenu = wx.Menu()
ritem1 = runMenu.Append(wx.ID_ANY, 'Run', 'Run script')
self.Bind(wx.EVT_MENU, self.RunScript, ritem1)
ritem2 = runMenu.Append(wx.ID_ANY, 'Stop', 'Stop script')
self.Bind(wx.EVT_MENU, self.StopScript, ritem2)
helpMenu = wx.Menu()
hitem1 = helpMenu.Append(wx.ID_ANY, 'About', 'About Python IDE')
self.Bind(wx.EVT_MENU, self.OnAbout, hitem1)
menubar.Append(fileMenu, '&File')
menubar.Append(runMenu, '&Run')
menubar.Append(helpMenu, '&Help')
self.SetMenuBar(menubar)
def createToolbar(self):
"""
Create a toolbar.
"""
self.toolbar = self.CreateToolBar()
self.toolbar.SetToolBitmapSize((16,16)) # sets icon size
# Use wx.ArtProvider for default icons
go_ico = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_TOOLBAR, (16,16))
goTool = self.toolbar.AddSimpleTool(wx.ID_ANY, go_ico, "Run", "Runs the current file")
self.Bind(wx.EVT_MENU, self.RunScript, goTool)
stop_ico = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR, (16,16))
stopTool = self.toolbar.AddSimpleTool(wx.ID_ANY, stop_ico, "Stop", "Stops the current execution")
self.Bind(wx.EVT_MENU, self.StopScript, stopTool)
self.toolbar.AddSeparator()
open_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (16,16))
openTool = self.toolbar.AddSimpleTool(wx.ID_ANY, open_ico, "Open", "Open a file")
self.Bind(wx.EVT_TOOL, self.OnOpen, openTool)
save_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, (16,16))
saveTool = self.toolbar.AddSimpleTool(wx.ID_ANY, save_ico, "Save", "")
self.Bind(wx.EVT_TOOL, self.OnSave, saveTool)
saveas_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, (16,16))
saveasTool = self.toolbar.AddSimpleTool(wx.ID_ANY, saveas_ico, "Save as", "")
self.Bind(wx.EVT_TOOL, self.OnSaveAs, saveasTool)
close_ico = wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR, (16,16))
closeTool = self.toolbar.AddSimpleTool(wx.ID_ANY, close_ico, "Close", "")
self.Bind(wx.EVT_TOOL, self.OnClose, closeTool)
new_ico = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, (16,16))
newTool = self.toolbar.AddSimpleTool(wx.ID_ANY, new_ico, "Close", "")
self.Bind(wx.EVT_TOOL, self.CreateTab, newTool)
# This basically shows the toolbar
self.toolbar.Realize()
def RunScript(self, event):
if len(self.notebook.Children):
self.notebook.Children[self.notebook.GetSelection()].RunScript(event)
def StopScript(self, event):
if len(self.notebook.Children):
self.notebook.Children[self.notebook.GetSelection()].StopScript(event)
def OnOpen(self, event):
openFileDialog = wx.FileDialog(self, "Open Python file", "", "", "Python files (*.py)|*.py", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if openFileDialog.ShowModal() == wx.ID_CANCEL:
return
self.CreateTab(event, openFileDialog.GetPath())
def OnSave(self, event):
if len(self.notebook.Children):
self.notebook.Children[self.notebook.GetSelection()].OnSave(event)
def OnSaveAs(self, event):
if len(self.notebook.Children):
self.notebook.Children[self.notebook.GetSelection()].OnSaveAs(event)
def OnClose(self, event):
if len(self.notebook.Children):
self.notebook.Children[self.notebook.GetSelection()].OnClose(event)
def CreateTab(self, event, path=None):
page1 = ScriptPanel(self.notebook, path=path)
if path:
tabname = os.path.split(path)[1]
else:
tabname = "New tab"
self.notebook.AddPage(page1, tabname)
self.notebook.ChangeSelection(len(self.notebook.Children) - 1)
def OnAbout(self, event):
message = """Name: pythonIDE
Purpose: A very basic Python IDE for Python scripts, potentially a basis for a fuller IDE
Author: Simon Cox
Created: 17th October 2015
Copyright: (c) Simon Cox 2015
Licence: Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
With thanks to: Everyone who works on open source and shares their code and knowledge online"""
dlg = wx.MessageDialog(self, message, 'About', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = wx.App()
MainFrame(None, title='pythonIDE')
app.MainLoop()