<commit_before>#!/usr/bin/env python2
# Copyright 2007 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @author <NAME> <<EMAIL>>
# part of gap project (https://github.com/czervenka/gap)

__author__ = '<NAME> <<EMAIL>>'

"""
Simple database app settings

BASIC USE
---------
    from app_settings import settings
    version = settings['version']  # read settings
    settings['version'] = '1'  # stores settings

INITIALIZATION
--------------
    from gap.conf import settings
    settings['version'] = '1'  # new version is automatically written to db
    settings.add_setting('my_new_setting', 'Initial value')   # adds new setting property to db
    settings.add_setting('my_structured_property', {'title': 'Title'})  # adds new structured property
    settings['my_structured_property'].title
    >> 'Title'
"""
import logging
from uuid import uuid1

from google.appengine.ext import ndb
from google.appengine.api import memcache


class AppSettings(ndb.Expando):

    KEY = 'app_settings'
    _default_indexed = False

    version = ndb.StringProperty(default='0', indexed=False)

    @classmethod
    def get_key(cls):
        return ndb.Key(cls, cls.KEY)

    def put(self, *args, **kwargs):
        self.set_uuid()
        if not self.key:
            self.key = self.get_key()
        # self.key.delete(use_datastore=False)  # flush ndb model cache
        super(self.__class__, self).put(*args, **kwargs)

    @classmethod
    def get_uuid(cls):
        return memcache.get('AppSettings:version')

    @classmethod
    def set_uuid(cls):
        uuid = uuid1()
        memcache.set('AppSettings:version', uuid)


class LazyAppSettings(object):
    """
    Settings row property descriptor used in SettingsDict.
    """

    #@ndb.transactional
    def __get__(self, instance, owner=None):
        uuid = AppSettings.get_uuid()
        if not hasattr(instance, '_cached_settings') or getattr(instance, '_cached_uuid', None) != uuid:
            key = ndb.Key(AppSettings, AppSettings.KEY)
            row = key.get()
            if row is None:
                row = AppSettings(key=key)
                row.put()
            instance._cached_settings = row
            instance._cached_uuid = uuid
            logging.debug('Getting settings')
        return instance._cached_settings


class SettingsDict(object):
    """
    Dictionary which reads settings from database.

    To list all settings simply calll settings.items()
    """

    _data = LazyAppSettings()

    def _set_settings(self, dict_data):
        self._data = dict_data

    def __getitem__(self, key):
        if hasattr(self._data, key):
            value = getattr(self._data, key)
            if isinstance(value, ndb.Expando):
                oldsetter = value.__setattr__
                def new_setter(obj, key, value):
                    oldsetter(key, value)
                    self.save()

                value.__setattribute__ = new_setter
            return value
        else:
            raise KeyError('Key %s not found.' % key)

    # @ndb.transactional
    def __setitem__(self, key, value):
        if not key in self:
            raise KeyError("Settings property %r is not defined yet. Please use add_key method to add new property." % key)
        setattr(self._data, key, value)
        self.save()

    # @ndb.transactional
    def add_setting(self, key, value):
        if key in self:
            raise KeyError("The property %r already exists.")
        setattr(self._data, key, value)
        self.save()

    def save(self):
        self._data.put()

    def reload(self):
        delattr(self, '_cached_uuid')

    def del_setting(self, key):
        delattr(self._data, key)
        self._data.put()

    def __hasitem__(self, key):
        return key in self.keys()<commit_msg>default values read from config.py<commit_after>#!/usr/bin/env python2
# Copyright 2007 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# @author <NAME> <<EMAIL>>
# part of gap project (https://github.com/czervenka/gap)

__author__ = '<NAME> <<EMAIL>>'

"""
Simple database app settings

BASIC USE
---------
    from app_settings import settings
    version = settings['version']  # read settings
    settings['version'] = '1'  # stores settings

INITIALIZATION
--------------
    from gap.conf import settings
    settings['version'] = '1'  # new version is automatically written to db
    settings.add_setting('my_new_setting', 'Initial value')   # adds new setting property to db
    settings.add_setting('my_structured_property', {'title': 'Title'})  # adds new structured property
    settings['my_structured_property'].title
    >> 'Title'
"""
import logging
from uuid import uuid1

from google.appengine.ext import ndb
from google.appengine.api import memcache
try:
    from config import DEFAULT_SETTINGS
except ImportError:
    DEFAULT_SETTINGS = {}


class AppSettings(ndb.Expando):

    KEY = 'app_settings'
    _default_indexed = False

    version = ndb.StringProperty(default='0', indexed=False)

    @classmethod
    def get_key(cls):
        return ndb.Key(cls, cls.KEY)

    def put(self, *args, **kwargs):
        self.set_uuid()
        if not self.key:
            self.key = self.get_key()
        # self.key.delete(use_datastore=False)  # flush ndb model cache
        super(self.__class__, self).put(*args, **kwargs)

    @classmethod
    def get_uuid(cls):
        return memcache.get('AppSettings:version')

    @classmethod
    def set_uuid(cls):
        uuid = uuid1()
        memcache.set('AppSettings:version', uuid)


class LazyAppSettings(object):
    """
    Settings row property descriptor used in SettingsDict.
    """

    <EMAIL>
    def __get__(self, instance, owner=None):
        uuid = AppSettings.get_uuid()
        if not hasattr(instance, '_cached_settings') or getattr(instance, '_cached_uuid', None) != uuid:
            key = ndb.Key(AppSettings, AppSettings.KEY)
            row = key.get()
            if row is None:
                row = AppSettings(key=key)
                row.put()
            instance._cached_settings = row
            instance._cached_uuid = uuid
            logging.debug('Getting settings')
        return instance._cached_settings


class SettingsDict(object):
    """
    Dictionary which reads settings from database.

    To list all settings simply calll settings.items()
    """

    _data = LazyAppSettings()

    def _set_settings(self, dict_data):
        self._data = dict_data

    def __getitem__(self, key):
        if hasattr(self._data, key):
            value = getattr(self._data, key)
            if isinstance(value, ndb.Expando):
                oldsetter = value.__setattr__
                def new_setter(obj, key, value):
                    oldsetter(key, value)
                    self.save()

                value.__setattribute__ = new_setter
            return value
        elif key in DEFAULT_SETTINGS:
            return DEFAULT_SETTINGS[key]
        else:
            raise KeyError('Key %s not found.' % key)

    # @ndb.transactional
    def __setitem__(self, key, value):
        if not key in self and key not in DEFAULT_SETTINGS:
            raise KeyError("Settings property %r is not defined yet. Please use add_key method to add new property." % key)
        setattr(self._data, key, value)
        self.save()

    # @ndb.transactional
    def add_setting(self, key, value):
        if key in self:
            raise KeyError("The property %r already exists.")
        setattr(self._data, key, value)
        self.save()

    def save(self):
        self._data.put()

    def reload(self):
        delattr(self, '_cached_uuid')

    def del_setting(self, key):
        delattr(self._data, key)
        self._data.put()

    def __hasitem__(self, key):
        return key in self.keys()
{'fraction_non_alphanumeric': 0.10576378666062733, 'fraction_numerical': 0.0031706488506397915, 'mean_word_length': 2.593165174938975, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 26, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3334353', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2487, 'n_words': 768}