source: sasview/calculatorview/src/sans/perspectives/calculator/pyconsole.py @ 490b281

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 490b281 was 490b281, checked in by Jae Cho <jhjcho@…>, 12 years ago

Add more comments on compile results

  • Property mode set to 100644
File size: 9.9 KB
Line 
1"""
2Console Module display Python console
3"""
4import sys
5import os
6import wx
7import wx.lib.dialogs
8import wx.py.editor as editor
9import wx.py.frame as frame
10import py_compile
11
12if sys.platform.count("win32")>0:
13    PANEL_WIDTH = 800
14    PANEL_HEIGHT = 700
15    FONT_VARIANT = 0
16else:
17    PANEL_WIDTH = 830
18    PANEL_HEIGHT = 730
19    FONT_VARIANT = 1
20ID_COMPILE = wx.NewId() 
21ID_RUN = wx.NewId() 
22
23def 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
35class 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        result = editor.openSingle(directory=filedir, 
160                            wildcard='Python Files (*.py)|*.py')
161        if result.path:
162            self.bufferCreate(result.path)
163        cancel = False
164        return cancel
165   
166    def bufferSaveAs(self):
167        """
168        Save buffer to a new filename: Bypassing editor bufferSaveAs
169        """
170        filedir = ''
171        if self.buffer and self.buffer.doc.filedir:
172            filedir = self.buffer.doc.filedir
173        result = editor.saveSingle(directory=filedir, 
174                                   filename='untitled.py',
175                                   wildcard='Python Files (*.py)|*.py')
176        if result.path:
177            self.buffer.saveAs(result.path)
178            cancel = False
179        else:
180            cancel = True
181        return cancel
182       
183    def OnRun(self, event):
184        """
185        Run
186        """
187        if self._check_changed():
188            return True
189        if self.buffer and self.buffer.doc.filepath:
190            self.editor.setFocus()
191            # Why we have to do this (Otherwise problems on Windows)?
192            forward_path = self.buffer.doc.filepath.replace('\\', '/')
193            self.shell.Execute("execfile('%s')"% forward_path) 
194            self.shell.Hide()
195            self.shell.Show(True)
196            return self.shell.GetText().split(">>>")[-2]
197        else:
198            mssg = "\n This is not a python file."
199            title = 'Error'
200            icon = wx.ICON_ERROR
201            wx.MessageBox(str(mssg), title, style=icon)
202            return 0
203       
204    def OnCompile(self, event):
205        """
206        Compile
207        """
208        if self._check_changed():
209            return True
210        run_out = self.OnRun(None)
211        if self._get_err_msg(run_out):
212            if self._manager != None and self.panel != None:
213                self._manager.set_edit_menu_helper(self.parent)
214                # Update custom model list in fitpage combobox
215                wx.CallAfter(self._manager.update_custom_combo)
216   
217    def _check_changed(self):   
218        """
219        If content was changed, suggest to save it first
220        """
221        if self.bufferHasChanged() and self.buffer.doc.filepath:
222            cancel = self.bufferSuggestSave()
223            if cancel:
224                return cancel
225             
226    def _get_err_msg(self, text=''):
227        """
228        Get err_msg
229        """
230        name = None
231        mssg = "\n This is not a python file."
232        title = 'Error'
233        icon = wx.ICON_ERROR
234        try:
235            fname = self.editor.getStatus()[0]
236            name = os.path.basename(fname)
237            if name.split('.')[-1] != 'py':
238                wx.MessageBox(str(mssg), title, style=icon)
239                return False
240            msg = compile_file(fname)
241        except:
242            msg = None
243        if name == None:
244            wx.MessageBox(str(mssg), title, style=icon)
245            return False
246        mssg = "Compiling '%s'...\n"% name
247        if msg != None:
248            mssg += "Error occurred:\n"
249            mssg += str(msg) + "\n\n"
250            if text:
251                mssg += "Run-Test results:\n"
252                mssg += str(text)
253                title = 'Warning'
254                icon = wx.ICON_WARNING
255        else:
256            mssg += "Successful.\n\n"
257            if text:
258                if text.count('Failed'):
259                    mssg += "But Simple Test FAILED: Please check your code.\n"
260                mssg += "Run-Test results:\n"
261                mssg += str(text)
262            title = 'Info'
263            icon = wx.ICON_INFORMATION
264        dlg = wx.lib.dialogs.ScrolledMessageDialog(self, mssg, title, 
265                                                   size = ((550, 250)))
266        fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
267        dlg.GetChildren()[0].SetFont(fnt)
268        dlg.GetChildren()[0].SetInsertionPoint(0)
269        dlg.ShowModal()
270        dlg.Destroy()
271        return True
272   
273    def OnUpdateCompileMenu(self, event):
274        """
275        Update Compile menu items based on current tap.
276        """
277        win = wx.Window.FindFocus()
278        id = event.GetId()
279        event.Enable(True)
280        try:
281            if id == ID_COMPILE or id == ID_RUN:
282                menu_on = False
283                if self.buffer and self.buffer.doc.filepath:
284                    menu_on = True
285                event.Enable(menu_on)
286        except AttributeError:
287            # This menu option is not supported in the current context.
288            event.Enable(False)
289           
290ABOUT =  "Welcome to Python %s! \n\n"% sys.version.split()[0]
291ABOUT += "This uses Py Shell/Editor in wx (developed by Patrick K. O'Brien).\n"
292ABOUT += "If this is your first time using Python, \n"
293ABOUT += "you should definitely check out the tutorial "
294ABOUT += "on the Internet at http://www.python.org/doc/tut/."
295 
296       
297if __name__ == "__main__":
298   
299    app  = wx.App()
300    dlg = PyConsole()
301    dlg.Show()
302    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.