How to use the aiopg.utils._ContextManager function in aiopg

To help you get started, we’ve selected a few aiopg examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aio-libs / aiopg / aiopg / utils.py View on Github external
await self._obj.wait_closed()
        self._obj = None


class _TransactionPointContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback_savepoint()
        else:
            await self._obj.release_savepoint()

        self._obj = None


class _TransactionBeginContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback()
        else:
            await self._obj.commit()

        self._obj = None


class _TransactionContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc, tb):
        if exc_type:
            await self._obj.rollback()
        else:
github aio-libs / aiopg / aiopg / utils.py View on Github external
self._obj = None


class _TransactionContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc, tb):
        if exc_type:
            await self._obj.rollback()
        else:
            if self._obj.is_active:
                await self._obj.commit()
        self._obj = None


class _PoolAcquireContextManager(_ContextManager):
    __slots__ = ('_coro', '_obj', '_pool')

    def __init__(self, coro, pool):
        super().__init__(coro)
        self._pool = pool

    async def __aexit__(self, exc_type, exc, tb):
        await self._pool.release(self._obj)
        self._pool = None
        self._obj = None


class _PoolConnectionContextManager:
    """Context manager.

    This enables the following idiom for acquiring and releasing a
github aio-libs / aiopg / aiopg / utils.py View on Github external
self._obj = None


class _TransactionBeginContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback()
        else:
            await self._obj.commit()

        self._obj = None


class _TransactionContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc, tb):
        if exc_type:
            await self._obj.rollback()
        else:
            if self._obj.is_active:
                await self._obj.commit()
        self._obj = None


class _PoolAcquireContextManager(_ContextManager):
    __slots__ = ('_coro', '_obj', '_pool')

    def __init__(self, coro, pool):
        super().__init__(coro)
        self._pool = pool
github aio-libs / aiopg / aiopg / utils.py View on Github external
return self.send(None)

    def __await__(self):
        resp = self._coro.__await__()
        return resp

    async def __aenter__(self):
        self._obj = await self._coro
        return self._obj

    async def __aexit__(self, exc_type, exc, tb):
        self._obj.close()
        self._obj = None


class _SAConnectionContextManager(_ContextManager):
    def __aiter__(self):
        return self

    async def __anext__(self):
        if self._obj is None:
            self._obj = await self._coro

        try:
            return (await self._obj.__anext__())
        except StopAsyncIteration:
            self._obj.close()
            self._obj = None
            raise


class _PoolContextManager(_ContextManager):
github aio-libs / aiopg / aiopg / utils.py View on Github external
try:
            return (await self._obj.__anext__())
        except StopAsyncIteration:
            self._obj.close()
            self._obj = None
            raise


class _PoolContextManager(_ContextManager):
    async def __aexit__(self, exc_type, exc, tb):
        self._obj.close()
        await self._obj.wait_closed()
        self._obj = None


class _TransactionPointContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback_savepoint()
        else:
            await self._obj.release_savepoint()

        self._obj = None


class _TransactionBeginContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback()
        else:
github aio-libs / aiopg / aiopg / utils.py View on Github external
def __aiter__(self):
        return self

    async def __anext__(self):
        if self._obj is None:
            self._obj = await self._coro

        try:
            return (await self._obj.__anext__())
        except StopAsyncIteration:
            self._obj.close()
            self._obj = None
            raise


class _PoolContextManager(_ContextManager):
    async def __aexit__(self, exc_type, exc, tb):
        self._obj.close()
        await self._obj.wait_closed()
        self._obj = None


class _TransactionPointContextManager(_ContextManager):

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            await self._obj.rollback_savepoint()
        else:
            await self._obj.release_savepoint()

        self._obj = None
github aio-libs / aiopg / aiopg / connection.py View on Github external
*cursor_factory* argument can be used to create non-standard
         cursors. The argument must be subclass of
         `psycopg2.extensions.cursor`.

        *name*, *scrollable* and *withhold* parameters are not supported by
        psycopg in asynchronous mode.

        NOTE: as of [TODO] any previously created created cursor from this
            connection will be closed
        """
        self._last_usage = self._loop.time()
        core = self._cursor(name=name, cursor_factory=cursor_factory,
                            scrollable=scrollable, withhold=withhold,
                            timeout=timeout)
        return _ContextManager(core)
github aio-libs / aiopg / aiopg / connection.py View on Github external
def connect(dsn=None, *, timeout=TIMEOUT, loop=None, enable_json=True,
            enable_hstore=True, enable_uuid=True, echo=False, **kwargs):
    """A factory for connecting to PostgreSQL.

    The coroutine accepts all parameters that psycopg2.connect() does
    plus optional keyword-only `loop` and `timeout` parameters.

    Returns instantiated Connection object.

    """
    coro = _connect(dsn=dsn, timeout=timeout, loop=loop,
                    enable_json=enable_json, enable_hstore=enable_hstore,
                    enable_uuid=enable_uuid, echo=echo, **kwargs)
    return _ContextManager(coro)
github DataDog / dd-trace-py / ddtrace / contrib / aiopg / connection.py View on Github external
def cursor(self, *args, **kwargs):
        # unfortunately we also need to patch this method as otherwise "self"
        # ends up being the aiopg connection object
        coro = self._cursor(*args, **kwargs)
        return _ContextManager(coro)