source: sasview/src/sans/guiframe/CategoryInstaller.py @ f468791

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 f468791 was b221176, checked in by Mathieu Doucet <doucetm@…>, 11 years ago

Fix installation and make sure SasView? starts

  • Property mode set to 100644
File size: 5.7 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        import sans.sansview
52        cat_file = "default_categories.p"
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')
60
61    @staticmethod
62    def _get_home_dir():
63        """
64        returns the users sansview config dir
65        """
66        return os.path.join(os.path.expanduser("~"), ".sasview")
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        """
105        returns the user data file, eg .sasview/serialized_cat.p
106        """
107        return os.path.join(CategoryInstaller._get_home_dir(),
108                            USER_FILE)
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(\
117            CategoryInstaller._get_default_cat_p_dir(), "default_categories.p")
118       
119    @staticmethod
120    def check_install(homedir = None, model_list=None):
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
126        :param model_list: List of model names except customized models
127        """
128        #model_list = []
129        default_file = CategoryInstaller.get_default_file()
130        serialized_file = None
131        master_category_dict = defaultdict(list)
132        if homedir == None:
133            serialized_file = CategoryInstaller.get_user_file()
134        else:
135            serialized_file = os.path.join(homedir, USER_FILE)
136        if os.path.isfile(serialized_file):
137            cat_file = open(serialized_file, 'rb')
138        else:
139            cat_file = open(default_file, 'rb')
140        master_category_dict = pickle.Unpickler(cat_file).load()
141        (by_model_dict, model_enabled_dict) = \
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)
166           
167            pickle.dump( master_category_dict,
168                         open(serialized_file, 'wb') )
169           
170            try:
171                #It happens only in source environment
172                shutil.copyfile(serialized_file, default_file)
173            except:
174                pass
175       
Note: See TracBrowser for help on using the repository browser.