<commit_before>"""
The base for using sqlalchemy as a store with TiddlyWeb.
"""

from __future__ import absolute_import

import logging

from pyparsing import ParseException

from base64 import b64encode, b64decode
from sqlalchemy import event
from sqlalchemy.engine import create_engine, Engine
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql.expression import and_

from tiddlyweb.filters import FilterIndexRefused
from tiddlyweb.model.bag import Bag
from tiddlyweb.model.policy import Policy
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.model.user import User
from tiddlyweb.serializer import Serializer
from tiddlyweb.store import (NoBagError, NoRecipeError, NoTiddlerError,
        NoUserError, StoreError)
from tiddlyweb.stores import StorageInterface
from tiddlyweb.util import binary_tiddler

from .model import (Base, Session, sBag, sPolicy, sRecipe, sTiddler, sRevision,
        sText, sTag, sField, sCurrentRevision, sFirstRevision, sUser, sRole)
from .parser import DEFAULT_PARSER
from .producer import Producer

__version__ = '3.0.16'

#logging.basicConfig()
#logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
#logging.getLogger('sqlalchemy.pool').setLevel(logging.DEBUG)


class Store(StorageInterface):
    """
    A SqlAlchemy based storage interface for TiddlyWeb.
    """

    mapped = False

    def __init__(self, store_config=None, environ=None):
        super(Store, self).__init__(store_config, environ)
        self.store_type = self._db_config().split(':', 1)[0]
        self.parser = DEFAULT_PARSER
        self.producer = Producer()
        self.has_geo = False
        self._init_store()

    def _init_store(self):
        """
        Establish the database engine and session,
        creating tables if needed.
        """
        engine = create_engine(self._db_config())
        Base.metadata.bind = engine
        Session.configure(bind=engine)
        self.session = Session()
        self.serializer = Serializer('text')

        # turn on foreign keys for sqlite (the default engine)
        if 'sqlite' in self.store_type:
            @event.listens_for(Engine, 'connect')
            def set_sqlite_pragma(dbapi_connection, connection_record):
                cursor = dbapi_connection.cursor()
                cursor.execute('PRAGMA foreign_keys=ON')
                cursor.close()

        if not Store.mapped:<commit_msg>Get rid of unused text serializer<commit_after>"""
The base for using sqlalchemy as a store with TiddlyWeb.
"""

from __future__ import absolute_import

import logging

from pyparsing import ParseException

from base64 import b64encode, b64decode
from sqlalchemy import event
from sqlalchemy.engine import create_engine, Engine
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql.expression import and_

from tiddlyweb.filters import FilterIndexRefused
from tiddlyweb.model.bag import Bag
from tiddlyweb.model.policy import Policy
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.model.user import User
from tiddlyweb.store import (NoBagError, NoRecipeError, NoTiddlerError,
        NoUserError, StoreError)
from tiddlyweb.stores import StorageInterface
from tiddlyweb.util import binary_tiddler

from .model import (Base, Session, sBag, sPolicy, sRecipe, sTiddler, sRevision,
        sText, sTag, sField, sCurrentRevision, sFirstRevision, sUser, sRole)
from .parser import DEFAULT_PARSER
from .producer import Producer

__version__ = '3.0.16'

#logging.basicConfig()
#logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
#logging.getLogger('sqlalchemy.pool').setLevel(logging.DEBUG)


class Store(StorageInterface):
    """
    A SqlAlchemy based storage interface for TiddlyWeb.
    """

    mapped = False

    def __init__(self, store_config=None, environ=None):
        super(Store, self).__init__(store_config, environ)
        self.store_type = self._db_config().split(':', 1)[0]
        self.parser = DEFAULT_PARSER
        self.producer = Producer()
        self.has_geo = False
        self._init_store()

    def _init_store(self):
        """
        Establish the database engine and session,
        creating tables if needed.
        """
        engine = create_engine(self._db_config())
        Base.metadata.bind = engine
        Session.configure(bind=engine)
        self.session = Session()

        # turn on foreign keys for sqlite (the default engine)
        if 'sqlite' in self.store_type:
            @event.listens_for(Engine, 'connect')
            def set_sqlite_pragma(dbapi_connection, connection_record):
                cursor = dbapi_connection.cursor()
                cursor.execute('PRAGMA foreign_keys=ON')
                cursor.close()

        if not Store.mapped:
{'fraction_non_alphanumeric': 0.0819938962360122, 'fraction_numerical': 0.0048830111902339775, 'mean_word_length': 3.5901027077497667, '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': '2388257', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1438, 'n_words': 390}