Changeset 26d6e045 in sasview for src/sas/sasgui/perspectives/calculator/pyconsole.py
- Timestamp:
- May 20, 2016 8:37:21 PM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 5dfbb24
- Parents:
- cd54205
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/calculator/pyconsole.py
rd85c194 r26d6e045 4 4 import sys 5 5 import os 6 7 import numpy as np 8 6 9 import wx 7 import wx.lib.dialogs 10 from wx.lib.dialogs import ScrolledMessageDialog 8 11 import wx.py.editor as editor 9 import wx.py.frame as frame10 import py_compile11 12 12 13 if sys.platform.count("win32") > 0: … … 18 19 PANEL_HEIGHT = 730 19 20 FONT_VARIANT = 1 20 ID_C OMPILE= wx.NewId()21 ID_CHECK_MODEL = wx.NewId() 21 22 ID_RUN = wx.NewId() 22 23 23 def c ompile_file(path):24 def check_model(path): 24 25 """ 25 C ompile a python file26 Check that the model on the path can run. 26 27 """ 28 # try running the model 29 from sasmodels.core import load_model, call_kernel 30 model = load_model(path) 31 32 q = np.array([0.01, 0.1]) 33 kernel = model.make_kernel([q]) 34 Iq = call_kernel(kernel, {}) 35 36 qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) 37 kernel = model.make_kernel([qx, qy]) 38 Iqxy = call_kernel(kernel, {}) 39 40 result = """ 41 Iq(%s) = %s 42 Iqxy(%s, %s) = %s 43 """%(q, Iq, qx, qy, Iqxy) 44 45 return result 46 47 def show_model_output(parent, fname): 48 # Make sure we have a python file; not sure why we care though... 49 if not (fname and os.path.exists(fname) and fname.endswith('.py')): 50 mssg = "\n This is not a python file." 51 wx.MessageBox(str(mssg), 'Error', style=wx.ICON_ERROR) 52 return False 53 27 54 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 55 result, errmsg = check_model(fname), None 56 except Exception: 57 import traceback 58 result, errmsg = None, traceback.format_exc(limit=2) 59 60 parts = ["Running model '%s'..." % os.path.basename(fname)] 61 if errmsg is not None: 62 parts.extend(["", "Error occurred:", errmsg, ""]) 63 title, icon = "Error", wx.ICON_ERROR 64 else: 65 parts.extend(["", "Success:", result, ""]) 66 title, icon = "Info", wx.ICON_INFORMATION 67 text = "\n".join(parts) 68 dlg = ScrolledMessageDialog(parent, text, title, size=((550, 250))) 69 fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) 70 dlg.GetChildren()[0].SetFont(fnt) 71 dlg.GetChildren()[0].SetInsertionPoint(0) 72 dlg.ShowModal() 73 dlg.Destroy() 74 return errmsg is None 34 75 35 76 class PyConsole(editor.EditorNotebookFrame): … … 65 106 self.Bind(wx.EVT_MENU, self.OnSaveFile, id=wx.ID_SAVE) 66 107 self.Bind(wx.EVT_MENU, self.OnSaveAsFile, id=wx.ID_SAVEAS) 67 self.Bind(wx.EVT_MENU, self.OnC ompile, id=ID_COMPILE)108 self.Bind(wx.EVT_MENU, self.OnCheckModel, id=ID_CHECK_MODEL) 68 109 self.Bind(wx.EVT_MENU, self.OnRun, id=ID_RUN) 69 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_C OMPILE)110 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_CHECK_MODEL) 70 111 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_RUN) 71 112 self.Bind(wx.EVT_CLOSE, self.on_close) … … 81 122 """ 82 123 self.compileMenu = wx.Menu() 83 self.compileMenu.Append(ID_C OMPILE, 'Compile',84 ' Compile the file')124 self.compileMenu.Append(ID_CHECK_MODEL, 'Check model', 125 'Loading and run the model') 85 126 self.compileMenu.AppendSeparator() 86 127 self.compileMenu.Append(ID_RUN, 'Run in Shell', … … 192 233 Run 193 234 """ 194 if self._check_changed():235 if not self._check_saved(): 195 236 return True 196 237 if self.buffer and self.buffer.doc.filepath: … … 207 248 icon = wx.ICON_ERROR 208 249 wx.MessageBox(str(mssg), title, style=icon) 209 return 0210 211 def OnC ompile(self, event):250 return False 251 252 def OnCheckModel(self, event): 212 253 """ 213 254 Compile 214 255 """ 215 if self._check_changed():256 if not self._check_saved(): 216 257 return True 217 run_out = self.OnRun(None) 218 if self._get_err_msg(run_out): 219 if self._manager != None and self.panel != None: 220 self._manager.set_edit_menu_helper(self.parent) 221 # Update custom model list in fitpage combobox 222 wx.CallAfter(self._manager.update_custom_combo) 223 224 def _check_changed(self): 258 fname = self.editor.getStatus()[0] 259 success = show_model_output(self, fname) 260 261 # Update custom model list in fitpage combobox 262 if success and self._manager != None and self.panel != None: 263 self._manager.set_edit_menu_helper(self.parent) 264 wx.CallAfter(self._manager.update_custom_combo) 265 266 def _check_saved(self): 225 267 """ 226 268 If content was changed, suggest to save it first … … 228 270 if self.bufferHasChanged() and self.buffer.doc.filepath: 229 271 cancel = self.bufferSuggestSave() 230 if cancel: 231 return cancel 232 233 def _get_err_msg(self, text=''): 234 """ 235 Get err_msg 236 """ 237 name = None 238 mssg = "\n This is not a python file." 239 title = 'Error' 240 icon = wx.ICON_ERROR 241 try: 242 fname = self.editor.getStatus()[0] 243 name = os.path.basename(fname) 244 if name.split('.')[-1] != 'py': 245 wx.MessageBox(str(mssg), title, style=icon) 246 return False 247 msg = compile_file(fname) 248 except: 249 msg = None 250 if name == None: 251 wx.MessageBox(str(mssg), title, style=icon) 252 return False 253 mssg = "Compiling '%s'...\n" % name 254 if msg != None: 255 mssg += "Error occurred:\n" 256 mssg += str(msg) + "\n\n" 257 if text: 258 mssg += "Run-Test results:\n" 259 mssg += str(text) 260 title = 'Warning' 261 icon = wx.ICON_WARNING 262 else: 263 mssg += "Successful.\n\n" 264 if text: 265 if text.count('Failed') or text.count('Error:') > 0: 266 mssg += "But Simple Test FAILED: Please check your code.\n" 267 mssg += "Run-Test results:\n" 268 mssg += str(text) 269 title = 'Info' 270 icon = wx.ICON_INFORMATION 271 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, mssg, title, 272 size=((550, 250))) 273 fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL) 274 dlg.GetChildren()[0].SetFont(fnt) 275 dlg.GetChildren()[0].SetInsertionPoint(0) 276 dlg.ShowModal() 277 dlg.Destroy() 272 return not cancel 278 273 return True 279 274 … … 286 281 event.Enable(True) 287 282 try: 288 if id == ID_C OMPILEor id == ID_RUN:283 if id == ID_CHECK_MODEL or id == ID_RUN: 289 284 menu_on = False 290 285 if self.buffer and self.buffer.doc.filepath:
Note: See TracChangeset
for help on using the changeset viewer.