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 |
---|
8 | @modified by NIST/MD sasview team |
---|
9 | """ |
---|
10 | |
---|
11 | import os |
---|
12 | import sys |
---|
13 | import json |
---|
14 | import logging |
---|
15 | from collections import defaultdict, OrderedDict |
---|
16 | |
---|
17 | USER_FILE = 'categories.json' |
---|
18 | |
---|
19 | logger = logging.getLogger(__name__) |
---|
20 | |
---|
21 | class CategoryInstaller: |
---|
22 | """ |
---|
23 | Class for making sure all category stuff is installed |
---|
24 | |
---|
25 | Note - class is entirely static! |
---|
26 | """ |
---|
27 | |
---|
28 | def __init__(self): |
---|
29 | """ initialization """ |
---|
30 | |
---|
31 | @staticmethod |
---|
32 | def _get_installed_model_dir(): |
---|
33 | """ |
---|
34 | returns the dir where installed_models.txt should be |
---|
35 | """ |
---|
36 | import sas.sascalc.dataloader.readers |
---|
37 | return sas.sascalc.dataloader.readers.get_data_path() |
---|
38 | |
---|
39 | @staticmethod |
---|
40 | def _get_models_py_dir(): |
---|
41 | """ |
---|
42 | returns the dir where models.py should be |
---|
43 | """ |
---|
44 | import sas.sasgui.perspectives.fitting.models |
---|
45 | return sas.sasgui.perspectives.fitting.models.get_model_python_path() |
---|
46 | |
---|
47 | @staticmethod |
---|
48 | def _get_default_cat_file_dir(): |
---|
49 | """ |
---|
50 | returns the dir where default_cat.j should be |
---|
51 | """ |
---|
52 | # The default categories file is usually found with the code, except |
---|
53 | # when deploying using py2app (it will be in Contents/Resources), or |
---|
54 | # py2exe (it will be in the exec dir). |
---|
55 | import sas.sasview |
---|
56 | cat_file = "default_categories.json" |
---|
57 | |
---|
58 | possible_cat_file_paths = [ |
---|
59 | os.path.join(os.path.split(sas.sasview.__file__)[0], cat_file), # Source |
---|
60 | os.path.join(os.path.dirname(sys.executable), '..', 'Resources', cat_file), # Mac |
---|
61 | os.path.join(os.path.dirname(sys.executable), cat_file) # Windows |
---|
62 | ] |
---|
63 | |
---|
64 | for path in possible_cat_file_paths: |
---|
65 | if os.path.isfile(path): |
---|
66 | return os.path.dirname(path) |
---|
67 | |
---|
68 | raise RuntimeError('CategoryInstaller: Could not find folder containing default categories') |
---|
69 | |
---|
70 | @staticmethod |
---|
71 | def _get_home_dir(): |
---|
72 | """ |
---|
73 | returns the users sasview config dir |
---|
74 | """ |
---|
75 | return os.path.join(os.path.expanduser("~"), ".sasview") |
---|
76 | |
---|
77 | @staticmethod |
---|
78 | def _regenerate_model_dict(master_category_dict): |
---|
79 | """ |
---|
80 | regenerates self.by_model_dict which has each model name as the key |
---|
81 | and the list of categories belonging to that model |
---|
82 | along with the enabled mapping |
---|
83 | returns tuplet (by_model_dict, model_enabled_dict) |
---|
84 | """ |
---|
85 | by_model_dict = defaultdict(list) |
---|
86 | model_enabled_dict = defaultdict(bool) |
---|
87 | |
---|
88 | for category in master_category_dict: |
---|
89 | for (model, enabled) in master_category_dict[category]: |
---|
90 | by_model_dict[model].append(category) |
---|
91 | model_enabled_dict[model] = enabled |
---|
92 | |
---|
93 | return (by_model_dict, model_enabled_dict) |
---|
94 | |
---|
95 | @staticmethod |
---|
96 | def _regenerate_master_dict(by_model_dict, model_enabled_dict): |
---|
97 | """ |
---|
98 | regenerates master_category_dict from by_model_dict |
---|
99 | and model_enabled_dict |
---|
100 | returns the master category dictionary |
---|
101 | """ |
---|
102 | master_category_dict = defaultdict(list) |
---|
103 | for model in by_model_dict: |
---|
104 | for category in by_model_dict[model]: |
---|
105 | master_category_dict[category].append(\ |
---|
106 | (model, model_enabled_dict[model])) |
---|
107 | return OrderedDict(sorted(master_category_dict.items(), key=lambda t: t[0])) |
---|
108 | |
---|
109 | @staticmethod |
---|
110 | def get_user_file(): |
---|
111 | """ |
---|
112 | returns the user data file, eg .sasview/categories.json.json |
---|
113 | """ |
---|
114 | return os.path.join(CategoryInstaller._get_home_dir(), USER_FILE) |
---|
115 | |
---|
116 | @staticmethod |
---|
117 | def get_default_file(): |
---|
118 | logger.warning("CategoryInstaller.get_default_file is deprecated.") |
---|
119 | |
---|
120 | @staticmethod |
---|
121 | def check_install(homedir = None, model_list=None): |
---|
122 | """ |
---|
123 | the main method of this class |
---|
124 | makes sure categories.json exists and if not |
---|
125 | compile it and install |
---|
126 | :param homefile: Override the default home directory |
---|
127 | :param model_list: List of model names except those in Plugin Models |
---|
128 | which are user supplied. |
---|
129 | """ |
---|
130 | _model_dict = { model.name: model for model in model_list} |
---|
131 | _model_list = _model_dict.keys() |
---|
132 | |
---|
133 | serialized_file = None |
---|
134 | if homedir is None: |
---|
135 | serialized_file = CategoryInstaller.get_user_file() |
---|
136 | else: |
---|
137 | serialized_file = os.path.join(homedir, USER_FILE) |
---|
138 | if os.path.isfile(serialized_file): |
---|
139 | with open(serialized_file, 'rb') as f: |
---|
140 | master_category_dict = json.load(f) |
---|
141 | else: |
---|
142 | master_category_dict = defaultdict(list) |
---|
143 | |
---|
144 | (by_model_dict, model_enabled_dict) = \ |
---|
145 | CategoryInstaller._regenerate_model_dict(master_category_dict) |
---|
146 | add_list = _model_list |
---|
147 | del_name = False |
---|
148 | for cat in master_category_dict.keys(): |
---|
149 | for ind in range(len(master_category_dict[cat])): |
---|
150 | model_name, enabled = master_category_dict[cat][ind] |
---|
151 | if model_name not in _model_list: |
---|
152 | del_name = True |
---|
153 | try: |
---|
154 | by_model_dict.pop(model_name) |
---|
155 | model_enabled_dict.pop(model_name) |
---|
156 | except: |
---|
157 | logger.error("CategoryInstaller: %s", sys.exc_value) |
---|
158 | else: |
---|
159 | add_list.remove(model_name) |
---|
160 | if del_name or (len(add_list) > 0): |
---|
161 | for model in add_list: |
---|
162 | model_enabled_dict[model]= True |
---|
163 | if _model_dict[model].category is None or len(str(_model_dict[model].category.capitalize())) == 0: |
---|
164 | by_model_dict[model].append('Uncategorized') |
---|
165 | else: |
---|
166 | category = _model_dict[model].category |
---|
167 | toks = category.split(':') |
---|
168 | category = toks[-1] |
---|
169 | toks = category.split('-') |
---|
170 | capitalized_words = [t.capitalize() for t in toks] |
---|
171 | category = ' '.join(capitalized_words) |
---|
172 | |
---|
173 | by_model_dict[model].append(category) |
---|
174 | |
---|
175 | master_category_dict = \ |
---|
176 | CategoryInstaller._regenerate_master_dict(by_model_dict, |
---|
177 | model_enabled_dict) |
---|
178 | |
---|
179 | json.dump(master_category_dict, open(serialized_file, 'wb')) |
---|