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

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

merging from the release 2.2.0

  • Property mode set to 100644
File size: 5.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
[dad7cef]8@modified by NIST/MD sasview team
[df7a7e3]9"""
10
11import os
[c35e6901]12import sys
[df7a7e3]13import shutil
14import cPickle as pickle
15from collections import defaultdict
16
[ea5fa58]17USER_FILE = 'serialized_cat.p'
[df7a7e3]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()
[dad7cef]45   
[df7a7e3]46    @staticmethod
47    def _get_default_cat_p_dir():
48        """
49        returns the dir where default_cat.p should be
50        """
[dad7cef]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')         
[df7a7e3]65
66    @staticmethod
67    def _get_home_dir():
68        """
69        returns the users sansview config dir
70        """
[ea5fa58]71        return os.path.join(os.path.expanduser("~"), ".sasview")
[df7a7e3]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        """
[ea5fa58]110        returns the user data file, eg .sasview/serialized_cat.p
[df7a7e3]111        """
112        return os.path.join(CategoryInstaller._get_home_dir(),
[ea5fa58]113                            USER_FILE)
[df7a7e3]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(\
[ea5fa58]122            CategoryInstaller._get_default_cat_p_dir(), "default_categories.p")
[df7a7e3]123       
124    @staticmethod
[657e52c]125    def check_install(homedir = None, model_list=None):
[df7a7e3]126        """
127        the main method of this class
128        makes sure serialized_cat.p exists and if not
129        compile it and install
130        :param homefile: Override the default home directory
[657e52c]131        :param model_list: List of model names except customized models
[df7a7e3]132        """
[657e52c]133        #model_list = []
134        default_file = CategoryInstaller.get_default_file()
[df7a7e3]135        serialized_file = None
[657e52c]136        master_category_dict = defaultdict(list)
[df7a7e3]137        if homedir == None:
138            serialized_file = CategoryInstaller.get_user_file()
139        else:
[ea5fa58]140            serialized_file = os.path.join(homedir, USER_FILE)
[657e52c]141        if os.path.isfile(serialized_file):
142            cat_file = open(serialized_file, 'rb')
[df7a7e3]143        else:
[657e52c]144            cat_file = open(default_file, 'rb')
145        master_category_dict = pickle.Unpickler(cat_file).load()
[df7a7e3]146        (by_model_dict, model_enabled_dict) = \
[657e52c]147                CategoryInstaller._regenerate_model_dict(master_category_dict)
148        cat_file.close()
149        add_list = model_list
150        del_name = False
151        for cat in master_category_dict.keys():
152            for ind in range(len(master_category_dict[cat])):
153                model_name, enabled = master_category_dict[cat][ind]
154                if model_name not in model_list:
155                    del_name = True 
156                    try:
157                        by_model_dict.pop(model_name)
158                        model_enabled_dict.pop(model_name)
159                    except:
160                        pass
161                else:
162                    add_list.remove(model_name)
163        if del_name or (len(add_list) > 0):
164            for model in add_list:
165                model_enabled_dict[model]= True
166                by_model_dict[model].append('Uncategorized')
167   
168            master_category_dict = \
169                CategoryInstaller._regenerate_master_dict(by_model_dict,
170                                                          model_enabled_dict)
[df7a7e3]171           
[657e52c]172            pickle.dump( master_category_dict,
173                         open(serialized_file, 'wb') )
[df7a7e3]174           
[657e52c]175            try:
176                #It happens only in source environment
177                shutil.copyfile(serialized_file, default_file)
178            except:
179                pass
[df7a7e3]180       
Note: See TracBrowser for help on using the repository browser.