source: sasview/src/sas/sasgui/guiframe/CategoryInstaller.py @ d6bc9901

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since d6bc9901 was 6e50a8d, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

remove unused methods from CategoryInstaller?

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[959eb01]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 json
14import logging
15from collections import defaultdict, OrderedDict
16
[914ba0a]17from sas.sasgui import get_user_dir
18
[959eb01]19USER_FILE = 'categories.json'
20
21logger = logging.getLogger(__name__)
22
[6e50a8d]23class CategoryInstaller(object):
[959eb01]24    """
25    Class for making sure all category stuff is installed
26
27    Note - class is entirely static!
28    """
29
30    @staticmethod
31    def _regenerate_model_dict(master_category_dict):
32        """
33        regenerates self.by_model_dict which has each model name as the key
34        and the list of categories belonging to that model
35        along with the enabled mapping
36        returns tuplet (by_model_dict, model_enabled_dict)
37        """
38        by_model_dict = defaultdict(list)
39        model_enabled_dict = defaultdict(bool)
[914ba0a]40
[959eb01]41        for category in master_category_dict:
42            for (model, enabled) in master_category_dict[category]:
43                by_model_dict[model].append(category)
44                model_enabled_dict[model] = enabled
45
46        return (by_model_dict, model_enabled_dict)
47
48    @staticmethod
49    def _regenerate_master_dict(by_model_dict, model_enabled_dict):
50        """
[914ba0a]51        regenerates master_category_dict from by_model_dict
[959eb01]52        and model_enabled_dict
53        returns the master category dictionary
54        """
55        master_category_dict = defaultdict(list)
56        for model in by_model_dict:
57            for category in by_model_dict[model]:
58                master_category_dict[category].append(\
59                    (model, model_enabled_dict[model]))
60        return OrderedDict(sorted(master_category_dict.items(), key=lambda t: t[0]))
61
62    @staticmethod
63    def get_user_file():
64        """
65        returns the user data file, eg .sasview/categories.json.json
66        """
[914ba0a]67        return os.path.join(get_user_dir(), USER_FILE)
[959eb01]68
69    @staticmethod
70    def get_default_file():
71        logger.warning("CategoryInstaller.get_default_file is deprecated.")
72
73    @staticmethod
74    def check_install(homedir = None, model_list=None):
75        """
76        the main method of this class
77        makes sure categories.json exists and if not
78        compile it and install
79        :param homefile: Override the default home directory
80        :param model_list: List of model names except those in Plugin Models
81               which are user supplied.
82        """
83        _model_dict = { model.name: model for model in model_list}
84        _model_list = _model_dict.keys()
85
86        serialized_file = None
[235f514]87        if homedir is None:
[959eb01]88            serialized_file = CategoryInstaller.get_user_file()
89        else:
90            serialized_file = os.path.join(homedir, USER_FILE)
91        if os.path.isfile(serialized_file):
92            with open(serialized_file, 'rb') as f:
93                master_category_dict = json.load(f)
94        else:
95            master_category_dict = defaultdict(list)
96
97        (by_model_dict, model_enabled_dict) = \
98                CategoryInstaller._regenerate_model_dict(master_category_dict)
99        add_list = _model_list
100        del_name = False
101        for cat in master_category_dict.keys():
102            for ind in range(len(master_category_dict[cat])):
103                model_name, enabled = master_category_dict[cat][ind]
104                if model_name not in _model_list:
[914ba0a]105                    del_name = True
[959eb01]106                    try:
107                        by_model_dict.pop(model_name)
108                        model_enabled_dict.pop(model_name)
109                    except:
110                        logger.error("CategoryInstaller: %s", sys.exc_value)
111                else:
112                    add_list.remove(model_name)
113        if del_name or (len(add_list) > 0):
114            for model in add_list:
115                model_enabled_dict[model]= True
116                if _model_dict[model].category is None or len(str(_model_dict[model].category.capitalize())) == 0:
117                    by_model_dict[model].append('Uncategorized')
118                else:
119                    category = _model_dict[model].category
120                    toks = category.split(':')
121                    category = toks[-1]
122                    toks = category.split('-')
123                    capitalized_words = [t.capitalize() for t in toks]
124                    category = ' '.join(capitalized_words)
125
126                    by_model_dict[model].append(category)
127
128            master_category_dict = \
129                CategoryInstaller._regenerate_master_dict(by_model_dict,
130                                                          model_enabled_dict)
131
132            json.dump(master_category_dict, open(serialized_file, 'wb'))
Note: See TracBrowser for help on using the repository browser.