Ignore:
Timestamp:
May 20, 2016 8:37:21 PM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
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
Message:

make sure compiler/test errors on custom models are reported to the user

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/calculator/pyconsole.py

    rd85c194 r26d6e045  
    44import sys 
    55import os 
     6 
     7import numpy as np 
     8 
    69import wx 
    7 import wx.lib.dialogs 
     10from wx.lib.dialogs import ScrolledMessageDialog 
    811import wx.py.editor as editor 
    9 import wx.py.frame as frame 
    10 import py_compile 
    1112 
    1213if sys.platform.count("win32") > 0: 
     
    1819    PANEL_HEIGHT = 730 
    1920    FONT_VARIANT = 1 
    20 ID_COMPILE = wx.NewId() 
     21ID_CHECK_MODEL = wx.NewId() 
    2122ID_RUN = wx.NewId() 
    2223 
    23 def compile_file(path): 
     24def check_model(path): 
    2425    """ 
    25     Compile a python file 
     26    Check that the model on the path can run. 
    2627    """ 
     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 
     47def 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 
    2754    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 
    3475 
    3576class PyConsole(editor.EditorNotebookFrame): 
     
    65106        self.Bind(wx.EVT_MENU, self.OnSaveFile, id=wx.ID_SAVE) 
    66107        self.Bind(wx.EVT_MENU, self.OnSaveAsFile, id=wx.ID_SAVEAS) 
    67         self.Bind(wx.EVT_MENU, self.OnCompile, id=ID_COMPILE) 
     108        self.Bind(wx.EVT_MENU, self.OnCheckModel, id=ID_CHECK_MODEL) 
    68109        self.Bind(wx.EVT_MENU, self.OnRun, id=ID_RUN) 
    69         self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_COMPILE) 
     110        self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_CHECK_MODEL) 
    70111        self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateCompileMenu, id=ID_RUN) 
    71112        self.Bind(wx.EVT_CLOSE, self.on_close) 
     
    81122        """ 
    82123        self.compileMenu = wx.Menu() 
    83         self.compileMenu.Append(ID_COMPILE, 'Compile', 
    84                  'Compile the file') 
     124        self.compileMenu.Append(ID_CHECK_MODEL, 'Check model', 
     125                 'Loading and run the model') 
    85126        self.compileMenu.AppendSeparator() 
    86127        self.compileMenu.Append(ID_RUN, 'Run in Shell', 
     
    192233        Run 
    193234        """ 
    194         if self._check_changed(): 
     235        if not self._check_saved(): 
    195236            return True 
    196237        if self.buffer and self.buffer.doc.filepath: 
     
    207248            icon = wx.ICON_ERROR 
    208249            wx.MessageBox(str(mssg), title, style=icon) 
    209             return 0 
    210  
    211     def OnCompile(self, event): 
     250            return False 
     251 
     252    def OnCheckModel(self, event): 
    212253        """ 
    213254        Compile 
    214255        """ 
    215         if self._check_changed(): 
     256        if not self._check_saved(): 
    216257            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): 
    225267        """ 
    226268        If content was changed, suggest to save it first 
     
    228270        if self.bufferHasChanged() and self.buffer.doc.filepath: 
    229271            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 
    278273        return True 
    279274 
     
    286281        event.Enable(True) 
    287282        try: 
    288             if id == ID_COMPILE or id == ID_RUN: 
     283            if id == ID_CHECK_MODEL or id == ID_RUN: 
    289284                menu_on = False 
    290285                if self.buffer and self.buffer.doc.filepath: 
Note: See TracChangeset for help on using the changeset viewer.