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

## This file is part of Invenio.
## Copyright (C) 2011, 2012, 2013 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
    invenio.ext.session.backends.sqlalchemy
    ---------------------------------------

    Configuration:

    - SESSION_BACKEND_SQLALCHEMY = 'invenio.ext.sqlalchemy:db'
    - SESSION_BACKEND_SQLALCHEMY_MODEL = 'invenio.ext.session.model:Session'
    - SESSION_BACKEND_SQLALCHEMY_GETTER = 'get_session'
    - SESSION_BACKEND_SQLALCHEMY_SETTER = 'set_session'
    - SESSION_BACKEND_SQLALCHEMY_VALUE = 'session_object'
"""

from datetime import datetime

from flask import current_app
from flask.ext.login import current_user
from flask.helpers import locked_cached_property
from werkzeug import import_string

from ..storage import SessionStorage
from ..model import Session


class Storage(SessionStorage):
    """
    Implements database backend for SQLAlchemy model storage.
    """

    def __init__(self, *args, **kwargs):
        if not self.db.engine.dialect.has_table(self.db.engine,
                                                self.model.__tablename__):
            self.model.__table__.create(bind=self.db.engine)
            self.db.commit()

    @locked_cached_property
    def db(self):
        """Returns SQLAlchemy database object."""
        return import_string(current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY', 'invenio.ext.sqlalchemy:db'))

    @locked_cached_property
    def model(self):
        """Returns SQLAlchemy model."""
        return import_string(current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_MODEL',
            'invenio.ext.session.model:Session'))()

    @locked_cached_property
    def getter(self):
        return getattr(self.model, current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_GETTER', 'get_session'
            ))

    @locked_cached_property
    def setter(self):
        return getattr(self.model, current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_SETTER', 'set_session'
            ))

    @locked_cached_property
    def value(self):
        return current_app.config.get('SESSION_BACKEND_SQLALCHEMY_VALUE',
                                      'session_object')

    def set(self, name, value, timeout=None):
        s = self.setter(name, value, timeout=timeout)
        self.db.session.merge(s)
        self.db.session.commit()

    def get(self, name):
        return getattr(self.getter(name), self.value)

    def delete(self, name):
        self.getter(name).delete()
        self.db.session.commit()
<commit_msg>session: fix commit after automatic table creation<commit_after># -*- coding: utf-8 -*-

## This file is part of Invenio.
## Copyright (C) 2011, 2012, 2013 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
    invenio.ext.session.backends.sqlalchemy
    ---------------------------------------

    Configuration:

    - SESSION_BACKEND_SQLALCHEMY = 'invenio.ext.sqlalchemy:db'
    - SESSION_BACKEND_SQLALCHEMY_MODEL = 'invenio.ext.session.model:Session'
    - SESSION_BACKEND_SQLALCHEMY_GETTER = 'get_session'
    - SESSION_BACKEND_SQLALCHEMY_SETTER = 'set_session'
    - SESSION_BACKEND_SQLALCHEMY_VALUE = 'session_object'
"""

from datetime import datetime

from flask import current_app
from flask.ext.login import current_user
from flask.helpers import locked_cached_property
from werkzeug import import_string

from ..storage import SessionStorage
from ..model import Session


class Storage(SessionStorage):
    """
    Implements database backend for SQLAlchemy model storage.
    """

    def __init__(self, *args, **kwargs):
        if not self.db.engine.dialect.has_table(self.db.engine,
                                                self.model.__tablename__):
            self.model.__table__.create(bind=self.db.engine)
            self.db.session.commit()

    @locked_cached_property
    def db(self):
        """Returns SQLAlchemy database object."""
        return import_string(current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY', 'invenio.ext.sqlalchemy:db'))

    @locked_cached_property
    def model(self):
        """Returns SQLAlchemy model."""
        return import_string(current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_MODEL',
            'invenio.ext.session.model:Session'))()

    @locked_cached_property
    def getter(self):
        return getattr(self.model, current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_GETTER', 'get_session'
            ))

    @locked_cached_property
    def setter(self):
        return getattr(self.model, current_app.config.get(
            'SESSION_BACKEND_SQLALCHEMY_SETTER', 'set_session'
            ))

    @locked_cached_property
    def value(self):
        return current_app.config.get('SESSION_BACKEND_SQLALCHEMY_VALUE',
                                      'session_object')

    def set(self, name, value, timeout=None):
        s = self.setter(name, value, timeout=timeout)
        self.db.session.merge(s)
        self.db.session.commit()

    def get(self, name):
        return getattr(self.getter(name), self.value)

    def delete(self, name):
        self.getter(name).delete()
        self.db.session.commit()

{'fraction_non_alphanumeric': 0.10651609949641386, 'fraction_numerical': 0.008545704257591942, 'mean_word_length': 2.915173237753883, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708273', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1875, 'n_words': 504}