<commit_before># -*- coding: utf-8 -*-

"""Settings."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

import os
import os.path as op

from ..ext.six.moves import cPickle
from .logging import debug, warn


#------------------------------------------------------------------------------
# Utility functions
#------------------------------------------------------------------------------

def _load_python(path):
    path = op.realpath(op.expanduser(path))
    assert op.exists(path)
    with open(path, 'r') as f:
        contents = f.read()
    store = {}
    exec(contents, {}, store)
    return store


def _load_pickle(path):
    path = op.realpath(op.expanduser(path))
    assert op.exists(path)
    with open(path, 'rb') as f:
        store = cPickle.load(f)
        assert isinstance(store, dict)
        return store


def _save_pickle(path, store):
    path = op.realpath(op.expanduser(path))
    with open(path, 'wb') as f:
        cPickle.dump(store, f)


#------------------------------------------------------------------------------
# Settings
#------------------------------------------------------------------------------

class BaseSettings(object):
    """Store key-value pairs."""
    def __init__(self):
        self._store = {}
        self._to_save = {}

    def __getitem__(self, key):
        return self._store[key]

    def __setitem__(self, key, value):
        self._store[key] = value
        self._to_save[key] = value

    def __contains__(self, key):
        return key in self._store

    def _try_load_pickle(self, path):
        try:
            self._store.update(_load_pickle(path))
            return True
        except Exception as e:
            warn("Unable to read the internal settings. "
                 "You may want to delete '{0}'.\n{1}".format(path, str(e)))

    def _try_load_python(self, path):
        try:
            self._store.update(_load_python(path))
            return True
        except Exception as e:
            warn("Unable to read the settings file "
                 "'{0}':\n{1}".format(path, str(e)))
            return {}

    def load(self, path):
        if not op.exists(path):
            debug("The settings file {} doesn't exist.".format(path))
            return
        # Try pickle.
        if not op.splitext(path)[1]:
            if not self._try_load_pickle(path):
                self._try_load_python(path)
        # Try Python.
        else:
            self._try_load_python(path)

    def save(self, path):
        try:
            _save_pickle(path, self._to_save)
        except Exception as e:
            warn("Unable to save the internal settings file "
                 "at '{}':\n{}".format(path, str(e)))
        self._to_save = {}


class Settings(object):
    """Manage default, user-wide, and experiment-wide settings."""

    def __init__(self, phy_user_dir=None, default_path=None):
        assert phy_user_dir is not None
        # `.phy/` user directory, ~/.phy by default.
        # if phy_user_dir is None:
            # phy_user_dir = _phy_user_dir()
        self.phy_user_dir = phy_user_dir
        _ensure_dir_exists(self.phy_user_dir)

        self._bs = BaseSettings()

        # Load phy's defaults.
        if default_path:
            self._bs.load(default_path)

        # Load the user's internal settings.
        self.internal_settings_path = op.join(self.phy_user_dir,
                                              'internal_settings')
        self._bs.load(self.internal_settings_path)

        # load the user defaults.
        self.user_settings_path = op.join(self.phy_user_dir,
                                          'user_settings.py')
        self._bs.load(self.user_settings_path)

    def on_open(self, path):
        # Get the experiment settings path.
        path = op.realpath(op.expanduser(path))
        self.exp_path = path
        self.exp_name = op.splitext(op.basename(path))[0]
        self.exp_dir = op.dirname(path)
        self.exp_settings_dir = op.join(self.exp_dir, self.exp_name + '.phy')
        self.exp_settings_path = op.join(self.exp_settings_dir,
                                         'user_settings.py')
        _ensure_dir_exists(self.exp_settings_dir)

        # Load experiment-wide settings.
        self._bs.load(self.exp_settings_path)

    def save(self):
        self._bs.save(self.internal_settings_path)

    def __getitem__(self, key):
        return self._bs[key]

    def __setitem__(self, key, value):
        self._bs[key] = value

    def __contains__(self, key):
        return key in self._bs


#------------------------------------------------------------------------------
# Config
#------------------------------------------------------------------------------

_PHY_USER_DIR_NAME = '.phy'


def _phy_user_dir():
    """Return the absolute path to the phy user directory."""
    home = op.expanduser("~")
    path = op.realpath(op.join(home, _PHY_USER_DIR_NAME))<commit_msg>Reload all user settings when opening a new file.<commit_after># -*- coding: utf-8 -*-

"""Settings."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

import os
import os.path as op

from ..ext.six.moves import cPickle
from .logging import debug, warn


#------------------------------------------------------------------------------
# Utility functions
#------------------------------------------------------------------------------

def _load_python(path):
    path = op.realpath(op.expanduser(path))
    assert op.exists(path)
    with open(path, 'r') as f:
        contents = f.read()
    store = {}
    exec(contents, {}, store)
    return store


def _load_pickle(path):
    path = op.realpath(op.expanduser(path))
    assert op.exists(path)
    with open(path, 'rb') as f:
        store = cPickle.load(f)
        assert isinstance(store, dict)
        return store


def _save_pickle(path, store):
    path = op.realpath(op.expanduser(path))
    with open(path, 'wb') as f:
        cPickle.dump(store, f)


#------------------------------------------------------------------------------
# Settings
#------------------------------------------------------------------------------

class BaseSettings(object):
    """Store key-value pairs."""
    def __init__(self):
        self._store = {}
        self._to_save = {}

    def __getitem__(self, key):
        return self._store[key]

    def __setitem__(self, key, value):
        self._store[key] = value
        self._to_save[key] = value

    def __contains__(self, key):
        return key in self._store

    def _try_load_pickle(self, path):
        try:
            self._store.update(_load_pickle(path))
            return True
        except Exception as e:
            warn("Unable to read the internal settings. "
                 "You may want to delete '{0}'.\n{1}".format(path, str(e)))

    def _try_load_python(self, path):
        try:
            self._store.update(_load_python(path))
            return True
        except Exception as e:
            warn("Unable to read the settings file "
                 "'{0}':\n{1}".format(path, str(e)))
            return {}

    def load(self, path):
        if not op.exists(path):
            debug("The settings file {} doesn't exist.".format(path))
            return
        # Try pickle.
        if not op.splitext(path)[1]:
            if not self._try_load_pickle(path):
                self._try_load_python(path)
        # Try Python.
        else:
            self._try_load_python(path)

    def save(self, path):
        try:
            _save_pickle(path, self._to_save)
        except Exception as e:
            warn("Unable to save the internal settings file "
                 "at '{}':\n{}".format(path, str(e)))
        self._to_save = {}


class Settings(object):
    """Manage default, user-wide, and experiment-wide settings."""

    def __init__(self, phy_user_dir=None, default_path=None):
        assert phy_user_dir is not None
        # `.phy/` user directory, ~/.phy by default.
        # if phy_user_dir is None:
            # phy_user_dir = _phy_user_dir()
        self.phy_user_dir = phy_user_dir
        _ensure_dir_exists(self.phy_user_dir)

        self._default_path = default_path

        self._bs = BaseSettings()
        self._load_user_settings()

    def _load_user_settings(self):

        # Load phy's defaults.
        if self._default_path:
            self._bs.load(self._default_path)

        # load the user defaults.
        self.user_settings_path = op.join(self.phy_user_dir,
                                          'user_settings.py')
        self._bs.load(self.user_settings_path)

        # Load the user's internal settings.
        self.internal_settings_path = op.join(self.phy_user_dir,
                                              'internal_settings')
        self._bs.load(self.internal_settings_path)

    def on_open(self, path):
        # Get the experiment settings path.
        path = op.realpath(op.expanduser(path))
        self.exp_path = path
        self.exp_name = op.splitext(op.basename(path))[0]
        self.exp_dir = op.dirname(path)
        self.exp_settings_dir = op.join(self.exp_dir, self.exp_name + '.phy')
        self.exp_settings_path = op.join(self.exp_settings_dir,
                                         'user_settings.py')
        _ensure_dir_exists(self.exp_settings_dir)

        # Load experiment-wide settings.
        self._load_user_settings()
        self._bs.load(self.exp_settings_path)

    def save(self):
        self._bs.save(self.internal_settings_path)

    def __getitem__(self, key):
        return self._bs[key]

    def __setitem__(self, key, value):
        self._bs[key] = value

    def __contains__(self, key):
        return key in self._bs


#------------------------------------------------------------------------------
# Config
#------------------------------------------------------------------------------

_PHY_USER_DIR_NAME = '.phy'


def _phy_user_dir():
    """Return the absolute path to the phy user directory."""
    home = op.expanduser("~")
    path = op.realpath(op.join(home, _PHY_USER_DIR_NAME))
{'fraction_non_alphanumeric': 0.22114920328343796, 'fraction_numerical': 0.0013520038628681795, 'mean_word_length': 1.4822627037392138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6235311', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2808, 'n_words': 643}