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

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 ae4c139 was ea5fa58, checked in by Jae Cho <jhjcho@…>, 12 years ago

category stuffs start working in interp. environment

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