Ignore:
Timestamp:
Apr 5, 2015 7:01:01 PM (9 years ago)
Author:
butler
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:
18ac46b
Parents:
7b1f4e3
Message:

use Regular text module to check for valid python name when creating a
custom model from composite model editor (sum|multi(p1,p2)). also
refactor checking code and made notes for future extraction of non GUI
code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/perspectives/calculator/model_editor.py

    r7b1f4e3 r439420f9  
    283283    def check_name(self, event=None): 
    284284        """ 
    285         Check name if exist already 
    286         """ 
     285        Check that proposed new model name is a valid Python module name  
     286        and that it does not already exist. If not show error message and 
     287        pink background in text box else call on_apply 
     288         
     289        :TODO this should be separated out from the GUI code.  For that we 
     290        need to pass it the name (or if we want to keep the default name  
     291        option also need to pass the self._operator attribute) We just need 
     292        the function to return an error code that the name is good or if  
     293        not why (not a valid name, name exists already).  The rest of the  
     294        error handling should be done in this module. so on_apply would then 
     295        start by checking the name and then either raise errors or do the 
     296        deed. 
     297        """ 
     298        #Get the function/file name 
    287299        mname = M_NAME 
    288300        self.on_change_name(None) 
    289         list_fnames = os.listdir(self.plugin_dir) 
    290         # fake existing regular model name list 
    291         m_list = [model + ".py" for model in self.model_list] 
    292         list_fnames.append(m_list) 
    293         # function/file name 
    294301        title = self.name_tcl.GetValue().lstrip().rstrip() 
    295302        if title == '': 
     
    303310        self.name = title 
    304311        t_fname = title + '.py' 
    305         if not self.overwrite_name: 
     312 
     313        #First check if the name is a valid Python name 
     314        if re.match('^[A-Za-z0-9_]*$',title): 
     315            self.good_name = True 
     316        else:  
     317            self.good_name = False 
     318            msg = ("%s is not a valid Python name. Only alphanumeric \n" \ 
     319                "and underscore allowed" % self.name) 
     320  
     321        #Now check if the name already exists 
     322        if not self.overwrite_name and self.good_name: 
     323            #Create list of existing model names for comparison 
     324            list_fnames = os.listdir(self.plugin_dir) 
     325            # fake existing regular model name list 
     326            m_list = [model + ".py" for model in self.model_list] 
     327            list_fnames.append(m_list) 
    306328            if t_fname in list_fnames and title != mname: 
    307                 self.name_tcl.SetBackgroundColour('pink') 
    308329                self.good_name = False 
    309                 info = 'Error' 
    310330                msg = "Name exists already." 
    311                 wx.MessageBox(msg, info) 
    312                 self._notes = msg 
    313                 color = 'red' 
    314                 self._msg_box.SetLabel(msg) 
    315                 self._msg_box.SetForegroundColour(color) 
    316                 return self.good_name 
     331 
     332        if self.good_name == False: 
     333            self.name_tcl.SetBackgroundColour('pink') 
     334            info = 'Error' 
     335            wx.MessageBox(msg, info) 
     336            self._notes = msg 
     337            color = 'red' 
     338            self._msg_box.SetLabel(msg) 
     339            self._msg_box.SetForegroundColour(color) 
     340            return self.good_name 
    317341        self.fname = os.path.join(self.plugin_dir, t_fname) 
    318342        s_title = title 
     
    435459        """ 
    436460 
    437         self._type = type 
    438461        name = '' 
    439462 
    440         if self._type == '*': 
     463        if type == '*': 
    441464            name = 'Multi' 
    442465            factor = 'BackGround' 
     
    448471 
    449472        self.factor = factor 
    450         self._operator = self._type 
     473        self._operator = type 
    451474        self.explanation = "  Custom Model = %s %s (model1 %s model2)\n" % \ 
    452475                    (self.factor, f_oper, self._operator) 
Note: See TracChangeset for help on using the changeset viewer.