Skip to content
Snippets Groups Projects
Commit 90642d4d authored by Jakub Ziebinski's avatar Jakub Ziebinski
Browse files

Implementation of the beekeepy objects to make it easier to create a beekeeper set

parent de324540
No related branches found
No related tags found
No related merge requests found
Pipeline #118245 passed
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",
]
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)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment