source: sasview/sansguiframe/src/sans/guiframe/CategoryInstaller.py @ aa01d07b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since aa01d07b was dad7cef, checked in by Jae Cho <jhjcho@…>, 12 years ago

more flexible finding of default cat folder

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