-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonRunner.py
More file actions
333 lines (297 loc) · 13.6 KB
/
PythonRunner.py
File metadata and controls
333 lines (297 loc) · 13.6 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
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# Name: PythonRunner.py
# Purpose: This WX component is used to play or create python programs on
# the fly. This allows you to create, save and open existing
# programs.
#
# Author: Michael Rydeen
#
# Created: 2010/11/03
# Copyright: (c) 2010
# Licence: Use as you wish.
#----------------------------------------------------------------------------
# History
import wx, sys, traceback, os, time, threading
import wx.stc as stc
from io import StringIO
# Variable to stop execution.
STOP = False
class Logger:
def __init__( self, log ):
self.out = log
return
def write( self, string ):
self.out.AppendText(string)
return
class Executor(threading.Thread):
def __init__(self, parent, pythonCode):
self.parent = parent
self.pythonCode = pythonCode
threading.Thread.__init__(self)
return
def run(self):
try:
sys.settrace(traceCalls)
buffer = Logger(self.parent.resultText)
sys.stdout = buffer
exec(self.pythonCode)
sys.stdout = sys.__stdout__
#if ( len(buffer.getvalue()) > 0 ):
# self.resultText.SetValue( buffer.getvalue() )
#else:
# self.resultText.SetValue( "No output detected" )
self.parent.finishedExecution()
except Exception as exception:
try:
print(exception)
sys.settrace(None)
buffer = StringIO()
sys.stderr = buffer
if ( str(exception) != "Stop Execution" ):
traceback.print_exc()
self.parent.resultText.SetValue( self.parent.resultText.GetValue() + "\n" + buffer.getvalue() +
"\n---------------------------------------------------------------------\n" )
else:
self.parent.resultText.SetValue( self.parent.resultText.GetValue() + "\n" + buffer.getvalue() +
"\n------------------------- STOPPED ----------------------------------\n" )
self.parent.executeButton.Enable()
self.parent.finishedExecution()
sys.stderr = sys.__stderr__
sys.stdout = sys.__stdout__
except Exception as e:
print(e)
return
#
# Need to trace the calls when the python script is running, that way
# we can interrupt the processing.
#
def traceCalls(frame, event, arg):
global STOP
if ( "PythonRunner" not in frame.f_code.co_filename ): return
if ( STOP == True ):
raise Exception("Stop Execution")
return
class PythonExecutor(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: PythonExecutor.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.codeWindow = wx.SplitterWindow(self.panel_1, -1, style=wx.SP_3D|wx.SP_BORDER)
self.codeWindow.SetSashGravity(0.5)
self.codeWindow.SetSashPosition(500)
self.window_1_pane_2 = wx.Panel(self.codeWindow, -1)
self.window_1_pane_1 = wx.Panel(self.codeWindow, -1)
self.sizer_6_staticbox = wx.StaticBox(self.window_1_pane_2, -1, "")
self.sizer_5_staticbox = wx.StaticBox(self.window_1_pane_2, -1, "Result")
self.sizer_7_staticbox = wx.StaticBox(self.window_1_pane_1, -1, "Code Window")
self.codeText = stc.StyledTextCtrl(self.window_1_pane_1, -1)
self.codeText.SetMarginType(1, stc.STC_MARGIN_NUMBER)
self.codeText.SetMarginWidth(1, 30)
font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Courier New')
self.codeText.StyleSetFont(stc.STC_STYLE_DEFAULT, font)
self.static_line_1 = wx.StaticLine(self.window_1_pane_1, -1)
self.resultText = wx.TextCtrl(self.window_1_pane_2, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_WORDWRAP)
font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Swiss')
self.resultText.SetFont(font)
self.resultText.SetForegroundColour(wx.BLUE)
self.executeButton = wx.Button(self.window_1_pane_2, -1, "Execute")
# Menu Bar
self.frame_1_menubar = wx.MenuBar()
self.fileMenu = wx.Menu()
self.openMenu = wx.MenuItem(self.fileMenu, wx.NewId(), "Open", "", wx.ITEM_NORMAL)
self.fileMenu.Append(self.openMenu)
self.saveMenu = wx.MenuItem(self.fileMenu, wx.NewId(), "Save", "", wx.ITEM_NORMAL)
self.fileMenu.Append(self.saveMenu)
self.saveAsMenu = wx.MenuItem(self.fileMenu, wx.NewId(), "Save As...", "", wx.ITEM_NORMAL)
self.fileMenu.Append(self.saveAsMenu)
self.exitMenu = wx.MenuItem(self.fileMenu, wx.NewId(), "Exit", "", wx.ITEM_NORMAL)
self.fileMenu.Append(self.exitMenu)
self.frame_1_menubar.Append(self.fileMenu, "File")
self.SetMenuBar(self.frame_1_menubar)
# Menu Bar end
self.__set_properties()
self.__do_layout()
self.init_locals()
self.Bind(wx.EVT_BUTTON, self.executeButtonHandler, self.executeButton)
self.Bind(wx.EVT_MENU, self.openMenuHandler, self.openMenu)
self.Bind(wx.EVT_MENU, self.saveMenuHandler, self.saveMenu)
self.Bind(wx.EVT_MENU, self.saveAsMenuHandler, self.saveAsMenu)
self.Bind(wx.EVT_MENU, self.exitMenuHandler, self.exitMenu)
self.Bind(stc.EVT_STC_CHANGE, self.codeTextChanged, self.codeText)
# end wxGlade
def __set_properties(self):
# begin wxGlade: PythonExecutor.__set_properties
self.SetTitle("Python Runner")
# end wxGlade
def __do_layout(self):
# begin wxGlade: PythonExecutor.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5 = wx.StaticBoxSizer(self.sizer_5_staticbox, wx.VERTICAL)
sizer_6 = wx.StaticBoxSizer(self.sizer_6_staticbox, wx.HORIZONTAL)
sizer_7 = wx.StaticBoxSizer(self.sizer_7_staticbox, wx.VERTICAL)
sizer_7.Add(self.codeText, 1, wx.EXPAND, 0)
sizer_7.Add(self.static_line_1, 0, wx.EXPAND, 0)
self.window_1_pane_1.SetSizer(sizer_7)
sizer_5.Add(self.resultText, 1, wx.EXPAND, 0)
sizer_6.Add(self.executeButton, 0, 0, 0)
sizer_5.Add(sizer_6, 0, wx.ALIGN_RIGHT, 0)
self.window_1_pane_2.SetSizer(sizer_5)
self.codeWindow.SplitHorizontally(self.window_1_pane_1, self.window_1_pane_2)
sizer_2.Add(self.codeWindow, 1, wx.EXPAND, 0)
self.panel_1.SetSizer(sizer_2)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
self.Centre()
self.SetSize((900, 800))
# end wxGlade
def init_locals(self):
self.myparent = None
self.filename = None
self.dirname = "."
self.changeMade = False
self.executorThread = None
self.beginCode = "import mysql.connector \n" + \
"\n" + \
"#\n" + \
"# Server Parameters\n" + \
"#\n" + \
"SERVER = \"opm-int.nane.netapp.com\" \n" + \
"USER = \"root\" \n"+ \
"PASSWORD = \"ugauga\" \n"+ \
"\n"+ \
"#\n" + \
"# Connect to the server\n" + \
"#\n" + \
"dbConn = mysql.connector.connect( host=SERVER,\n"+ \
" user=USER, \n" + \
" passwd=PASSWORD, \n" + \
" autocommit=True, \n" + \
" raw=True, \n" + \
" db=\"\")\n" + \
"dbWorker = dbConn.cursor()\n"+ \
"\n"+ \
"#\n" + \
"# Now test out the connection\n" + \
"#\n" + \
"dbWorker.execute(\"show databases\")\n"+ \
"databases = dbWorker.fetchall()\n"+ \
"for db in databases:\n" + \
" print db[0]\n"+ \
"\n"
self.codeText.SetText(self.beginCode)
return
def setParent(self, parent):
self.myparent = parent
self.dbConnector = parent
return
###########################################################################
# Change handler
###########################################################################
def codeTextChanged(self, event):
self.changeMade = True;
return
###########################################################################
# Grab the code and run it.
###########################################################################
def executeButtonHandler(self, event): # wxGlade: PythonExecutor.<event_handler>
global STOP
if ( self.executorThread == None ):
self.resultText.SetValue("")
STOP = False
pythonCode = self.codeText.GetText()
self.executorThread = Executor(self, pythonCode)
self.executorThread.start()
self.executeButton.SetLabel("Stop")
else:
wx.MessageBox( "If your program is in a long wait, then the program will not exit until it comes out of that sleep" )
STOP = True
self.executeButton.Disable()
# Assume that it is running
self.executeButton.SetLabel("Execute")
sys.settrace(None)
self.executorThread = None
return
# Called when the execute has finished.
def finishedExecution(self):
global STOP
STOP = False
self.executeButton.SetLabel("Execute")
self.executorThread = None
self.executeButton.Enable()
return
###########################################################################
# Open a file
###########################################################################
def openMenuHandler(self, event): # wxGlade: PythonExecutor.<event_handler>
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.py", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
# Open the file, read the contents and set them into
# the text edit window
filehandle=open(os.path.join(self.dirname, self.filename),'r')
self.codeText.SetText(filehandle.read())
filehandle.close()
# Report on name of latest file read
self.SetTitle("Python Runner - "+self.filename)
# Later - could be enhanced to include a "changed" flag whenever
# the text is actually changed, could also be altered on "save" ...
dlg.Destroy()
return
###########################################################################
# Save a file
###########################################################################
def saveMenuHandler(self, event): # wxGlade: PythonExecutor.<event_handler>
# Save away the edited text
# Open the file, do an RU sure check for an overwrite!
if ( self.filename != None ):
# Grab the content to be saved
itcontains = self.codeText.GetText()
# Open the file for write, write, close
filehandle=open(os.path.join(self.dirname, self.filename),'w')
filehandle.write(itcontains)
filehandle.close()
else:
self.saveAsMenuHandler(event)
return
###########################################################################
# Prompt for a filename to save as
###########################################################################
def saveAsMenuHandler(self, event): # wxGlade: PythonExecutor.<event_handler>
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.py", \
wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
# Grab the content to be saved
itcontains = self.codeText.GetText()
# Open the file for write, write, close
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname, self.filename),'w')
filehandle.write(itcontains)
filehandle.close()
# Get rid of the dialog to keep things tidy
dlg.Destroy()
return
###########################################################################
# Exit handler
###########################################################################
def exitMenuHandler(self, event): # wxGlade: PythonExecutor.<event_handler>
if ( self.changeMade == True ):
retval = wx.MessageBox( "Changes were made, are you sure you want to exit?",
style=wx.OK|wx.CANCEL)
if ( retval != 4 ):
return
self.Hide()
return
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = PythonExecutor(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()