[ec6c520] | 1 | """ |
---|
| 2 | Console Module display Python console |
---|
| 3 | """ |
---|
| 4 | import sys |
---|
[5d1c1f4] | 5 | import os |
---|
[fb58234] | 6 | import wx |
---|
[5d1c1f4] | 7 | import wx.lib.dialogs |
---|
[fb58234] | 8 | import wx.py.editor as editor |
---|
[5d1c1f4] | 9 | import wx.py.frame as frame |
---|
| 10 | import py_compile |
---|
| 11 | |
---|
[ec6c520] | 12 | if sys.platform.count("win32")>0: |
---|
| 13 | PANEL_WIDTH = 800 |
---|
[7c8d3093] | 14 | PANEL_HEIGHT = 700 |
---|
[ec6c520] | 15 | FONT_VARIANT = 0 |
---|
| 16 | else: |
---|
| 17 | PANEL_WIDTH = 830 |
---|
[7c8d3093] | 18 | PANEL_HEIGHT = 730 |
---|
[ec6c520] | 19 | FONT_VARIANT = 1 |
---|
[5d1c1f4] | 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 | |
---|
[fb58234] | 35 | class PyConsole(editor.EditorNotebookFrame): |
---|
[ec6c520] | 36 | ## Internal nickname for the window, used by the AUI manager |
---|
[5d1c1f4] | 37 | window_name = "Custom Model Editor" |
---|
[ec6c520] | 38 | ## Name to appear on the window title bar |
---|
[5d1c1f4] | 39 | window_caption = "Custom Model Editor" |
---|
[ec6c520] | 40 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 41 | CENTER_PANE = False |
---|
[5d1c1f4] | 42 | def __init__(self, parent=None, manager=None, panel=None, |
---|
[fb58234] | 43 | title='Python Shell/Editor', filename=None, |
---|
[ec6c520] | 44 | size=(PANEL_WIDTH, PANEL_HEIGHT)): |
---|
[fb58234] | 45 | self.config = None |
---|
| 46 | editor.EditorNotebookFrame.__init__(self, parent=parent, |
---|
[ec6c520] | 47 | title=title, size=size, |
---|
[fb58234] | 48 | filename=filename) |
---|
[ec6c520] | 49 | self.parent = parent |
---|
| 50 | self._manager = manager |
---|
[5d1c1f4] | 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 |
---|
[ec6c520] | 60 | self.Centre() |
---|
[7c8d3093] | 61 | |
---|
| 62 | self.Bind(wx.EVT_MENU, self.OnNewFile, id=wx.ID_NEW) |
---|
[4c5448c] | 63 | self.Bind(wx.EVT_MENU, self.OnOpenFile, id=wx.ID_OPEN) |
---|
[5d1c1f4] | 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) |
---|
[7c8d3093] | 70 | if not title.count('Python Shell'): |
---|
| 71 | # Delete menu item (open and new) if not python shell |
---|
[86be650] | 72 | #self.fileMenu.Delete(wx.ID_NEW) |
---|
[7c8d3093] | 73 | self.fileMenu.Delete(wx.ID_OPEN) |
---|
[5d1c1f4] | 74 | |
---|
[7c8d3093] | 75 | |
---|
[5d1c1f4] | 76 | def _add_menu(self): |
---|
[861f8317] | 77 | """ |
---|
[5d1c1f4] | 78 | Add menu |
---|
[861f8317] | 79 | """ |
---|
[5d1c1f4] | 80 | self.compileMenu = wx.Menu() |
---|
| 81 | self.compileMenu.Append(ID_COMPILE, 'Compile', |
---|
| 82 | 'Compile the file') |
---|
| 83 | self.compileMenu.AppendSeparator() |
---|
[7c8d3093] | 84 | self.compileMenu.Append(ID_RUN, 'Run in Shell', |
---|
[5d1c1f4] | 85 | 'Run the file in the Python Shell') |
---|
[7c8d3093] | 86 | self.MenuBar.Insert(3, self.compileMenu, '&Run') |
---|
[5d1c1f4] | 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 | |
---|
[ec6c520] | 103 | def set_manager(self, manager): |
---|
| 104 | """ |
---|
| 105 | Set the manager of this window |
---|
| 106 | """ |
---|
| 107 | self._manager = manager |
---|
[861f8317] | 108 | |
---|
| 109 | def OnAbout(self, event): |
---|
| 110 | """ |
---|
| 111 | On About |
---|
| 112 | """ |
---|
| 113 | message = ABOUT |
---|
[b1bda35] | 114 | dial = wx.MessageDialog(self, message, 'About', |
---|
[861f8317] | 115 | wx.OK|wx.ICON_INFORMATION) |
---|
| 116 | dial.ShowModal() |
---|
[7c8d3093] | 117 | |
---|
| 118 | def OnNewFile(self, event): |
---|
| 119 | """ |
---|
| 120 | OnFileOpen |
---|
| 121 | """ |
---|
| 122 | self.OnFileNew(event) |
---|
[4c5448c] | 123 | |
---|
| 124 | def OnOpenFile(self, event): |
---|
| 125 | """ |
---|
| 126 | OnFileOpen |
---|
| 127 | """ |
---|
| 128 | self.OnFileOpen(event) |
---|
| 129 | self.Show(False) |
---|
| 130 | self.Show(True) |
---|
[5d1c1f4] | 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 | |
---|
[7c8d3093] | 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 |
---|
[86be650] | 159 | if not filedir: |
---|
| 160 | filedir = self.dataDir |
---|
[7c8d3093] | 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 | |
---|
[5d1c1f4] | 168 | def bufferSaveAs(self): |
---|
| 169 | """ |
---|
[7c8d3093] | 170 | Save buffer to a new filename: Bypassing editor bufferSaveAs |
---|
[5d1c1f4] | 171 | """ |
---|
| 172 | filedir = '' |
---|
| 173 | if self.buffer and self.buffer.doc.filedir: |
---|
| 174 | filedir = self.buffer.doc.filedir |
---|
[86be650] | 175 | if not filedir: |
---|
| 176 | filedir = self.dataDir |
---|
[7c8d3093] | 177 | result = editor.saveSingle(directory=filedir, |
---|
| 178 | filename='untitled.py', |
---|
| 179 | wildcard='Python Files (*.py)|*.py') |
---|
[5d1c1f4] | 180 | if result.path: |
---|
[f706e09c] | 181 | self.buffer.confirmed = True |
---|
[5d1c1f4] | 182 | self.buffer.saveAs(result.path) |
---|
| 183 | cancel = False |
---|
| 184 | else: |
---|
| 185 | cancel = True |
---|
| 186 | return cancel |
---|
| 187 | |
---|
| 188 | def OnRun(self, event): |
---|
| 189 | """ |
---|
| 190 | Run |
---|
| 191 | """ |
---|
| 192 | if self._check_changed(): |
---|
| 193 | return True |
---|
| 194 | if self.buffer and self.buffer.doc.filepath: |
---|
| 195 | self.editor.setFocus() |
---|
| 196 | # Why we have to do this (Otherwise problems on Windows)? |
---|
| 197 | forward_path = self.buffer.doc.filepath.replace('\\', '/') |
---|
[7c8d3093] | 198 | self.shell.Execute("execfile('%s')"% forward_path) |
---|
[5d1c1f4] | 199 | self.shell.Hide() |
---|
| 200 | self.shell.Show(True) |
---|
[7c8d3093] | 201 | return self.shell.GetText().split(">>>")[-2] |
---|
[5d1c1f4] | 202 | else: |
---|
| 203 | mssg = "\n This is not a python file." |
---|
| 204 | title = 'Error' |
---|
| 205 | icon = wx.ICON_ERROR |
---|
| 206 | wx.MessageBox(str(mssg), title, style=icon) |
---|
[7c8d3093] | 207 | return 0 |
---|
[5d1c1f4] | 208 | |
---|
| 209 | def OnCompile(self, event): |
---|
| 210 | """ |
---|
| 211 | Compile |
---|
| 212 | """ |
---|
| 213 | if self._check_changed(): |
---|
| 214 | return True |
---|
[7c8d3093] | 215 | run_out = self.OnRun(None) |
---|
| 216 | if self._get_err_msg(run_out): |
---|
[5d1c1f4] | 217 | if self._manager != None and self.panel != None: |
---|
[7c8d3093] | 218 | self._manager.set_edit_menu_helper(self.parent) |
---|
[491d0fc] | 219 | # Update custom model list in fitpage combobox |
---|
| 220 | wx.CallAfter(self._manager.update_custom_combo) |
---|
[5d1c1f4] | 221 | |
---|
| 222 | def _check_changed(self): |
---|
| 223 | """ |
---|
| 224 | If content was changed, suggest to save it first |
---|
| 225 | """ |
---|
| 226 | if self.bufferHasChanged() and self.buffer.doc.filepath: |
---|
| 227 | cancel = self.bufferSuggestSave() |
---|
| 228 | if cancel: |
---|
| 229 | return cancel |
---|
[fb58234] | 230 | |
---|
[7c8d3093] | 231 | def _get_err_msg(self, text=''): |
---|
[5d1c1f4] | 232 | """ |
---|
| 233 | Get err_msg |
---|
| 234 | """ |
---|
| 235 | name = None |
---|
| 236 | mssg = "\n This is not a python file." |
---|
| 237 | title = 'Error' |
---|
| 238 | icon = wx.ICON_ERROR |
---|
| 239 | try: |
---|
| 240 | fname = self.editor.getStatus()[0] |
---|
| 241 | name = os.path.basename(fname) |
---|
| 242 | if name.split('.')[-1] != 'py': |
---|
| 243 | wx.MessageBox(str(mssg), title, style=icon) |
---|
| 244 | return False |
---|
| 245 | msg = compile_file(fname) |
---|
| 246 | except: |
---|
| 247 | msg = None |
---|
| 248 | if name == None: |
---|
| 249 | wx.MessageBox(str(mssg), title, style=icon) |
---|
| 250 | return False |
---|
| 251 | mssg = "Compiling '%s'...\n"% name |
---|
| 252 | if msg != None: |
---|
| 253 | mssg += "Error occurred:\n" |
---|
[7c8d3093] | 254 | mssg += str(msg) + "\n\n" |
---|
| 255 | if text: |
---|
| 256 | mssg += "Run-Test results:\n" |
---|
| 257 | mssg += str(text) |
---|
| 258 | title = 'Warning' |
---|
| 259 | icon = wx.ICON_WARNING |
---|
[5d1c1f4] | 260 | else: |
---|
[7c8d3093] | 261 | mssg += "Successful.\n\n" |
---|
| 262 | if text: |
---|
[9e5ec99] | 263 | if text.count('Failed') or text.count('Error:') > 0: |
---|
[490b281] | 264 | mssg += "But Simple Test FAILED: Please check your code.\n" |
---|
[7c8d3093] | 265 | mssg += "Run-Test results:\n" |
---|
| 266 | mssg += str(text) |
---|
[5d1c1f4] | 267 | title = 'Info' |
---|
| 268 | icon = wx.ICON_INFORMATION |
---|
| 269 | dlg = wx.lib.dialogs.ScrolledMessageDialog(self, mssg, title, |
---|
| 270 | size = ((550, 250))) |
---|
| 271 | fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) |
---|
| 272 | dlg.GetChildren()[0].SetFont(fnt) |
---|
| 273 | dlg.GetChildren()[0].SetInsertionPoint(0) |
---|
| 274 | dlg.ShowModal() |
---|
| 275 | dlg.Destroy() |
---|
| 276 | return True |
---|
| 277 | |
---|
| 278 | def OnUpdateCompileMenu(self, event): |
---|
| 279 | """ |
---|
| 280 | Update Compile menu items based on current tap. |
---|
| 281 | """ |
---|
| 282 | win = wx.Window.FindFocus() |
---|
| 283 | id = event.GetId() |
---|
| 284 | event.Enable(True) |
---|
| 285 | try: |
---|
| 286 | if id == ID_COMPILE or id == ID_RUN: |
---|
| 287 | menu_on = False |
---|
| 288 | if self.buffer and self.buffer.doc.filepath: |
---|
| 289 | menu_on = True |
---|
| 290 | event.Enable(menu_on) |
---|
| 291 | except AttributeError: |
---|
| 292 | # This menu option is not supported in the current context. |
---|
| 293 | event.Enable(False) |
---|
| 294 | |
---|
[b1bda35] | 295 | ABOUT = "Welcome to Python %s! \n\n"% sys.version.split()[0] |
---|
[5d1c1f4] | 296 | ABOUT += "This uses Py Shell/Editor in wx (developed by Patrick K. O'Brien).\n" |
---|
[861f8317] | 297 | ABOUT += "If this is your first time using Python, \n" |
---|
| 298 | ABOUT += "you should definitely check out the tutorial " |
---|
| 299 | ABOUT += "on the Internet at http://www.python.org/doc/tut/." |
---|
[ec6c520] | 300 | |
---|
[861f8317] | 301 | |
---|
[ec6c520] | 302 | if __name__ == "__main__": |
---|
| 303 | |
---|
| 304 | app = wx.App() |
---|
| 305 | dlg = PyConsole() |
---|
| 306 | dlg.Show() |
---|
| 307 | app.MainLoop() |
---|