[125c4be] | 1 | """ |
---|
| 2 | Class for making sure all category stuff is installed |
---|
| 3 | and works fine. |
---|
| 4 | |
---|
| 5 | Copyright (c) Institut Laue-Langevin 2012 |
---|
| 6 | |
---|
| 7 | @author kieranrcampbell@gmail.com |
---|
| 8 | @modified by NIST/MD sasview team |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | import os |
---|
| 12 | import sys |
---|
| 13 | import json |
---|
| 14 | import logging |
---|
| 15 | from collections import defaultdict, OrderedDict |
---|
| 16 | |
---|
| 17 | USER_FILE = 'categories.json' |
---|
| 18 | |
---|
| 19 | class CategoryInstaller: |
---|
| 20 | """ |
---|
| 21 | Class for making sure all category stuff is installed |
---|
| 22 | |
---|
| 23 | Note - class is entirely static! |
---|
| 24 | """ |
---|
| 25 | |
---|
| 26 | def __init__(self): |
---|
| 27 | """ initialization """ |
---|
| 28 | |
---|
| 29 | @staticmethod |
---|
| 30 | def _get_home_dir(): |
---|
| 31 | """ |
---|
| 32 | returns the users sasview config dir |
---|
| 33 | """ |
---|
| 34 | return os.path.join(os.path.expanduser("~"), ".sasview") |
---|
| 35 | |
---|
| 36 | @staticmethod |
---|
| 37 | def _regenerate_model_dict(master_category_dict): |
---|
| 38 | """ |
---|
| 39 | regenerates self.by_model_dict which has each model name as the key |
---|
| 40 | and the list of categories belonging to that model |
---|
| 41 | along with the enabled mapping |
---|
| 42 | returns tuplet (by_model_dict, model_enabled_dict) |
---|
| 43 | """ |
---|
| 44 | by_model_dict = defaultdict(list) |
---|
| 45 | model_enabled_dict = defaultdict(bool) |
---|
| 46 | |
---|
| 47 | for category in master_category_dict: |
---|
| 48 | for (model, enabled) in master_category_dict[category]: |
---|
| 49 | by_model_dict[model].append(category) |
---|
| 50 | model_enabled_dict[model] = enabled |
---|
| 51 | |
---|
| 52 | return (by_model_dict, model_enabled_dict) |
---|
| 53 | |
---|
| 54 | @staticmethod |
---|
| 55 | def _regenerate_master_dict(by_model_dict, model_enabled_dict): |
---|
| 56 | """ |
---|
| 57 | regenerates master_category_dict from by_model_dict |
---|
| 58 | and model_enabled_dict |
---|
| 59 | returns the master category dictionary |
---|
| 60 | """ |
---|
| 61 | master_category_dict = defaultdict(list) |
---|
| 62 | for model in by_model_dict: |
---|
| 63 | for category in by_model_dict[model]: |
---|
| 64 | master_category_dict[category].append(\ |
---|
| 65 | (model, model_enabled_dict[model])) |
---|
[b3e8629] | 66 | return OrderedDict(sorted(list(master_category_dict.items()), key=lambda t: t[0])) |
---|
[125c4be] | 67 | |
---|
| 68 | @staticmethod |
---|
| 69 | def get_user_file(): |
---|
| 70 | """ |
---|
| 71 | returns the user data file, eg .sasview/categories.json.json |
---|
| 72 | """ |
---|
| 73 | return os.path.join(CategoryInstaller._get_home_dir(), USER_FILE) |
---|
| 74 | |
---|
| 75 | @staticmethod |
---|
| 76 | def get_default_file(): |
---|
| 77 | logging.warning("CategoryInstaller.get_default_file is deprecated.") |
---|
| 78 | |
---|
| 79 | @staticmethod |
---|
| 80 | def check_install(homedir = None, model_list=None): |
---|
| 81 | """ |
---|
| 82 | the main method of this class |
---|
| 83 | makes sure categories.json exists and if not |
---|
| 84 | compile it and install |
---|
| 85 | :param homefile: Override the default home directory |
---|
| 86 | :param model_list: List of model names except customized models |
---|
| 87 | """ |
---|
| 88 | _model_dict = { model.name: model for model in model_list} |
---|
[b3e8629] | 89 | _model_list = list(_model_dict.keys()) |
---|
[125c4be] | 90 | |
---|
| 91 | serialized_file = None |
---|
[cee5c78] | 92 | if homedir is None: |
---|
[125c4be] | 93 | serialized_file = CategoryInstaller.get_user_file() |
---|
| 94 | else: |
---|
| 95 | serialized_file = os.path.join(homedir, USER_FILE) |
---|
| 96 | if os.path.isfile(serialized_file): |
---|
| 97 | with open(serialized_file, 'rb') as f: |
---|
| 98 | master_category_dict = json.load(f) |
---|
| 99 | else: |
---|
| 100 | master_category_dict = defaultdict(list) |
---|
| 101 | |
---|
| 102 | (by_model_dict, model_enabled_dict) = \ |
---|
| 103 | CategoryInstaller._regenerate_model_dict(master_category_dict) |
---|
| 104 | add_list = _model_list |
---|
| 105 | del_name = False |
---|
[b3e8629] | 106 | for cat in list(master_category_dict.keys()): |
---|
[125c4be] | 107 | for ind in range(len(master_category_dict[cat])): |
---|
| 108 | model_name, enabled = master_category_dict[cat][ind] |
---|
| 109 | if model_name not in _model_list: |
---|
| 110 | del_name = True |
---|
| 111 | try: |
---|
| 112 | by_model_dict.pop(model_name) |
---|
| 113 | model_enabled_dict.pop(model_name) |
---|
| 114 | except: |
---|
[b3e8629] | 115 | logging.error("CategoryInstaller: %s", sys.exc_info()[1]) |
---|
[125c4be] | 116 | else: |
---|
| 117 | add_list.remove(model_name) |
---|
| 118 | if del_name or (len(add_list) > 0): |
---|
| 119 | for model in add_list: |
---|
| 120 | model_enabled_dict[model]= True |
---|
| 121 | if _model_dict[model].category is None or len(str(_model_dict[model].category.capitalize())) == 0: |
---|
| 122 | by_model_dict[model].append('Uncategorized') |
---|
| 123 | else: |
---|
| 124 | category = _model_dict[model].category |
---|
| 125 | toks = category.split(':') |
---|
| 126 | category = toks[-1] |
---|
| 127 | toks = category.split('-') |
---|
| 128 | capitalized_words = [t.capitalize() for t in toks] |
---|
| 129 | category = ' '.join(capitalized_words) |
---|
| 130 | |
---|
| 131 | by_model_dict[model].append(category) |
---|
| 132 | |
---|
| 133 | master_category_dict = \ |
---|
| 134 | CategoryInstaller._regenerate_master_dict(by_model_dict, |
---|
| 135 | model_enabled_dict) |
---|
| 136 | |
---|
[c889a3e] | 137 | json.dump(master_category_dict, open(serialized_file, "w", encoding="utf8")) |
---|
[125c4be] | 138 | |
---|