<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 keys(self):
        """List of settings keys."""
        return self._store.keys()

    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):
        """Load a settings Python file."""
        if not op.exists(path):
            debug("Skipping non-existing settings file: {}.".format(path))
            return
        # Try pickle.
        if not op.splitext(path)[1]:<commit_msg>Added debug message when loading settings.<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 keys(self):
        """List of settings keys."""
        return self._store.keys()

    def _try_load_pickle(self, path):
        try:
            self._store.update(_load_pickle(path))
            debug("Loaded internal settings file "
                  "from `{}`.".format(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))
            debug("Loaded internal settings file "
                  "from `{}`.".format(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):
        """Load a settings Python file."""
        if not op.exists(path):
            debug("Skipping non-existing settings file: {}.".format(path))
            return
        # Try pickle.
        if not op.splitext(path)[1]:
{'fraction_non_alphanumeric': 0.2826046511627907, 'fraction_numerical': 0.0022325581395348836, 'mean_word_length': 1.288633461047254, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3267916', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1367, 'n_words': 364}