<commit_before>"""
Class for making sure all category stuff is installed
and works fine.

Copyright (c) Institut Laue-Langevin 2012

@author <EMAIL>
@modified by NIST/MD sasview team
"""

import os
import sys
import shutil
import cPickle as pickle
from collections import defaultdict

USER_FILE = 'serialized_cat.p'

class CategoryInstaller:
    """
    Class for making sure all category stuff is installed

    Note - class is entirely static!
    """


    def __init__(self):
        """ initialization """

    @staticmethod
    def _get_installed_model_dir():
        """
        returns the dir where installed_models.txt should be
        """
        import sans.dataloader.readers
        return sans.dataloader.readers.get_data_path()

    @staticmethod
    def _get_models_py_dir():
        """
        returns the dir where models.py should be
        """
        import sans.perspectives.fitting.models
        return sans.perspectives.fitting.models.get_model_python_path()
    
    @staticmethod
    def _get_default_cat_p_dir():
        """
        returns the dir where default_cat.p should be
        """
        import sans.sansview
        cat_file = "default_categories.p"
        dir, file_name = os.path.split(sans.sansview.__file__)
        cat_file_path = os.path.join(dir, cat_file)
        if os.path.isfile(cat_file_path):
            cat_file_dir = os.path.dirname(cat_file_path)
            return cat_file_dir
        
        raise RuntimeError('CategoryInstaller: Could not find folder containing default categories')

    @staticmethod
    def _get_home_dir():
        """
        returns the users sansview config dir
        """
        return os.path.join(os.path.expanduser("~"), ".sasview")

    @staticmethod
    def _regenerate_model_dict(master_category_dict):
        """
        regenerates self.by_model_dict which has each model name as the key
        and the list of categories belonging to that model
        along with the enabled mapping
        returns tuplet (by_model_dict, model_enabled_dict)
        """
        by_model_dict = defaultdict(list)
        model_enabled_dict = defaultdict(bool)
        
        for category in master_category_dict:
            for (model, enabled) in master_category_dict[category]:
                by_model_dict[model].append(category)
                model_enabled_dict[model] = enabled
    
        return (by_model_dict, model_enabled_dict)<commit_msg>Find default categories with p2app<commit_after>"""
Class for making sure all category stuff is installed
and works fine.

Copyright (c) Institut Laue-Langevin 2012

@author <EMAIL>
@modified by NIST/MD sasview team
"""

import os
import sys
import shutil
import cPickle as pickle
from collections import defaultdict

USER_FILE = 'serialized_cat.p'

class CategoryInstaller:
    """
    Class for making sure all category stuff is installed

    Note - class is entirely static!
    """


    def __init__(self):
        """ initialization """

    @staticmethod
    def _get_installed_model_dir():
        """
        returns the dir where installed_models.txt should be
        """
        import sans.dataloader.readers
        return sans.dataloader.readers.get_data_path()

    @staticmethod
    def _get_models_py_dir():
        """
        returns the dir where models.py should be
        """
        import sans.perspectives.fitting.models
        return sans.perspectives.fitting.models.get_model_python_path()
    
    @staticmethod
    def _get_default_cat_p_dir():
        """
        returns the dir where default_cat.p should be
        """
        # The default categories file is usually found with the code
        import sans.sansview
        cat_file = "default_categories.p"
        dir, file_name = os.path.split(sans.sansview.__file__)
        cat_file_path = os.path.join(dir, cat_file)
        if os.path.isfile(cat_file_path):
            cat_file_dir = os.path.dirname(cat_file_path)
            return cat_file_dir
        
        # When deploying using py2app, the default categories file
        # can be found in Contents/Resources
        cat_file_path = os.path.join(os.path.dirname(sys.executable), '..', 'Resources', 'default_categories.p')
        if os.path.isfile(cat_file_path):
            cat_file_dir = os.path.dirname(cat_file_path)
            return cat_file_dir
        
        raise RuntimeError('CategoryInstaller: Could not find folder containing default categories')

    @staticmethod
    def _get_home_dir():
        """
        returns the users sansview config dir
        """
        return os.path.join(os.path.expanduser("~"), ".sasview")

    @staticmethod
    def _regenerate_model_dict(master_category_dict):
        """
        regenerates self.by_model_dict which has each model name as the key
        and the list of categories belonging to that model
        along with the enabled mapping
        returns tuplet (by_model_dict, model_enabled_dict)
        """
        by_model_dict = defaultdict(list)
        model_enabled_dict = defaultdict(bool)
        
        for category in master_category_dict:
            for (model, enabled) in master_category_dict[category]:
                by_model_dict[model].append(category)
                model_enabled_dict[model] = enabled
    
        return (by_model_dict, model_enabled_dict)
{'fraction_non_alphanumeric': 0.07650579872802095, 'fraction_numerical': 0.0018705574261129816, 'mean_word_length': 2.555186170212766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7234507', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1494, 'n_words': 406}