Index: src/sas/perspectives/calculator/model_editor.py
===================================================================
--- src/sas/perspectives/calculator/model_editor.py	(revision 18ac46beea11120c792abb2dd1344b3fa14af1df)
+++ src/sas/perspectives/calculator/model_editor.py	(revision 2d50115b2948c926e7f4e07cad27f3638c5df359)
@@ -2,13 +2,13 @@
 This module provides three model editor classes: the composite model editor,
 the easy editor which provides a simple interface with tooltip help to enter
-the parameters of the model and their default value and a panel to input a 
-function of y (usually the intensity).  It also provides a drop down of 
+the parameters of the model and their default value and a panel to input a
+function of y (usually the intensity).  It also provides a drop down of
 standard available math functions.  Finally a full python editor panel for
 complete customizatin is provided.
 
-..TODO:: the writiong of the file and name checking (and maybe some other
+:TODO the writiong of the file and name checking (and maybe some other
 funtions?) should be moved to a computational module which could be called
 fropm a python script.  Basically one just needs to pass the name,
-description text and function text (or in the case of the composite editor 
+description text and function text (or in the case of the composite editor
 the names of the first and second model and the operator to be used).
 '''
@@ -72,17 +72,17 @@
     to choose two existing models (including pre-existing custom models which
     may themselves be composite models) as well as an operation on those models
-    (add or multiply) the resulting model will add a scale parameter for summed 
+    (add or multiply) the resulting model will add a scale parameter for summed
     models and a background parameter for a multiplied model.
-    
-    The user also gives a brief help for the model in a description box and 
+
+    The user also gives a brief help for the model in a description box and
     must provide a unique name which is verified as unique before the new
     model is saved.
-    
+
     This Dialog pops up for the user when they press 'Sum|Multi(p1,p2)' under
     'Edit Custom Model' under 'Fitting' menu.  This is currently called as
     a Modal Dialog.
-    
-    ..TODO:: the build in compiler currently balks at when it tries to import
-    a model whose name contains spaces or symbols (such as + ... underscore 
+
+    :TODO the build in compiler currently balks at when it tries to import
+    a model whose name contains spaces or symbols (such as + ... underscore
     should be fine).  Have fixed so the editor cannot save such a file name
     but if a file is dropped in the plugin directory from outside this class
@@ -121,5 +121,5 @@
         self.desc_sizer = None
         self.desc_tcl = None
-        self._selection_box= None
+        self._selection_box = None
         self.model1 = None
         self.model2 = None
@@ -147,13 +147,14 @@
 
         #set up label and input box with tool tip and event handling
-        self.name_txt = wx.StaticText(self, -1, 'Function Name : ')
-        self.name_tcl = wx.TextCtrl(self, -1, value = 'MySumFunction')
+        name_txt = wx.StaticText(self, -1, 'Function Name : ')
+        self.name_tcl = wx.TextCtrl(self, -1, value='MySumFunction')
         self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name)
         hint_name = "Unique Sum/Multiply Model Function Name."
         self.name_tcl.SetToolTipString(hint_name)
 
-        self.name_sizer.AddMany([(self.name_txt, 0, wx.LEFT | wx.TOP, 10),
-                                (self.name_tcl, -1,
-                                wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM, 10)])
+        self.name_sizer.AddMany([(name_txt, 0, wx.LEFT | wx.TOP, 10),
+                                 (self.name_tcl, -1,
+                                  wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM,
+                                  10)])
 
 
@@ -185,6 +186,6 @@
         #First set up main sizer for the selection
         selection_box_title = wx.StaticBox(self, -1, 'Select',
-                                       size=(PNL_WIDTH - 30, 70))
-        self._selection_box = wx.StaticBoxSizer(selection_box_title, 
+                                           size=(PNL_WIDTH - 30, 70))
+        self._selection_box = wx.StaticBoxSizer(selection_box_title,
                                                 wx.VERTICAL)
 
@@ -194,5 +195,5 @@
         select_help_box.Add(wx.StaticText(self, -1, model_string % (1, 1)),
                             0, 0)
-        select_help_box.Add((box_width - 25, 10),0,0)
+        select_help_box.Add((box_width - 25, 10), 0, 0)
         select_help_box.Add(wx.StaticText(self, -1, model_string % (2, 2)),
                             0, 0)
@@ -225,5 +226,5 @@
         selection_box_choose.Add(self.model2, 0, 0)
         # add some space between labels and selection
-        self._selection_box.Add((20,5), 0, 0)
+        self._selection_box.Add((20, 5), 0, 0)
         self._selection_box.Add(selection_box_choose, 0, 0)
 
@@ -231,5 +232,5 @@
         """
         Build GUI with calls to _layout_name, _layout Description
-        and _layout_model_selection which each build a their portion of the 
+        and _layout_model_selection which each build a their portion of the
         GUI.
         """
@@ -239,12 +240,12 @@
         # and adding to main sizer
         self._layout_name()
-        mainsizer.Add(self.name_sizer,0, wx.EXPAND)
+        mainsizer.Add(self.name_sizer, 0, wx.EXPAND)
         self._layout_description()
-        mainsizer.Add(self.desc_sizer,0, wx.EXPAND)
+        mainsizer.Add(self.desc_sizer, 0, wx.EXPAND)
 
         # Add an explanation of dialog (short help)
         self.explanationctr = wx.StaticText(self, -1, self.explanation)
         self.fill_explanation_helpstring(self._operator)
-        mainsizer.Add(self.explanationctr,0, wx.LEFT | wx.EXPAND, 15)
+        mainsizer.Add(self.explanationctr, 0, wx.LEFT | wx.EXPAND, 15)
 
         # Add the selection box stuff with border and labels built
@@ -264,8 +265,8 @@
         self.msg_sizer.Add(self._msg_box, 0, wx.LEFT, 0)
         mainsizer.Add(self.msg_sizer, 0,
-                 wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE | wx.BOTTOM, 10)
+                      wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE | wx.BOTTOM, 10)
 
         # Finally add the buttons (apply and close) on the bottom
-        # TODO: need help added here
+        # Eventually need to add help here
         self.ok_button = wx.Button(self, wx.ID_OK, 'Apply')
         self.ok_button.Bind(wx.EVT_BUTTON, self.check_name)
@@ -274,5 +275,5 @@
         sizer_button.AddMany([((20, 20), 1, 0),
                               (self.ok_button, 0, 0),
-                              (self.close_button, 0, wx.LEFT | wx.RIGHT, 10)])        
+                              (self.close_button, 0, wx.LEFT | wx.RIGHT, 10)])
         mainsizer.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10)
 
@@ -291,13 +292,13 @@
     def check_name(self, event=None):
         """
-        Check that proposed new model name is a valid Python module name 
+        Check that proposed new model name is a valid Python module name
         and that it does not already exist. If not show error message and
         pink background in text box else call on_apply
-        
-        ..TODO:: this should be separated out from the GUI code.  For that we
-        need to pass it the name (or if we want to keep the default name 
+
+        :TODO this should be separated out from the GUI code.  For that we
+        need to pass it the name (or if we want to keep the default name
         option also need to pass the self._operator attribute) We just need
-        the function to return an error code that the name is good or if 
-        not why (not a valid name, name exists already).  The rest of the 
+        the function to return an error code that the name is good or if
+        not why (not a valid name, name exists already).  The rest of the
         error handling should be done in this module. so on_apply would then
         start by checking the name and then either raise errors or do the
@@ -320,11 +321,11 @@
 
         #First check if the name is a valid Python name
-        if re.match('^[A-Za-z0-9_]*$',title):
+        if re.match('^[A-Za-z0-9_]*$', title):
             self.good_name = True
-        else: 
+        else:
             self.good_name = False
             msg = ("%s is not a valid Python name. Only alphanumeric \n" \
                    "and underscore allowed" % self.name)
- 
+
         #Now check if the name already exists
         if not self.overwrite_name and self.good_name:
@@ -361,8 +362,8 @@
         event.  Instead the apply button event goes to check_name which
         then calls this method if the name of the new file is acceptable.
-        
-        ..todo:: this should be bound to the apply button.  The first line
+
+        :TODO this should be bound to the apply button.  The first line
         should call the check_name method which itself should be in another
-        module separated from the the GUI modules. 
+        module separated from the the GUI modules.
         """
         self.name_tcl.SetBackgroundColour('white')
@@ -463,5 +464,5 @@
         self.fill_explanation_helpstring(text)
 
-    def fill_explanation_helpstring(self, type):
+    def fill_explanation_helpstring(self, operator):
         """
         Choose the equation to use depending on whether we now have
@@ -471,5 +472,5 @@
         name = ''
 
-        if type == '*':
+        if operator == '*':
             name = 'Multi'
             factor = 'BackGround'
@@ -481,5 +482,5 @@
 
         self.factor = factor
-        self._operator = type
+        self._operator = operator
         self.explanation = "  Custom Model = %s %s (model1 %s model2)\n" % \
                            (self.factor, f_oper, self._operator)
@@ -524,5 +525,5 @@
         try:
             out_f = open(path, 'w')
-        except :
+        except:
             raise
         lines = SUM_TEMPLATE.split('\n')
@@ -600,4 +601,5 @@
         kwds["style"] = wx.FULL_REPAINT_ON_RESIZE
         wx.ScrolledWindow.__init__(self, parent, *args, **kwds)
+        self.SetScrollbars(1,1,1,1)
         self.parent = parent
         self.base = base
@@ -813,12 +815,13 @@
         """
         event.Skip()
-        label = self.math_combo.GetLabel()
+        label = self.math_combo.GetValue()
         self.function_tcl.SetFocus()
         # Put the text at the cursor position
         pos = self.function_tcl.GetCurrentPos()
         self.function_tcl.InsertText(pos, label)
-        # Put the cursor at appropriate postion
+        # Put the cursor at appropriate position
         length = len(label)
-        if label[-1] == ')':
+        print length
+        if label[length-1] == ')':
             length -= 1
         f_pos = pos + length
@@ -876,13 +879,13 @@
         """
         Changes are saved in data object imported to edit.
-        
+
         checks firs for valid name, then if it already exists then checks
         that a function was entered and finally that if entered it contains at
-        least a return statement.  If all passes writes file then tries to 
+        least a return statement.  If all passes writes file then tries to
         compile.  If compile fails or import module fails or run method fails
         tries to remove any .py and pyc files that may have been created and
         sets error message.
-        
-        ..todo:: this code still could do with a careful going over to clean
+
+        :todo this code still could do with a careful going over to clean
         up and simplify. the non GUI methods such as this one should be removed
         to computational code of SasView. Most of those computational methods
@@ -896,5 +899,5 @@
         # Sort out the errors if occur
         # First check for valid python name then if the name already exists
-        if not re.match('^[A-Za-z0-9_]*$',name):
+        if not re.match('^[A-Za-z0-9_]*$', name):
             msg = "not a valid python name. Name must include only alpha \n"
             msg += "numeric or underline characters and no spaces"
@@ -914,4 +917,10 @@
                         msg.replace("  ", "\n")
                         msg += "\nCompiling Failed"
+                        try:
+                            # try to remove pyc file if exists
+                            _delete_file(self.fname)
+                            _delete_file(self.fname + "c")
+                        except:
+                            pass
                 else:
                     msg = "Error: The func(x) must 'return' a value at least.\n"
@@ -929,4 +938,9 @@
             except:
                 msg = 'new model fails to import in python'
+                try:
+                    # try to remove pyc file if exists
+                    _delete_file(self.fname + "c")
+                except:
+                    pass
         if self.base != None and not msg:
             try:
@@ -936,14 +950,13 @@
                 _, value, _ = sys.exc_info()
                 msg += "in %s:\n%s\n" % (name, value)
+                try:
+                    # try to remove pyc file if exists
+                    _delete_file(self.fname + "c")
+                except:
+                    pass
         # Prepare the messagebox
         if msg:
             info = 'Error'
             color = 'red'
-            try:
-                # try to remove pyc file if exists
-                _delete_file(self.fname)
-                _delete_file(self.fname + "c")
-            except:
-                pass
         else:
             msg = "Successful! "
@@ -1099,7 +1112,7 @@
                                  path=path, title=title)
         self.Show(True)
-        wx.EVT_CLOSE(self, self.OnClose)
-
-    def OnClose(self, event):
+        wx.EVT_CLOSE(self, self.on_close)
+
+    def on_close(self, event):
         """
         On close event
@@ -1514,9 +1527,9 @@
 if __name__ == "__main__":
 #    app = wx.PySimpleApp()
-    app = wx.App()
-    frame = TextDialog(id=1, model_list=["SphereModel", "CylinderModel"],
+    main_app = wx.App()
+    main_frame = TextDialog(id=1, model_list=["SphereModel", "CylinderModel"],
                        plugin_dir='../fitting/plugin_models')
-    frame.ShowModal()
-    app.MainLoop()
+    main_frame.ShowModal()
+    main_app.MainLoop()
 
 #if __name__ == "__main__":
