Changeset 212bfc2 in sasview for src/sas/sasgui/guiframe


Ignore:
Timestamp:
Oct 7, 2016 3:11:40 PM (7 years ago)
Author:
mathieu
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, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
b61bd57
Parents:
60df6c1
Message:

Pull categories from models. Get rid of default categories. Fixes #535

Location:
src/sas/sasgui/guiframe
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/guiframe/CategoryInstaller.py

    r50008e3 r212bfc2  
    1111import os 
    1212import sys 
    13 import shutil 
    1413import json 
    15 from collections import defaultdict 
     14import logging 
     15from collections import defaultdict, OrderedDict 
    1616 
    1717USER_FILE = 'categories.json' 
     
    2323    Note - class is entirely static! 
    2424    """ 
    25  
    2625 
    2726    def __init__(self): 
     
    4342        import sas.sasgui.perspectives.fitting.models 
    4443        return sas.sasgui.perspectives.fitting.models.get_model_python_path() 
    45      
     44 
    4645    @staticmethod 
    4746    def _get_default_cat_file_dir(): 
     
    5453        import sas.sasview 
    5554        cat_file = "default_categories.json" 
    56          
     55 
    5756        possible_cat_file_paths = [ 
    5857            os.path.join(os.path.split(sas.sasview.__file__)[0], cat_file),           # Source 
     
    6463            if os.path.isfile(path): 
    6564                return os.path.dirname(path) 
    66              
     65 
    6766        raise RuntimeError('CategoryInstaller: Could not find folder containing default categories') 
    6867 
     
    8988                by_model_dict[model].append(category) 
    9089                model_enabled_dict[model] = enabled 
    91      
     90 
    9291        return (by_model_dict, model_enabled_dict) 
    93  
    9492 
    9593    @staticmethod 
     
    105103                master_category_dict[category].append(\ 
    106104                    (model, model_enabled_dict[model])) 
    107          
    108         return master_category_dict 
     105        return OrderedDict(sorted(master_category_dict.items(), key=lambda t: t[0])) 
    109106 
    110107    @staticmethod 
     
    113110        returns the user data file, eg .sasview/categories.json.json 
    114111        """ 
    115         return os.path.join(CategoryInstaller._get_home_dir(), 
    116                             USER_FILE) 
     112        return os.path.join(CategoryInstaller._get_home_dir(), USER_FILE) 
    117113 
    118114    @staticmethod 
    119115    def get_default_file(): 
    120         """ 
    121         returns the path of the default file 
    122         e.g. blahblah/default_categories.json 
    123         """ 
    124         return os.path.join(\ 
    125             CategoryInstaller._get_default_cat_file_dir(), "default_categories.json") 
    126          
     116        logging.warning("CategoryInstaller.get_default_file is deprecated.") 
     117 
    127118    @staticmethod 
    128119    def check_install(homedir = None, model_list=None): 
     
    134125        :param model_list: List of model names except customized models 
    135126        """ 
    136         #model_list = [] 
    137         default_file = CategoryInstaller.get_default_file() 
     127        _model_dict = { model.name: model for model in model_list} 
     128        _model_list = _model_dict.keys() 
     129 
    138130        serialized_file = None 
    139         master_category_dict = defaultdict(list) 
    140131        if homedir == None: 
    141132            serialized_file = CategoryInstaller.get_user_file() 
     
    143134            serialized_file = os.path.join(homedir, USER_FILE) 
    144135        if os.path.isfile(serialized_file): 
    145             cat_file = open(serialized_file, 'rb') 
     136            with open(serialized_file, 'rb') as f: 
     137                master_category_dict = json.load(f) 
    146138        else: 
    147             cat_file = open(default_file, 'rb') 
    148         master_category_dict = json.load(cat_file) 
    149 #        master_category_dict = pickle.Unpickler(cat_file).load() 
     139            master_category_dict = defaultdict(list) 
     140 
    150141        (by_model_dict, model_enabled_dict) = \ 
    151142                CategoryInstaller._regenerate_model_dict(master_category_dict) 
    152         cat_file.close() 
    153         add_list = model_list 
     143        add_list = _model_list 
    154144        del_name = False 
    155145        for cat in master_category_dict.keys(): 
    156146            for ind in range(len(master_category_dict[cat])): 
    157147                model_name, enabled = master_category_dict[cat][ind] 
    158                 if model_name not in model_list: 
     148                if model_name not in _model_list: 
    159149                    del_name = True  
    160150                    try: 
     
    162152                        model_enabled_dict.pop(model_name) 
    163153                    except: 
    164                         pass 
     154                        logging.error("CategoryInstaller: %s", sys.exc_value) 
    165155                else: 
    166156                    add_list.remove(model_name) 
     
    168158            for model in add_list: 
    169159                model_enabled_dict[model]= True 
    170                 by_model_dict[model].append('Uncategorized') 
    171      
     160                if _model_dict[model].category is None or len(str(_model_dict[model].category.capitalize())) == 0: 
     161                    by_model_dict[model].append('Uncategorized') 
     162                else: 
     163                    category = _model_dict[model].category 
     164                    toks = category.split(':') 
     165                    category = toks[-1] 
     166                    toks = category.split('-') 
     167                    capitalized_words = [t.capitalize() for t in toks] 
     168                    category = ' '.join(capitalized_words) 
     169 
     170                    by_model_dict[model].append(category) 
     171 
    172172            master_category_dict = \ 
    173173                CategoryInstaller._regenerate_master_dict(by_model_dict, 
    174174                                                          model_enabled_dict) 
    175              
    176             json.dump( master_category_dict, 
    177                          open(serialized_file, 'wb') ) 
     175 
     176            json.dump(master_category_dict, open(serialized_file, 'wb')) 
  • src/sas/sasgui/guiframe/CategoryManager.py

    r80b1df3 r212bfc2  
    1313import sys 
    1414import os 
     15import logging 
    1516from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin 
    1617from collections import defaultdict 
     
    366367        """ 
    367368        try: 
    368             file = CategoryInstaller.get_user_file() 
    369             if os.path.isfile(file): 
    370                 cat_file = open(file, 'rb') 
    371 #               self.master_category_dict = pickle.load(cat_file) 
    372                 self.master_category_dict = json.load(cat_file) 
    373             else: 
    374                 cat_file = open(CategoryInstaller.get_default_file(), 'rb') 
    375 #                       self.master_category_dict = pickle.load(cat_file) 
    376                 self.master_category_dict = json.load(cat_file) 
    377             cat_file.close() 
     369            cat_file = CategoryInstaller.get_user_file() 
     370            self.master_category_dict = {} 
     371            if os.path.isfile(cat_file): 
     372                with open(cat_file, 'rb') as f: 
     373                    self.master_category_dict = json.load(f) 
    378374        except IOError: 
    379             print 'Problem reading in category file. Please review' 
    380  
     375            logging.error('Problem reading in category file.') 
    381376 
    382377        self._regenerate_model_dict() 
  • src/sas/sasgui/guiframe/customdir.py

    rd85c194 r212bfc2  
    11# Setup and find Custom config dir 
    2 import sys 
    32import os.path 
    43import shutil 
    5 from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller 
    64 
    75CONF_DIR = 'config'  
     
    1210    Find and return user/.sasview dir 
    1311    """ 
    14     dir = os.path.join(os.path.expanduser("~"),  
    15                        ("." + APPLICATION_NAME)) 
    16     return dir 
     12    return os.path.join(os.path.expanduser("~"), ("." + APPLICATION_NAME)) 
    1713 
    1814def _find_customconf_dir(): 
     
    2218    """ 
    2319    u_dir = _find_usersasview_dir() 
    24     dir = os.path.join(u_dir, CONF_DIR) 
    25      
    26     return dir 
     20    return os.path.join(u_dir, CONF_DIR) 
    2721 
    2822def _setup_conf_dir(path): 
     
    3024    Setup the custom config dir and cat file 
    3125    """ 
    32     dir = _find_customconf_dir() 
     26    conf_dir = _find_customconf_dir() 
    3327    # If the plugin directory doesn't exist, create it 
    34     if not os.path.isdir(dir): 
    35         os.makedirs(dir) 
    36     file = os.path.join(dir, "custom_config.py") 
    37     cat_file = CategoryInstaller.get_user_file() 
    38     # If the user category file doesn't exist copy the default to 
    39     # the user directory 
    40     if not os.path.isfile(cat_file): 
    41         try: 
    42             default_cat_file = CategoryInstaller.get_default_file() 
    43             if os.path.isfile(default_cat_file): 
    44                 shutil.copyfile(default_cat_file, cat_file) 
    45             else: 
    46                 print "Unable to find/copy default cat file" 
    47         except: 
    48             print "Unable to copy default cat file to the user dir." 
     28    if not os.path.isdir(conf_dir): 
     29        os.makedirs(conf_dir) 
     30    config_file = os.path.join(conf_dir, "custom_config.py") 
    4931 
    5032    # Place example user models as needed 
    5133    try: 
    52         if not os.path.isfile(file): 
    53          shutil.copyfile(os.path.join(path, "custom_config.py"), file) 
     34        if not os.path.isfile(config_file): 
     35            shutil.copyfile(os.path.join(path, "custom_config.py"), config_file) 
    5436    except: 
    5537        # Check for data path next to exe/zip file. 
     
    6345            temp_path = os.path.join(f_dir, "custom_config.py") 
    6446            if os.path.isfile(temp_path): 
    65                 shutil.copyfile(temp_path, file) 
     47                shutil.copyfile(temp_path, config_file) 
    6648                is_dir = True 
    6749                break 
    6850        if not is_dir: 
    6951            raise 
    70          
    71     return dir 
    72    
    73          
     52    return conf_dir 
     53 
     54 
    7455class SetupCustom(object): 
    7556    """ 
     
    8162    def setup_dir(self, path): 
    8263        return _setup_conf_dir(path) 
    83      
    84  
    85      
    86      
    87    
Note: See TracChangeset for help on using the changeset viewer.