Changeset 914ba0a in sasview for src/sas/sasgui/guiframe/CategoryInstaller.py
- Timestamp:
- May 2, 2017 3:58:01 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- d66dbcc
- Parents:
- 74d9780 (diff), 658dd57 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/guiframe/CategoryInstaller.py
refe730d r914ba0a 12 12 import sys 13 13 import json 14 from collections import defaultdict 14 import logging 15 from collections import defaultdict, OrderedDict 15 16 16 17 from sas.sasgui import get_user_dir 17 18 18 19 USER_FILE = 'categories.json' 20 21 logger = logging.getLogger(__name__) 19 22 20 23 class CategoryInstaller: … … 24 27 Note - class is entirely static! 25 28 """ 26 27 29 28 30 def __init__(self): … … 44 46 import sas.sasgui.perspectives.fitting.models 45 47 return sas.sasgui.perspectives.fitting.models.get_model_python_path() 46 48 47 49 @staticmethod 48 50 def _get_default_cat_file_dir(): … … 55 57 import sas.sasview 56 58 cat_file = "default_categories.json" 57 59 58 60 possible_cat_file_paths = [ 59 61 os.path.join(os.path.split(sas.sasview.__file__)[0], cat_file), # Source … … 65 67 if os.path.isfile(path): 66 68 return os.path.dirname(path) 67 69 68 70 raise RuntimeError('CategoryInstaller: Could not find folder containing default categories') 69 71 … … 78 80 by_model_dict = defaultdict(list) 79 81 model_enabled_dict = defaultdict(bool) 80 82 81 83 for category in master_category_dict: 82 84 for (model, enabled) in master_category_dict[category]: 83 85 by_model_dict[model].append(category) 84 86 model_enabled_dict[model] = enabled 85 87 86 88 return (by_model_dict, model_enabled_dict) 87 88 89 89 90 @staticmethod 90 91 def _regenerate_master_dict(by_model_dict, model_enabled_dict): 91 92 """ 92 regenerates master_category_dict from by_model_dict 93 regenerates master_category_dict from by_model_dict 93 94 and model_enabled_dict 94 95 returns the master category dictionary … … 99 100 master_category_dict[category].append(\ 100 101 (model, model_enabled_dict[model])) 101 102 return master_category_dict 102 return OrderedDict(sorted(master_category_dict.items(), key=lambda t: t[0])) 103 103 104 104 @staticmethod … … 111 111 @staticmethod 112 112 def get_default_file(): 113 """ 114 returns the path of the default file 115 e.g. blahblah/default_categories.json 116 """ 117 return os.path.join(\ 118 CategoryInstaller._get_default_cat_file_dir(), "default_categories.json") 119 113 logger.warning("CategoryInstaller.get_default_file is deprecated.") 114 120 115 @staticmethod 121 116 def check_install(homedir = None, model_list=None): … … 125 120 compile it and install 126 121 :param homefile: Override the default home directory 127 :param model_list: List of model names except customized models 122 :param model_list: List of model names except those in Plugin Models 123 which are user supplied. 128 124 """ 129 #model_list = [] 130 default_file = CategoryInstaller.get_default_file() 125 _model_dict = { model.name: model for model in model_list} 126 _model_list = _model_dict.keys() 127 131 128 serialized_file = None 132 master_category_dict = defaultdict(list) 133 if homedir == None: 129 if homedir is None: 134 130 serialized_file = CategoryInstaller.get_user_file() 135 131 else: 136 132 serialized_file = os.path.join(homedir, USER_FILE) 137 133 if os.path.isfile(serialized_file): 138 cat_file = open(serialized_file, 'rb') 134 with open(serialized_file, 'rb') as f: 135 master_category_dict = json.load(f) 139 136 else: 140 cat_file = open(default_file, 'rb') 141 master_category_dict = json.load(cat_file) 142 # master_category_dict = pickle.Unpickler(cat_file).load() 137 master_category_dict = defaultdict(list) 138 143 139 (by_model_dict, model_enabled_dict) = \ 144 140 CategoryInstaller._regenerate_model_dict(master_category_dict) 145 cat_file.close() 146 add_list = model_list 141 add_list = _model_list 147 142 del_name = False 148 143 for cat in master_category_dict.keys(): 149 144 for ind in range(len(master_category_dict[cat])): 150 145 model_name, enabled = master_category_dict[cat][ind] 151 if model_name not in model_list:152 del_name = True 146 if model_name not in _model_list: 147 del_name = True 153 148 try: 154 149 by_model_dict.pop(model_name) 155 150 model_enabled_dict.pop(model_name) 156 151 except: 157 pass152 logger.error("CategoryInstaller: %s", sys.exc_value) 158 153 else: 159 154 add_list.remove(model_name) … … 161 156 for model in add_list: 162 157 model_enabled_dict[model]= True 163 by_model_dict[model].append('Uncategorized') 164 158 if _model_dict[model].category is None or len(str(_model_dict[model].category.capitalize())) == 0: 159 by_model_dict[model].append('Uncategorized') 160 else: 161 category = _model_dict[model].category 162 toks = category.split(':') 163 category = toks[-1] 164 toks = category.split('-') 165 capitalized_words = [t.capitalize() for t in toks] 166 category = ' '.join(capitalized_words) 167 168 by_model_dict[model].append(category) 169 165 170 master_category_dict = \ 166 171 CategoryInstaller._regenerate_master_dict(by_model_dict, 167 172 model_enabled_dict) 168 169 json.dump( master_category_dict, 170 open(serialized_file, 'wb') ) 173 174 json.dump(master_category_dict, open(serialized_file, 'wb'))
Note: See TracChangeset
for help on using the changeset viewer.