1 | """ |
---|
2 | Console Module display Python console |
---|
3 | """ |
---|
4 | import sys |
---|
5 | import os |
---|
6 | import wx |
---|
7 | import wx.lib.dialogs |
---|
8 | import wx.py.editor as editor |
---|
9 | import wx.py.frame as frame |
---|
10 | import py_compile |
---|
11 | |
---|
12 | if sys.platform.count("win32")>0: |
---|
13 | PANEL_WIDTH = 800 |
---|
14 | PANEL_HEIGHT = 700 |
---|
15 | FONT_VARIANT = 0 |
---|
16 | else: |
---|
17 | PANEL_WIDTH = 830 |
---|
18 | PANEL_HEIGHT = 730 |
---|
19 | FONT_VARIANT = 1 |
---|
20 | ID_COMPILE = wx.NewId() |
---|
21 | ID_RUN = wx.NewId() |
---|
22 | |
---|
23 | def compile_file(path): |
---|
24 | """ |
---|
25 | Compile a python file |
---|
26 | """ |
---|
27 | try: |
---|
28 | import py_compile |
---|
29 | py_compile.compile(file=path, doraise=True) |
---|
30 | except: |
---|
31 | type, value, traceback = sys.exc_info() |
---|
32 | return value |
---|
33 | return None |
---|
34 | |
---|
35 | class PyConsole(editor.EditorNotebookFrame): |
---|
36 | ## Internal nickname for the window, used by the AUI manager |
---|
37 | window_name = "Custom Model Editor" |
---|
38 | ## Name to appear on the window title bar |
---|
39 | window_caption = "Custom Model Editor" |
---|
40 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
41 | CENTER_PANE = False |
---|
42 | def __init__(self, parent=None, manager=None, panel=None, |
---|
43 | title='Python Shell/Editor', filename=None, |
---|
44 | size=(PANEL_WIDTH, PANEL_HEIGHT)): |
---|
45 | self.config = None |
---|
46 | editor.EditorNotebookFrame.__init__(self, parent=parent, |
---|
47 | title=title, size=size, |
---|
48 | filename=filename) |
---|
49 | self.parent = parent |
---|
50 | self._manager = manager |
---|
51 | self.panel = panel |
---|
52 | self._add_menu() |
---|
53 | if filename != None: |
---|
54 | dataDir = os.path.dirname(filename) |
---|
55 | elif self.parent != None: |
---|
56 | dataDir = self.parent._default_save_location |
---|
57 | else: |
---|
58 | dataDir = None |
---|
59 | self.dataDir = dataDir |
---|
60 | self.Centre() |
---|
61 | |
---|
62 | self.Bind(wx.EVT_MENU, self.OnNewFile, id=wx.ID_NEW) |
---|
63 | self.Bind(wx.EVT_MENU, self.OnOpenFile, id=wx.ID_OPEN) |
---|
64 | self.Bind(wx.EVT_MENU, self.OnSaveFile, id=wx.ID_SAVE) |
---|
65 | self.Bind(wx.EVT_MENU, self.OnSaveAsFile, id=wx.ID_SAVEAS) |
---|
66 | self.Bind(wx.EVT_MENU, self.OnCompile, id=ID_COMPILE) |
---|
67 | self.Bind(wx.EVT_MENU, self.OnRun, id=ID_RUN) |
---|
68 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_COMPILE) |
---|
69 | self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_RUN) |
---|
70 | if not title.count('Python Shell'): |
---|
71 | # Delete menu item (open and new) if not python shell |
---|
72 | #self.fileMenu.Delete(wx.ID_NEW) |
---|
73 | self.fileMenu.Delete(wx.ID_OPEN) |
---|
74 | |
---|
75 | |
---|
76 | def _add_menu(self): |
---|
77 | """ |
---|
78 | Add menu |
---|
79 | """ |
---|
80 | self.compileMenu = wx.Menu() |
---|
81 | self.compileMenu.Append(ID_COMPILE, 'Compile', |
---|
82 | 'Compile the file') |
---|
83 | self.compileMenu.AppendSeparator() |
---|
84 | self.compileMenu.Append(ID_RUN, 'Run in Shell', |
---|
85 | 'Run the file in the Python Shell') |
---|
86 | self.MenuBar.Insert(3, self.compileMenu, '&Run') |
---|
87 | |
---|
88 | def OnHelp(self, event): |
---|
89 | """ |
---|
90 | Show a help dialog. |
---|
91 | """ |
---|
92 | import wx.lib.dialogs |
---|
93 | title = 'Help on key bindings' |
---|
94 | text = wx.py.shell.HELP_TEXT |
---|
95 | dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, |
---|
96 | size = ((700, 540))) |
---|
97 | fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) |
---|
98 | dlg.GetChildren()[0].SetFont(fnt) |
---|
99 | dlg.GetChildren()[0].SetInsertionPoint(0) |
---|
100 | dlg.ShowModal() |
---|
101 | dlg.Destroy() |
---|
102 | |
---|
103 | def set_manager(self, manager): |
---|
104 | """ |
---|
105 | Set the manager of this window |
---|
106 | """ |
---|
107 | self._manager = manager |
---|
108 | |
---|
109 | def OnAbout(self, event): |
---|
110 | """ |
---|
111 | On About |
---|
112 | """ |
---|
113 | message = ABOUT |
---|
114 | dial = wx.MessageDialog(self, message, 'About', |
---|
115 | wx.OK|wx.ICON_INFORMATION) |
---|
116 | dial.ShowModal() |
---|
117 | |
---|
118 | def OnNewFile(self, event): |
---|
119 | """ |
---|
120 | OnFileOpen |
---|
121 | """ |
---|
122 | self.OnFileNew(event) |
---|
123 | |
---|
124 | def OnOpenFile(self, event): |
---|
125 | """ |
---|
126 | OnFileOpen |
---|
127 | """ |
---|
128 | self.OnFileOpen(event) |
---|
129 | self.Show(False) |
---|
130 | self.Show(True) |
---|
131 | |
---|
132 | def OnSaveFile(self, event): |
---|
133 | """ |
---|
134 | OnFileSave overwrite |
---|
135 | """ |
---|
136 | self.OnFileSave(event) |
---|
137 | self.Show(False) |
---|
138 | self.Show(True) |
---|
139 | |
---|
140 | def OnSaveAsFile(self, event): |
---|
141 | """ |
---|
142 | OnFileSaveAs overwrite |
---|
143 | """ |
---|
144 | self.OnFileSaveAs(event) |
---|
145 | self.Show(False) |
---|
146 | self.Show(True) |
---|
147 | |
---|
148 | def bufferOpen(self): |
---|
149 | """ |
---|
150 | Open file in buffer, bypassing editor bufferOpen |
---|
151 | """ |
---|
152 | if self.bufferHasChanged(): |
---|
153 | cancel = self.bufferSuggestSave() |
---|
154 | if cancel: |
---|
155 | return cancel |
---|
156 | filedir = '' |
---|
157 | if self.buffer and self.buffer.doc.filedir: |
---|
158 | filedir = self.buffer.doc.filedir |
---|
159 | if not filedir: |
---|
160 | filedir = self.dataDir |
---|
161 | result = editor.openSingle(directory=filedir, |
---|
162 | wildcard='Python Files (*.py)|*.py') |
---|
163 | if result.path: |
---|
164 | self.bufferCreate(result.path) |
---|
165 | cancel = False |
---|
166 | return cancel |
---|
167 | |
---|
168 | def bufferSaveAs(self): |
---|
169 | """ |
---|
170 | Save buffer to a new filename: Bypassing editor bufferSaveAs |
---|
171 | """ |
---|
172 | filedir = '' |
---|
173 | if self.buffer and self.buffer.doc.filedir: |
---|
174 | filedir = self.buffer.doc.filedir |
---|
175 | if not filedir: |
---|
176 | filedir = self.dataDir |
---|
177 | result = editor.saveSingle(directory=filedir, |
---|
178 | filename='untitled.py', |
---|
179 | wildcard='Python Files (*.py)|*.py') |
---|
180 | if result.path: |
---|
181 | self.buffer.saveAs(result.path) |
---|
182 | cancel = False |
---|
183 | else: |
---|
184 | cancel = True |
---|
185 | return cancel |
---|
186 | |
---|
187 | def OnRun(self, event): |
---|
188 | """ |
---|
189 | Run |
---|
190 | """ |
---|
191 | if self._check_changed(): |
---|
192 | return True |
---|
193 | if self.buffer and self.buffer.doc.filepath: |
---|
194 | self.editor.setFocus() |
---|
195 | # Why we have to do this (Otherwise problems on Windows)? |
---|
196 | forward_path = self.buffer.doc.filepath.replace('\\', '/') |
---|
197 | self.shell.Execute("execfile('%s')"% forward_path) |
---|
198 | self.shell.Hide() |
---|
199 | self.shell.Show(True) |
---|
200 | return self.shell.GetText().split(">>>")[-2] |
---|
201 | else: |
---|
202 | mssg = "\n This is not a python file." |
---|
203 | title = 'Error' |
---|
204 | icon = wx.ICON_ERROR |
---|
205 | wx.MessageBox(str(mssg), title, style=icon) |
---|
206 | return 0 |
---|
207 | |
---|
208 | def OnCompile(self, event): |
---|
209 | """ |
---|
210 | Compile |
---|
211 | """ |
---|
212 | if self._check_changed(): |
---|
213 | return True |
---|
214 | run_out = self.OnRun(None) |
---|
215 | if self._get_err_msg(run_out): |
---|
216 | if self._manager != None and self.panel != None: |
---|
217 | self._manager.set_edit_menu_helper(self.parent) |
---|
218 | # Update custom model list in fitpage combobox |
---|
219 | wx.CallAfter(self._manager.update_custom_combo) |
---|
220 | |
---|
221 | def _check_changed(self): |
---|
222 | """ |
---|
223 | If content was changed, suggest to save it first |
---|
224 | """ |
---|
225 | if self.bufferHasChanged() and self.buffer.doc.filepath: |
---|
226 | cancel = self.bufferSuggestSave() |
---|
227 | if cancel: |
---|
228 | return cancel |
---|
229 | |
---|
230 | def _get_err_msg(self, text=''): |
---|
231 | """ |
---|
232 | Get err_msg |
---|
233 | """ |
---|
234 | name = None |
---|
235 | mssg = "\n This is not a python file." |
---|
236 | title = 'Error' |
---|
237 | icon = wx.ICON_ERROR |
---|
238 | try: |
---|
239 | fname = self.editor.getStatus()[0] |
---|
240 | name = os.path.basename(fname) |
---|
241 | if name.split('.')[-1] != 'py': |
---|
242 | wx.MessageBox(str(mssg), title, style=icon) |
---|
243 | return False |
---|
244 | msg = compile_file(fname) |
---|
245 | except: |
---|
246 | msg = None |
---|
247 | if name == None: |
---|
248 | wx.MessageBox(str(mssg), title, style=icon) |
---|
249 | return False |
---|
250 | mssg = "Compiling '%s'...\n"% name |
---|
251 | if msg != None: |
---|
252 | mssg += "Error occurred:\n" |
---|
253 | mssg += str(msg) + "\n\n" |
---|
254 | if text: |
---|
255 | mssg += "Run-Test results:\n" |
---|
256 | mssg += str(text) |
---|
257 | title = 'Warning' |
---|
258 | icon = wx.ICON_WARNING |
---|
259 | else: |
---|
260 | mssg += "Successful.\n\n" |
---|
261 | if text: |
---|
262 | if text.count('Failed') or text.count('Error:') > 0: |
---|
263 | mssg += "But Simple Test FAILED: Please check your code.\n" |
---|
264 | mssg += "Run-Test results:\n" |
---|
265 | mssg += str(text) |
---|
266 | title = 'Info' |
---|
267 | icon = wx.ICON_INFORMATION |
---|
268 | dlg = wx.lib.dialogs.ScrolledMessageDialog(self, mssg, title, |
---|
269 | size = ((550, 250))) |
---|
270 | fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) |
---|
271 | dlg.GetChildren()[0].SetFont(fnt) |
---|
272 | dlg.GetChildren()[0].SetInsertionPoint(0) |
---|
273 | dlg.ShowModal() |
---|
274 | dlg.Destroy() |
---|
275 | return True |
---|
276 | |
---|
277 | def OnUpdateCompileMenu(self, event): |
---|
278 | """ |
---|
279 | Update Compile menu items based on current tap. |
---|
280 | """ |
---|
281 | win = wx.Window.FindFocus() |
---|
282 | id = event.GetId() |
---|
283 | event.Enable(True) |
---|
284 | try: |
---|
285 | if id == ID_COMPILE or id == ID_RUN: |
---|
286 | menu_on = False |
---|
287 | if self.buffer and self.buffer.doc.filepath: |
---|
288 | menu_on = True |
---|
289 | event.Enable(menu_on) |
---|
290 | except AttributeError: |
---|
291 | # This menu option is not supported in the current context. |
---|
292 | event.Enable(False) |
---|
293 | |
---|
294 | ABOUT = "Welcome to Python %s! \n\n"% sys.version.split()[0] |
---|
295 | ABOUT += "This uses Py Shell/Editor in wx (developed by Patrick K. O'Brien).\n" |
---|
296 | ABOUT += "If this is your first time using Python, \n" |
---|
297 | ABOUT += "you should definitely check out the tutorial " |
---|
298 | ABOUT += "on the Internet at http://www.python.org/doc/tut/." |
---|
299 | |
---|
300 | |
---|
301 | if __name__ == "__main__": |
---|
302 | |
---|
303 | app = wx.App() |
---|
304 | dlg = PyConsole() |
---|
305 | dlg.Show() |
---|
306 | app.MainLoop() |
---|