예제 #1
0
 def init_app(self, app):
     app.config.setdefault('DBKIT_POOL_SIZE', 5)
     conn_kwargs = self._get_conn_args(app.config)
     self.mdr_kwargs.update(conn_kwargs)
     pool = dbkit.create_pool(
         self.module,
         app.config['DBKIT_POOL_SIZE'],
         *self.mdr_args,
         **self.mdr_kwargs)
     pool.default_factory = self.default_factory
     app.wsgi_app = _DBKitMiddleware(app, pool)
예제 #2
0
    def test_setting_propagation(self):
        pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
        try:
            self.assertTrue(pool.default_factory is dbkit.TupleFactory)
            self.assertTrue(pool.logger is dbkit.null_logger)
            with pool.connect() as ctx:
                self.assertTrue(ctx.default_factory is dbkit.TupleFactory)
                self.assertTrue(ctx.logger is dbkit.null_logger)
        finally:
            pool.finalise()

        pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
        try:
            self.assertTrue(pool.default_factory is dbkit.TupleFactory)
            self.assertTrue(pool.logger is dbkit.null_logger)
            pool.default_factory = None
            pool.logger = None
            with pool.connect() as ctx:
                self.assertTrue(ctx.default_factory is None)
                self.assertTrue(ctx.logger is None)
        finally:
            pool.finalise()
예제 #3
0
def test_setting_propagation():
    pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
    try:
        assert pool.default_factory is dbkit.TupleFactory
        assert pool.logger is dbkit.null_logger
        with pool.connect() as ctx:
            assert ctx.default_factory is dbkit.TupleFactory
            assert ctx.logger is dbkit.null_logger
    finally:
        pool.finalise()

    pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
    try:
        assert pool.default_factory is dbkit.TupleFactory
        assert pool.logger is dbkit.null_logger
        pool.default_factory = None
        pool.logger = None
        with pool.connect() as ctx:
            assert ctx.default_factory is None
            assert ctx.logger is None
    finally:
        pool.finalise()
예제 #4
0
def configure_db_hook(app, global_config, settings):
    """
    Ensure each request is done in a properly configured database context.
    """
    path = settings.get('db_path', '%(here)s/pastetron.db') % global_config
    pool = dbkit.create_pool(sqlite3, 10, path)
    pool.default_factory = dbkit.dict_set

    def request_processor(handler):
        """
        Do anything that needs to be done at the beginning and end of a
        request here.
        """
        with pool.connect():
            return handler()
    app.add_processor(request_processor)
예제 #5
0
    def test_pool_contention(self):
        pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
        # Here, we're testing that the pool behaves properly when it hits its
        # maximum number of connections and a thread it waiting for another one
        # to release the connection it's currently using.
        release = threading.Event()
        spawn = threading.Event()

        def hog_connection():
            with pool.connect():
                with dbkit.transaction():
                    spawn.set()
                    release.wait()

        def wait_on_connection():
            with pool.connect():
                spawn.wait()
                # Request the other thread to release the connection after a
                # short period, enough to ensure the conditional variable
                # managing the pool is waited on by this thread. Basically
                # nearly any pause should be long enough, though 1/100 of a
                # second seems like a reasonable balance.
                #
                # We do this because we want to deterministically introduce a
                # wait on the condition variable that signals when there's a
                # free connection. In normal operation, this happens in a
                # nondeterministic manner. This pause and the use of the
                # release and spawn events ensure that the threads proceed in
                # lockstep to produce the behaviour we need to set.
                threading.Timer(1.0 / 100, lambda: release.set()).start()
                with dbkit.transaction():
                    pass

        utils.spawn([wait_on_connection, hog_connection])

        self.assertEqual(pool._allocated, 1)
        self.assertEqual(len(pool._pool), 1)
        pool.finalise()
        self.assertEqual(pool._allocated, 0)
        self.assertEqual(len(pool._pool), 0)
예제 #6
0
def initialise(app, global_config=None, **settings):
    """
    Initialise a web.py application, returning a WSGI application.
    """
    if global_config is None:
        global_config = {
            'here': '.',
        }

    # Defaults.
    settings.setdefault('db_path', '%(here)s/pastetron.db' % global_config)
    settings.setdefault('auth_method', 'pastetron.httpauth:DUMMY')
    settings.setdefault('auth_realm', 'Pastetron')

    # Allow the application access to the configuration.
    web.config.app = web.Storage(**settings)

    # Ensure each request is done in a properly configured database context.
    pool = dbkit.create_pool(sqlite3, 10, settings['db_path'])
    pool.default_factory = dbkit.dict_set

    def request_processor(handler):
        """
        Do anything that needs to be done at the beginning and end of a
        request here.
        """
        with pool.connect():
            return handler()

    app.add_processor(request_processor)

    # Authentication.
    views.auth.method = utils.load_object(settings['auth_method'])
    views.auth.realm = settings['auth_realm']

    return app.wsgifunc()
예제 #7
0
파일: notary.py 프로젝트: justinfay/dbkit
import creole
import dbkit
import web


urls = (
    '/', 'Frontpage',
    '/([-a-z0-9]+)/', 'Project',
)

app = web.application(urls, globals())
render = web.template.render('templates', base='layout', globals={
    'creole2html': creole.creole2html
})
pool = dbkit.create_pool(sqlite3, 10, "notary.db")
pool.default_factory = dbkit.dict_set


def strip_accents(s):
    """
    Strip accents to prepare for slugification.
    """
    nfkd = unicodedata.normalize('NFKD', unicode(s))
    return u''.join(ch for ch in nfkd if not unicodedata.combining(ch))


def slugify(s):
    """
    Converts the given string to a URL slug.
    """
예제 #8
0
 def setUp(self):
     self.pool = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)
예제 #9
0
from __future__ import with_statement
import threading

import dbkit
from tests import fakedb, utils


POOL = dbkit.create_pool(fakedb, 1, fakedb.INVALID_CURSOR)


def test_check_pool():
    assert isinstance(POOL, dbkit.Pool)
    assert POOL.module is fakedb
    assert POOL.OperationalError is fakedb.OperationalError
    assert POOL._allocated == 0
    assert len(POOL._pool) == 0


def test_lazy_connect():
    assert len(POOL._pool) == 0
    with POOL.connect() as ctx:
        assert isinstance(ctx.mdr, dbkit.PooledConnectionMediator)
        assert POOL._allocated == 0
        assert len(POOL._pool) == 0
    assert POOL._allocated == 0
    assert len(POOL._pool) == 0


def test_real_connect():
    with POOL.connect():
        with dbkit.transaction():