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 sanview team |
---|
9 | """ |
---|
10 | |
---|
11 | import os |
---|
12 | import sys |
---|
13 | import shutil |
---|
14 | import cPickle as pickle |
---|
15 | from collections import defaultdict |
---|
16 | |
---|
17 | USER_FILE = 'serialized_cat.p' |
---|
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() |
---|
45 | @staticmethod |
---|
46 | def _get_default_cat_p_dir(): |
---|
47 | """ |
---|
48 | returns the dir where default_cat.p should be |
---|
49 | """ |
---|
50 | app_path = sys.path[0] |
---|
51 | if os.path.isfile(app_path): |
---|
52 | app_path = os.path.dirname(app_path) |
---|
53 | return app_path |
---|
54 | |
---|
55 | @staticmethod |
---|
56 | def _get_home_dir(): |
---|
57 | """ |
---|
58 | returns the users sansview config dir |
---|
59 | """ |
---|
60 | return os.path.join(os.path.expanduser("~"), ".sasview") |
---|
61 | |
---|
62 | @staticmethod |
---|
63 | def _regenerate_model_dict(master_category_dict): |
---|
64 | """ |
---|
65 | regenerates self.by_model_dict which has each model name as the key |
---|
66 | and the list of categories belonging to that model |
---|
67 | along with the enabled mapping |
---|
68 | returns tuplet (by_model_dict, model_enabled_dict) |
---|
69 | """ |
---|
70 | by_model_dict = defaultdict(list) |
---|
71 | model_enabled_dict = defaultdict(bool) |
---|
72 | |
---|
73 | for category in master_category_dict: |
---|
74 | for (model, enabled) in master_category_dict[category]: |
---|
75 | by_model_dict[model].append(category) |
---|
76 | model_enabled_dict[model] = enabled |
---|
77 | |
---|
78 | return (by_model_dict, model_enabled_dict) |
---|
79 | |
---|
80 | |
---|
81 | @staticmethod |
---|
82 | def _regenerate_master_dict(by_model_dict, model_enabled_dict): |
---|
83 | """ |
---|
84 | regenerates master_category_dict from by_model_dict |
---|
85 | and model_enabled_dict |
---|
86 | returns the master category dictionary |
---|
87 | """ |
---|
88 | master_category_dict = defaultdict(list) |
---|
89 | for model in by_model_dict: |
---|
90 | for category in by_model_dict[model]: |
---|
91 | master_category_dict[category].append(\ |
---|
92 | (model, model_enabled_dict[model])) |
---|
93 | |
---|
94 | return master_category_dict |
---|
95 | |
---|
96 | @staticmethod |
---|
97 | def get_user_file(): |
---|
98 | """ |
---|
99 | returns the user data file, eg .sasview/serialized_cat.p |
---|
100 | """ |
---|
101 | return os.path.join(CategoryInstaller._get_home_dir(), |
---|
102 | USER_FILE) |
---|
103 | |
---|
104 | @staticmethod |
---|
105 | def get_default_file(): |
---|
106 | """ |
---|
107 | returns the path of the default file |
---|
108 | e.g. blahblah/default_categories.p |
---|
109 | """ |
---|
110 | return os.path.join(\ |
---|
111 | CategoryInstaller._get_default_cat_p_dir(), "default_categories.p") |
---|
112 | |
---|
113 | @staticmethod |
---|
114 | def check_install(homedir = None, defaultfile = None, |
---|
115 | modelsdir = None, installed_models_dir = None): |
---|
116 | """ |
---|
117 | the main method of this class |
---|
118 | makes sure serialized_cat.p exists and if not |
---|
119 | compile it and install |
---|
120 | :param homefile: Override the default home directory |
---|
121 | :param defaultfile: Override the default file location |
---|
122 | :param modelsfile: The file where models.py lives. This |
---|
123 | MUST be overwritten in setup.py |
---|
124 | :param installed_models_dir: Where installed_models.txt is to go: |
---|
125 | """ |
---|
126 | model_list = [] |
---|
127 | serialized_file = None |
---|
128 | if homedir == None: |
---|
129 | serialized_file = CategoryInstaller.get_user_file() |
---|
130 | else: |
---|
131 | serialized_file = os.path.join(homedir, USER_FILE) |
---|
132 | |
---|
133 | if os.path.exists(serialized_file): |
---|
134 | return |
---|
135 | |
---|
136 | if installed_models_dir == None: |
---|
137 | installed_models_dir = \ |
---|
138 | CategoryInstaller._get_installed_model_dir() |
---|
139 | |
---|
140 | installed_model_file = open( |
---|
141 | os.path.join(installed_models_dir, |
---|
142 | "installed_models.txt"), 'w') |
---|
143 | |
---|
144 | if modelsdir == None: |
---|
145 | modelsdir = CategoryInstaller._get_models_py_dir() |
---|
146 | python_model_file = open(os.path.join(modelsdir, |
---|
147 | "models.py"), |
---|
148 | 'r') |
---|
149 | |
---|
150 | python_models = python_model_file.read() |
---|
151 | |
---|
152 | # we remove models that appear in the installed |
---|
153 | # model folder but not in models.py . the excess |
---|
154 | # hard coded ones on the end come from them being |
---|
155 | # present in models.py but not actual models, eg |
---|
156 | # TwoLorenzianModel contains the string 'Lorenzian' |
---|
157 | # but we don't actually want to include Lorenzian |
---|
158 | model_list = [mod for mod in model_list if \ |
---|
159 | mod in python_models and \ |
---|
160 | not 'init' in mod and \ |
---|
161 | not 'BaseComponent' in mod \ |
---|
162 | and not 'MultiplicationModel' in mod \ |
---|
163 | and not 'pluginmodel' in mod \ |
---|
164 | and mod != 'PowerLawModel' \ |
---|
165 | and mod != 'Lorentzian'] |
---|
166 | |
---|
167 | |
---|
168 | for mod in model_list: |
---|
169 | installed_model_file.write(mod + '\n') |
---|
170 | |
---|
171 | installed_model_file.close() |
---|
172 | |
---|
173 | # start sorting category stuff |
---|
174 | default_file = None |
---|
175 | if defaultfile == None: |
---|
176 | default_file = CategoryInstaller.get_default_file() |
---|
177 | else: |
---|
178 | default_file = defaultfile |
---|
179 | |
---|
180 | master_category_dict = pickle.load(open(default_file, 'rb')) |
---|
181 | |
---|
182 | (by_model_dict, model_enabled_dict) = \ |
---|
183 | CategoryInstaller._regenerate_model_dict(master_category_dict) |
---|
184 | |
---|
185 | |
---|
186 | for found_model in model_list: |
---|
187 | if not found_model in by_model_dict: |
---|
188 | print found_model + ' : ' + str(by_model_dict[found_model]) |
---|
189 | by_model_dict[found_model].append("Uncategorized") |
---|
190 | model_enabled_dict[found_model] = True |
---|
191 | |
---|
192 | # remove any stray models from categorization |
---|
193 | # that aren't stored anymore |
---|
194 | |
---|
195 | models_to_delete = [] |
---|
196 | for model in by_model_dict: |
---|
197 | if not model in model_list: |
---|
198 | models_to_delete.append(model) |
---|
199 | |
---|
200 | for model in models_to_delete: |
---|
201 | by_model_dict.pop(model) |
---|
202 | |
---|
203 | master_category_dict = \ |
---|
204 | CategoryInstaller._regenerate_master_dict(by_model_dict, |
---|
205 | model_enabled_dict) |
---|
206 | |
---|
207 | pickle.dump( master_category_dict, |
---|
208 | open(default_file, 'wb') ) |
---|
209 | |
---|
210 | #shutil.copyfile(default_file, serialized_file) |
---|