Changeset 212bfc2 in sasview for src/sas/sasgui/guiframe/CategoryInstaller.py
- Timestamp:
- Oct 7, 2016 5:11:40 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, 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/guiframe/CategoryInstaller.py
r50008e3 r212bfc2 11 11 import os 12 12 import sys 13 import shutil14 13 import json 15 from collections import defaultdict 14 import logging 15 from collections import defaultdict, OrderedDict 16 16 17 17 USER_FILE = 'categories.json' … … 23 23 Note - class is entirely static! 24 24 """ 25 26 25 27 26 def __init__(self): … … 43 42 import sas.sasgui.perspectives.fitting.models 44 43 return sas.sasgui.perspectives.fitting.models.get_model_python_path() 45 44 46 45 @staticmethod 47 46 def _get_default_cat_file_dir(): … … 54 53 import sas.sasview 55 54 cat_file = "default_categories.json" 56 55 57 56 possible_cat_file_paths = [ 58 57 os.path.join(os.path.split(sas.sasview.__file__)[0], cat_file), # Source … … 64 63 if os.path.isfile(path): 65 64 return os.path.dirname(path) 66 65 67 66 raise RuntimeError('CategoryInstaller: Could not find folder containing default categories') 68 67 … … 89 88 by_model_dict[model].append(category) 90 89 model_enabled_dict[model] = enabled 91 90 92 91 return (by_model_dict, model_enabled_dict) 93 94 92 95 93 @staticmethod … … 105 103 master_category_dict[category].append(\ 106 104 (model, model_enabled_dict[model])) 107 108 return master_category_dict 105 return OrderedDict(sorted(master_category_dict.items(), key=lambda t: t[0])) 109 106 110 107 @staticmethod … … 113 110 returns the user data file, eg .sasview/categories.json.json 114 111 """ 115 return os.path.join(CategoryInstaller._get_home_dir(), 116 USER_FILE) 112 return os.path.join(CategoryInstaller._get_home_dir(), USER_FILE) 117 113 118 114 @staticmethod 119 115 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 127 118 @staticmethod 128 119 def check_install(homedir = None, model_list=None): … … 134 125 :param model_list: List of model names except customized models 135 126 """ 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 138 130 serialized_file = None 139 master_category_dict = defaultdict(list)140 131 if homedir == None: 141 132 serialized_file = CategoryInstaller.get_user_file() … … 143 134 serialized_file = os.path.join(homedir, USER_FILE) 144 135 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) 146 138 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 150 141 (by_model_dict, model_enabled_dict) = \ 151 142 CategoryInstaller._regenerate_model_dict(master_category_dict) 152 cat_file.close() 153 add_list = model_list 143 add_list = _model_list 154 144 del_name = False 155 145 for cat in master_category_dict.keys(): 156 146 for ind in range(len(master_category_dict[cat])): 157 147 model_name, enabled = master_category_dict[cat][ind] 158 if model_name not in model_list:148 if model_name not in _model_list: 159 149 del_name = True 160 150 try: … … 162 152 model_enabled_dict.pop(model_name) 163 153 except: 164 pass154 logging.error("CategoryInstaller: %s", sys.exc_value) 165 155 else: 166 156 add_list.remove(model_name) … … 168 158 for model in add_list: 169 159 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 172 172 master_category_dict = \ 173 173 CategoryInstaller._regenerate_master_dict(by_model_dict, 174 174 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'))
Note: See TracChangeset
for help on using the changeset viewer.