[df7a7e3] | 1 | """ |
---|
| 2 | Class for making sure all category stuff is installed |
---|
| 3 | and works fine. |
---|
| 4 | |
---|
| 5 | Copyright (c) Institut Laue-Langevin 2012 |
---|
| 6 | |
---|
| 7 | @author kieranrcampbell@gmail.com |
---|
[dad7cef] | 8 | @modified by NIST/MD sasview team |
---|
[df7a7e3] | 9 | """ |
---|
| 10 | |
---|
| 11 | import os |
---|
[c35e6901] | 12 | import sys |
---|
[df7a7e3] | 13 | import shutil |
---|
| 14 | import cPickle as pickle |
---|
| 15 | from collections import defaultdict |
---|
| 16 | |
---|
[ea5fa58] | 17 | USER_FILE = 'serialized_cat.p' |
---|
[df7a7e3] | 18 | |
---|
| 19 | class CategoryInstaller: |
---|
| 20 | """ |
---|
| 21 | Class for making sure all category stuff is installed |
---|
| 22 | |
---|
| 23 | Note - class is entirely static! |
---|
| 24 | """ |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | def __init__(self): |
---|
| 28 | """ initialization """ |
---|
| 29 | |
---|
| 30 | @staticmethod |
---|
| 31 | def _get_installed_model_dir(): |
---|
| 32 | """ |
---|
| 33 | returns the dir where installed_models.txt should be |
---|
| 34 | """ |
---|
| 35 | import sans.dataloader.readers |
---|
| 36 | return sans.dataloader.readers.get_data_path() |
---|
| 37 | |
---|
| 38 | @staticmethod |
---|
| 39 | def _get_models_py_dir(): |
---|
| 40 | """ |
---|
| 41 | returns the dir where models.py should be |
---|
| 42 | """ |
---|
| 43 | import sans.perspectives.fitting.models |
---|
| 44 | return sans.perspectives.fitting.models.get_model_python_path() |
---|
[dad7cef] | 45 | |
---|
[df7a7e3] | 46 | @staticmethod |
---|
| 47 | def _get_default_cat_p_dir(): |
---|
| 48 | """ |
---|
| 49 | returns the dir where default_cat.p should be |
---|
| 50 | """ |
---|
[b221176] | 51 | import sans.sansview |
---|
[dad7cef] | 52 | cat_file = "default_categories.p" |
---|
[b221176] | 53 | dir, file_name = os.path.split(sans.sansview.__file__) |
---|
| 54 | cat_file_path = os.path.join(dir, cat_file) |
---|
| 55 | if os.path.isfile(cat_file_path): |
---|
| 56 | cat_file_dir = os.path.dirname(cat_file_path) |
---|
| 57 | return cat_file_dir |
---|
| 58 | |
---|
| 59 | raise RuntimeError('CategoryInstaller: Could not find folder containing default categories') |
---|
[df7a7e3] | 60 | |
---|
| 61 | @staticmethod |
---|
| 62 | def _get_home_dir(): |
---|
| 63 | """ |
---|
| 64 | returns the users sansview config dir |
---|
| 65 | """ |
---|
[ea5fa58] | 66 | return os.path.join(os.path.expanduser("~"), ".sasview") |
---|
[df7a7e3] | 67 | |
---|
| 68 | @staticmethod |
---|
| 69 | def _regenerate_model_dict(master_category_dict): |
---|
| 70 | """ |
---|
| 71 | regenerates self.by_model_dict which has each model name as the key |
---|
| 72 | and the list of categories belonging to that model |
---|
| 73 | along with the enabled mapping |
---|
| 74 | returns tuplet (by_model_dict, model_enabled_dict) |
---|
| 75 | """ |
---|
| 76 | by_model_dict = defaultdict(list) |
---|
| 77 | model_enabled_dict = defaultdict(bool) |
---|
| 78 | |
---|
| 79 | for category in master_category_dict: |
---|
| 80 | for (model, enabled) in master_category_dict[category]: |
---|
| 81 | by_model_dict[model].append(category) |
---|
| 82 | model_enabled_dict[model] = enabled |
---|
| 83 | |
---|
| 84 | return (by_model_dict, model_enabled_dict) |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | @staticmethod |
---|
| 88 | def _regenerate_master_dict(by_model_dict, model_enabled_dict): |
---|
| 89 | """ |
---|
| 90 | regenerates master_category_dict from by_model_dict |
---|
| 91 | and model_enabled_dict |
---|
| 92 | returns the master category dictionary |
---|
| 93 | """ |
---|
| 94 | master_category_dict = defaultdict(list) |
---|
| 95 | for model in by_model_dict: |
---|
| 96 | for category in by_model_dict[model]: |
---|
| 97 | master_category_dict[category].append(\ |
---|
| 98 | (model, model_enabled_dict[model])) |
---|
| 99 | |
---|
| 100 | return master_category_dict |
---|
| 101 | |
---|
| 102 | @staticmethod |
---|
| 103 | def get_user_file(): |
---|
| 104 | """ |
---|
[ea5fa58] | 105 | returns the user data file, eg .sasview/serialized_cat.p |
---|
[df7a7e3] | 106 | """ |
---|
| 107 | return os.path.join(CategoryInstaller._get_home_dir(), |
---|
[ea5fa58] | 108 | USER_FILE) |
---|
[df7a7e3] | 109 | |
---|
| 110 | @staticmethod |
---|
| 111 | def get_default_file(): |
---|
| 112 | """ |
---|
| 113 | returns the path of the default file |
---|
| 114 | e.g. blahblah/default_categories.p |
---|
| 115 | """ |
---|
| 116 | return os.path.join(\ |
---|
[ea5fa58] | 117 | CategoryInstaller._get_default_cat_p_dir(), "default_categories.p") |
---|
[df7a7e3] | 118 | |
---|
| 119 | @staticmethod |
---|
[657e52c] | 120 | def check_install(homedir = None, model_list=None): |
---|
[df7a7e3] | 121 | """ |
---|
| 122 | the main method of this class |
---|
| 123 | makes sure serialized_cat.p exists and if not |
---|
| 124 | compile it and install |
---|
| 125 | :param homefile: Override the default home directory |
---|
[657e52c] | 126 | :param model_list: List of model names except customized models |
---|
[df7a7e3] | 127 | """ |
---|
[657e52c] | 128 | #model_list = [] |
---|
| 129 | default_file = CategoryInstaller.get_default_file() |
---|
[df7a7e3] | 130 | serialized_file = None |
---|
[657e52c] | 131 | master_category_dict = defaultdict(list) |
---|
[df7a7e3] | 132 | if homedir == None: |
---|
| 133 | serialized_file = CategoryInstaller.get_user_file() |
---|
| 134 | else: |
---|
[ea5fa58] | 135 | serialized_file = os.path.join(homedir, USER_FILE) |
---|
[657e52c] | 136 | if os.path.isfile(serialized_file): |
---|
| 137 | cat_file = open(serialized_file, 'rb') |
---|
[df7a7e3] | 138 | else: |
---|
[657e52c] | 139 | cat_file = open(default_file, 'rb') |
---|
| 140 | master_category_dict = pickle.Unpickler(cat_file).load() |
---|
[df7a7e3] | 141 | (by_model_dict, model_enabled_dict) = \ |
---|
[657e52c] | 142 | CategoryInstaller._regenerate_model_dict(master_category_dict) |
---|
| 143 | cat_file.close() |
---|
| 144 | add_list = model_list |
---|
| 145 | del_name = False |
---|
| 146 | for cat in master_category_dict.keys(): |
---|
| 147 | for ind in range(len(master_category_dict[cat])): |
---|
| 148 | model_name, enabled = master_category_dict[cat][ind] |
---|
| 149 | if model_name not in model_list: |
---|
| 150 | del_name = True |
---|
| 151 | try: |
---|
| 152 | by_model_dict.pop(model_name) |
---|
| 153 | model_enabled_dict.pop(model_name) |
---|
| 154 | except: |
---|
| 155 | pass |
---|
| 156 | else: |
---|
| 157 | add_list.remove(model_name) |
---|
| 158 | if del_name or (len(add_list) > 0): |
---|
| 159 | for model in add_list: |
---|
| 160 | model_enabled_dict[model]= True |
---|
| 161 | by_model_dict[model].append('Uncategorized') |
---|
| 162 | |
---|
| 163 | master_category_dict = \ |
---|
| 164 | CategoryInstaller._regenerate_master_dict(by_model_dict, |
---|
| 165 | model_enabled_dict) |
---|
[df7a7e3] | 166 | |
---|
[657e52c] | 167 | pickle.dump( master_category_dict, |
---|
| 168 | open(serialized_file, 'wb') ) |
---|
[df7a7e3] | 169 | |
---|
[657e52c] | 170 | try: |
---|
| 171 | #It happens only in source environment |
---|
| 172 | shutil.copyfile(serialized_file, default_file) |
---|
| 173 | except: |
---|
| 174 | pass |
---|
[df7a7e3] | 175 | |
---|