<commit_before>from setman.exceptions import ValidationError
from setman.utils import logger
from setman.utils.parsing import is_settings_container


__all__ = ('SetmanBackend', )


class SetmanBackend(object):
    """
    How ``setman`` library will interact with the data storage.
    """
    available_settings = None
    data_cache_key = '_data_cache'
    framework = None

    def __init__(self, **kwargs):
        """
        Initialize backend object and read settings from data storage.
        """
        # Copy all keyword arguments to the instance scope. But ignore ``data``
        # keyword if any.
        if 'data' in kwargs:
            kwargs.pop('data')

        self.__dict__.update(kwargs)

    def __getattribute__(self, name):
        """
        Deny ability of saving invalid data.
        """
        if name != 'save':
            return super(SetmanBackend, self).__getattribute__(name)

        if hasattr(self, 'error'):
            raise ValueError('Cannot save invalid settings.')

        return super(SetmanBackend, self).__getattribute__(name)

    def clear(self):
        """
        Clear data cache from backend instance if any.
        """
        del self.data

        if hasattr(self, 'error'):
            delattr(self, 'error')

    @property
    def data(self):
        """
        Read data from storage or return it from cache.
        """
        if not hasattr(self, self.data_cache_key):
            value = self._batch_method('to_python', self.read())
            setattr(self, self.data_cache_key, value)
        return getattr(self, self.data_cache_key)

    @data.deleter
    def data(self):
        """
        Delete data attribute.
        """
        if hasattr(self, self.data_cache_key):
            delattr(self, self.data_cache_key)

    @data.setter
    def data(self, value):
        """
        Alternate setup of data storage.
        """
        value = self._batch_method('to_python', value)
        setattr(self, self.data_cache_key, value)

    def is_valid(self, prefix=None):
        """
        Validate every setting before save.<commit_msg>Add some logging to find what wrong with saving values on uWSGI setup.<commit_after>from setman.exceptions import ValidationError
from setman.utils import logger
from setman.utils.parsing import is_settings_container


__all__ = ('SetmanBackend', )


class SetmanBackend(object):
    """
    How ``setman`` library will interact with the data storage.
    """
    available_settings = None
    data_cache_key = '_data_cache'
    framework = None

    def __init__(self, **kwargs):
        """
        Initialize backend object and read settings from data storage.
        """
        # Copy all keyword arguments to the instance scope. But ignore ``data``
        # keyword if any.
        if 'data' in kwargs:
            kwargs.pop('data')

        self.__dict__.update(kwargs)

    def __getattribute__(self, name):
        """
        Deny ability of saving invalid data.
        """
        if name != 'save':
            return super(SetmanBackend, self).__getattribute__(name)

        if hasattr(self, 'error'):
            raise ValueError('Cannot save invalid settings.')

        logger.info('Initiate saving %s settings to %s backend',
                    self.data, self.__class__.name)
        return super(SetmanBackend, self).__getattribute__(name)

    def clear(self):
        """
        Clear data cache from backend instance if any.
        """
        del self.data

        if hasattr(self, 'error'):
            delattr(self, 'error')

    @property
    def data(self):
        """
        Read data from storage or return it from cache.
        """
        if not hasattr(self, self.data_cache_key):
            logger.info('Reading data from backend')
            value = self._batch_method('to_python', self.read())
            setattr(self, self.data_cache_key, value)
        else:
            logger.info('Reading data from cache')
        return getattr(self, self.data_cache_key)

    @data.deleter
    def data(self):
        """
        Delete data attribute.
        """
        if hasattr(self, self.data_cache_key):
            logger.info('Removing data from cache')
            delattr(self, self.data_cache_key)
        else:
            logger.info('Data is not at cache backend')

    @data.setter
    def data(self, value):
        """
        Alternate setup of data storage.
        """
        logger.info('Setting up data to cache')
        value = self._batch_method('to_python', value)
        setattr(self, self.data_cache_key, value)

    def is_valid(self, prefix=None):
        """
        Validate every setting before save.
{'fraction_non_alphanumeric': 0.10055389859394973, 'fraction_numerical': 0.0, 'mean_word_length': 2.196051735874745, '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': '213431', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1269, 'n_words': 375}