summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/asyncio/base.py
blob: 051f9e21a1f420101115302ad07e0edae7742bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import abc

from . import exc as async_exc


class StartableContext(abc.ABC):
    @abc.abstractmethod
    async def start(self) -> "StartableContext":
        pass

    def __await__(self):
        return self.start().__await__()

    async def __aenter__(self):
        return await self.start()

    @abc.abstractmethod
    async def __aexit__(self, type_, value, traceback):
        pass

    def _raise_for_not_started(self):
        raise async_exc.AsyncContextNotStarted(
            "%s context has not been started and object has not been awaited."
            % (self.__class__.__name__)
        )