<commit_before>from structlog import get_logger
import simplejson as json

from app.data_model.models import EQSession, db
from app.data_model.session_data import SessionData
from app.storage.storage_encryption import StorageEncryption

logger = get_logger()


class SessionStore:

    def __init__(self, user_ik, pepper, eq_session_id=None):
        self.eq_session_id = eq_session_id
        self.user_id = None
        self.user_ik = user_ik
        self.session_data = None
        self._eq_session = None
        self.pepper = pepper
        if eq_session_id:
            self._load()

    def create(self, eq_session_id, user_id, session_data):
        """
        Create a new eq_session and associate it with the user_id specified
        """
        self.eq_session_id = eq_session_id
        self.user_id = user_id
        self.session_data = session_data
        self._eq_session = EQSession(self.eq_session_id, self.user_id)

        return self

    def save(self):
        """
        save session
        """
        if self._eq_session:
            self._eq_session.session_data = \
                StorageEncryption(self.user_id, self.user_ik, self.pepper).encrypt_data(vars(self.session_data))

            # pylint: disable=maybe-no-member
            db.session.add(self._eq_session)
            db.session.commit()

    def delete(self):
        """
        deletes user session from database
        """
        if self._eq_session:
            # pylint: disable=maybe-no-member
            db.session.delete(self._eq_session)
            db.session.commit()

            self._eq_session = None
            self.eq_session_id = None
            self.user_id = None
            self.session_data = None

    def _load(self):

        if self._eq_session:
            return self._eq_session

        if self.eq_session_id is None:
            return None

        logger.debug('finding eq_session_id in database', eq_session_id=self.eq_session_id)

        # pylint: disable=maybe-no-member
        # SQLAlchemy doing declarative magic which makes session scope query property available
        self._eq_session = EQSession.query.filter(EQSession.eq_session_id == self.eq_session_id).first()

        if self._eq_session:
            self.user_id = self._eq_session.user_id

            if self._eq_session.session_data:
                encrypted_session_data = self._eq_session.session_data
                session_data = StorageEncryption(self.user_id, self.user_ik, self.pepper)\
                    .decrypt_data(encrypted_session_data)

                self.session_data = json.loads(session_data, object_hook=lambda d: SessionData(**d))

            logger.debug('found matching eq_session for eq_session_id in database',
                         session_id=self._eq_session.eq_session_id,
                         user_id=self._eq_session.user_id)
        else:
            logger.debug('eq_session_id not found in database', eq_session_id=self.eq_session_id)

        return self._eq_session

<commit_msg>lint change<commit_after>from structlog import get_logger
import simplejson as json

from app.data_model.models import EQSession, db
from app.data_model.session_data import SessionData
from app.storage.storage_encryption import StorageEncryption

logger = get_logger()


class SessionStore:

    def __init__(self, user_ik, pepper, eq_session_id=None):
        self.eq_session_id = eq_session_id
        self.user_id = None
        self.user_ik = user_ik
        self.session_data = None
        self._eq_session = None
        self.pepper = pepper
        if eq_session_id:
            self._load()

    def create(self, eq_session_id, user_id, session_data):
        """
        Create a new eq_session and associate it with the user_id specified
        """
        self.eq_session_id = eq_session_id
        self.user_id = user_id
        self.session_data = session_data
        self._eq_session = EQSession(self.eq_session_id, self.user_id)

        return self

    def save(self):
        """
        save session
        """
        if self._eq_session:
            self._eq_session.session_data = \
                StorageEncryption(self.user_id, self.user_ik, self.pepper).encrypt_data(vars(self.session_data))

            # pylint: disable=maybe-no-member
            db.session.add(self._eq_session)
            db.session.commit()

    def delete(self):
        """
        deletes user session from database
        """
        if self._eq_session:
            # pylint: disable=maybe-no-member
            db.session.delete(self._eq_session)
            db.session.commit()

            self._eq_session = None
            self.eq_session_id = None
            self.user_id = None
            self.session_data = None

    def _load(self):

        if self._eq_session:
            return self._eq_session

        if self.eq_session_id is None:
            return None

        logger.debug('finding eq_session_id in database', eq_session_id=self.eq_session_id)

        # pylint: disable=maybe-no-member
        # SQLAlchemy doing declarative magic which makes session scope query property available
        self._eq_session = EQSession.query.filter(EQSession.eq_session_id == self.eq_session_id).first()

        if self._eq_session:
            self.user_id = self._eq_session.user_id

            if self._eq_session.session_data:
                encrypted_session_data = self._eq_session.session_data
                session_data = StorageEncryption(self.user_id, self.user_ik, self.pepper)\
                    .decrypt_data(encrypted_session_data)

                self.session_data = json.loads(session_data, object_hook=lambda d: SessionData(**d))

            logger.debug('found matching eq_session for eq_session_id in database',
                         session_id=self._eq_session.eq_session_id,
                         user_id=self._eq_session.user_id)
        else:
            logger.debug('eq_session_id not found in database', eq_session_id=self.eq_session_id)

        return self._eq_session

{'fraction_non_alphanumeric': 0.07614966210647767, 'fraction_numerical': 0.0, 'mean_word_length': 2.225943646996279, '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': '5807682', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1720, 'n_words': 353}