diff --git a/beekeepy/beekeepy/objects/__init__.py b/beekeepy/beekeepy/objects/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..7a6390c3b49ff288751e8771894fb00d5a7b2a87 --- /dev/null +++ b/beekeepy/beekeepy/objects/__init__.py @@ -0,0 +1,12 @@ +from __future__ import annotations + +from .asynchronous import BeekeepyObjects as AsyncBeekeepyObjects +from .asynchronous import create_beekeeper_set as create_async_beekeeper_set +from .synchronous import BeekeepyObjects, create_beekeeper_set + +__all__ = [ + "AsyncBeekeepyObjects", + "create_async_beekeeper_set", + "BeekeepyObjects", + "create_beekeeper_set", +] diff --git a/beekeepy/beekeepy/objects/asynchronous.py b/beekeepy/beekeepy/objects/asynchronous.py new file mode 100644 index 0000000000000000000000000000000000000000..72d35d074d93bb4c741be955f88a6b578cf2d967 --- /dev/null +++ b/beekeepy/beekeepy/objects/asynchronous.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from contextlib import asynccontextmanager +from dataclasses import dataclass +from typing import AsyncGenerator + +from beekeepy import AsyncBeekeeper, AsyncSession, AsyncUnlockedWallet + + +@dataclass +class BeekeepyObjects: + wallet: AsyncUnlockedWallet + session: AsyncSession + beekeeper: AsyncBeekeeper + + +@asynccontextmanager +async def create_beekeeper_set(*, wallet_name: str, password: str) -> AsyncGenerator[BeekeepyObjects, None]: + """ + Create a beekeeper set with a wallet and session. + + This context manager handle the creation of a wallet and session and running beekeeper instance. + If you already have a wallet with the same name, it will unlock it. + + Args: + wallet_name (str): The wallet to create/unlock name. + password (str): The wallet to create/unlock password. + """ + async with await AsyncBeekeeper.factory() as beekeeper, await beekeeper.create_session() as session: + wallet = ( + await session.create_wallet(name=wallet_name, password=password) + if wallet_name not in [w.name for w in await session.wallets_created] + else await (await session.open_wallet(name=wallet_name)).unlock(password=password) + ) + + yield BeekeepyObjects(wallet=wallet, session=session, beekeeper=beekeeper) diff --git a/beekeepy/beekeepy/objects/synchronous.py b/beekeepy/beekeepy/objects/synchronous.py new file mode 100644 index 0000000000000000000000000000000000000000..ea1c634bc7d1c19202016d9a5e3031ac1163580b --- /dev/null +++ b/beekeepy/beekeepy/objects/synchronous.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from contextlib import contextmanager +from dataclasses import dataclass +from typing import Generator + +from beekeepy import Beekeeper, Session, UnlockedWallet + + +@dataclass +class BeekeepyObjects: + wallet: UnlockedWallet + session: Session + beekeeper: Beekeeper + + +@contextmanager +def create_beekeeper_set(*, wallet_name: str, password: str) -> Generator[BeekeepyObjects, None, None]: + """ + Create a beekeeper set with a wallet and session. + + This context manager handle the creation of a wallet and session and running beekeeper instance. + If you already have a wallet with the same name, it will unlock it. + + Args: + wallet_name (str): The wallet to create/unlock name. + password (str): The wallet to create/unlock password. + """ + with Beekeeper.factory() as beekeeper, beekeeper.create_session() as session: + wallet = ( + session.create_wallet(name=wallet_name, password=password) + if wallet_name not in [w.name for w in session.wallets_created] + else (session.open_wallet(name=wallet_name)).unlock(password=password) + ) + + yield BeekeepyObjects(wallet=wallet, session=session, beekeeper=beekeeper)