From 773c835cfee182520a536b9619702b6ad52bc2a6 Mon Sep 17 00:00:00 2001 From: kmochocki <kmochocki@syncad.com> Date: Thu, 6 Feb 2025 13:53:36 +0000 Subject: [PATCH] Add is_unlocked to wallet interface --- .../_interface/abc/asynchronous/wallet.py | 3 +++ .../_interface/abc/synchronous/wallet.py | 3 +++ .../beekeepy/_interface/asynchronous/session.py | 10 ++++------ .../beekeepy/_interface/asynchronous/wallet.py | 16 +++++++--------- beekeepy/beekeepy/_interface/common.py | 12 +++++++----- .../beekeepy/_interface/synchronous/session.py | 8 ++++---- .../beekeepy/_interface/synchronous/wallet.py | 16 +++++++--------- 7 files changed, 35 insertions(+), 33 deletions(-) diff --git a/beekeepy/beekeepy/_interface/abc/asynchronous/wallet.py b/beekeepy/beekeepy/_interface/abc/asynchronous/wallet.py index 6f536f2a..4fcd73fe 100644 --- a/beekeepy/beekeepy/_interface/abc/asynchronous/wallet.py +++ b/beekeepy/beekeepy/_interface/abc/asynchronous/wallet.py @@ -22,6 +22,9 @@ class Wallet(ContainsWalletName, ABC): @abstractmethod async def unlocked(self) -> UnlockedWallet | None: ... + @abstractmethod + async def is_unlocked(self) -> bool: ... + @abstractmethod async def unlock(self, password: str) -> UnlockedWallet: ... diff --git a/beekeepy/beekeepy/_interface/abc/synchronous/wallet.py b/beekeepy/beekeepy/_interface/abc/synchronous/wallet.py index 6ea8292e..91a835c7 100644 --- a/beekeepy/beekeepy/_interface/abc/synchronous/wallet.py +++ b/beekeepy/beekeepy/_interface/abc/synchronous/wallet.py @@ -25,6 +25,9 @@ class Wallet(ContainsWalletName, ABC): @abstractmethod def unlock(self, password: str) -> UnlockedWallet: ... + @abstractmethod + def is_unlocked(self) -> bool: ... + @property @abstractmethod def name(self) -> str: ... diff --git a/beekeepy/beekeepy/_interface/asynchronous/session.py b/beekeepy/beekeepy/_interface/asynchronous/session.py index e245b3f8..e898d0d9 100644 --- a/beekeepy/beekeepy/_interface/asynchronous/session.py +++ b/beekeepy/beekeepy/_interface/asynchronous/session.py @@ -97,7 +97,7 @@ class Session(SessionInterface, StateInvalidator): @property async def wallets_unlocked(self) -> list[UnlockedWalletInterface]: result = [] - for wallet in await self.__list_wallets(refresh_timeout=False): + for wallet in await self.__list_wallets(): unlocked_wallet = await wallet.unlocked if unlocked_wallet: result.append(unlocked_wallet) @@ -111,7 +111,7 @@ class Session(SessionInterface, StateInvalidator): @property async def wallets(self) -> list[WalletInterface]: - return await self.__list_wallets(refresh_timeout=True) + return await self.__list_wallets() @property async def wallets_created(self) -> list[WalletInterface]: @@ -134,13 +134,11 @@ class Session(SessionInterface, StateInvalidator): self.register_invalidable(wallet) return wallet - async def __list_wallets(self, *, refresh_timeout: bool) -> list[WalletInterface]: + async def __list_wallets(self) -> list[WalletInterface]: return await asyncio.gather( *[ self.__construct_wallet(name=wallet.name) - for wallet in ( - await self.__beekeeper.api.list_wallets(token=await self.token, refresh_timeout=refresh_timeout) - ).wallets + for wallet in (await self.__beekeeper.api.list_wallets(token=await self.token)).wallets ] ) diff --git a/beekeepy/beekeepy/_interface/asynchronous/wallet.py b/beekeepy/beekeepy/_interface/asynchronous/wallet.py index 6092dd8a..914fe5fe 100644 --- a/beekeepy/beekeepy/_interface/asynchronous/wallet.py +++ b/beekeepy/beekeepy/_interface/asynchronous/wallet.py @@ -40,12 +40,12 @@ class Wallet(WalletCommons[AsyncRemoteBeekeeper, AsyncWalletLocked, AsyncDelayGu @property async def unlocked(self) -> UnlockedWallet | None: - if await self.__is_unlocked(): + if await self.is_unlocked(): return self.__construct_unlocked_wallet() return None async def unlock(self, password: str) -> UnlockedWallet: - if not (await self.__is_unlocked()): + if not (await self.is_unlocked()): first_try = True while first_try or self._guard.error_occured(): first_try = False @@ -56,13 +56,11 @@ class Wallet(WalletCommons[AsyncRemoteBeekeeper, AsyncWalletLocked, AsyncDelayGu ) return self.__construct_unlocked_wallet() - async def __is_unlocked(self) -> bool: - for wallet in (await self._beekeeper.api.list_wallets(token=self.session_token)).wallets: - if wallet.name == self.name: - self._last_lock_state = wallet.unlocked - return self._last_lock_state - self._last_lock_state = False - return self._last_lock_state + async def is_unlocked(self) -> bool: + return self._is_wallet_unlocked( + wallet_name=self.name, + wallets=(await self._beekeeper.api.list_wallets(token=self.session_token)).wallets, + ) def __construct_unlocked_wallet(self) -> UnlockedWallet: wallet = UnlockedWallet( diff --git a/beekeepy/beekeepy/_interface/common.py b/beekeepy/beekeepy/_interface/common.py index e45b8726..cac4a233 100644 --- a/beekeepy/beekeepy/_interface/common.py +++ b/beekeepy/beekeepy/_interface/common.py @@ -70,7 +70,7 @@ class WalletCommons(ContainsWalletName, StateInvalidator, Generic[BeekeeperT, Ca def _last_lock_state(self, value: bool) -> None: self.__last_check_is_locked = value - def _is_wallet_locked(self, *, wallet_name: str, wallets: list[WalletDetails]) -> bool: + def _is_wallet_unlocked(self, *, wallet_name: str, wallets: list[WalletDetails]) -> bool: """Checks is wallet locked. Args: @@ -82,8 +82,10 @@ class WalletCommons(ContainsWalletName, StateInvalidator, Generic[BeekeeperT, Ca """ for wallet in wallets: if wallet.name == wallet_name: - return not wallet.unlocked - return True + self._last_lock_state = wallet.unlocked + return wallet.unlocked + self._last_lock_state = False + return False def _raise_wallet_is_locked_error(self, wallet_name: str) -> NoReturn: raise WalletIsLockedError(wallet_name=wallet_name) @@ -91,7 +93,7 @@ class WalletCommons(ContainsWalletName, StateInvalidator, Generic[BeekeeperT, Ca async def _async_call_callback_if_locked(self, *, wallet_name: str, token: str) -> None: assert isinstance(self._beekeeper, AsyncRemoteBeekeeper), "invalid beekeeper type, require asynchronous" wallets = (await self._beekeeper.api.list_wallets(token=token)).wallets - if self._is_wallet_locked(wallet_name=wallet_name, wallets=wallets): + if not self._is_wallet_unlocked(wallet_name=wallet_name, wallets=wallets): if self._last_lock_state is False: wallet_names = [w.name for w in wallets if w.unlocked is False] await asyncio.gather( @@ -106,7 +108,7 @@ class WalletCommons(ContainsWalletName, StateInvalidator, Generic[BeekeeperT, Ca def _sync_call_callback_if_locked(self, *, wallet_name: str, token: str) -> None: assert isinstance(self._beekeeper, SyncRemoteBeekeeper), "invalid beekeeper type, require synchronous" wallets = self._beekeeper.api.list_wallets(token=token).wallets - if self._is_wallet_locked(wallet_name=wallet_name, wallets=wallets): + if not self._is_wallet_unlocked(wallet_name=wallet_name, wallets=wallets): if self._last_lock_state is False: wallet_names = [w.name for w in wallets if w.unlocked is False] for callback in self.__wallet_close_callbacks: diff --git a/beekeepy/beekeepy/_interface/synchronous/session.py b/beekeepy/beekeepy/_interface/synchronous/session.py index 0713023b..92379374 100644 --- a/beekeepy/beekeepy/_interface/synchronous/session.py +++ b/beekeepy/beekeepy/_interface/synchronous/session.py @@ -87,7 +87,7 @@ class Session(SessionInterface, StateInvalidator): @property def wallets_unlocked(self) -> list[UnlockedWalletInterface]: - return [wallet.unlocked for wallet in self.__list_wallets(refresh_timeout=False) if wallet.unlocked] + return [wallet.unlocked for wallet in self.__list_wallets() if wallet.unlocked] @property def token(self) -> str: @@ -97,7 +97,7 @@ class Session(SessionInterface, StateInvalidator): @property def wallets(self) -> list[WalletInterface]: - return self.__list_wallets(refresh_timeout=True) + return self.__list_wallets() @property def wallets_created(self) -> list[WalletInterface]: @@ -120,10 +120,10 @@ class Session(SessionInterface, StateInvalidator): self.register_invalidable(wallet) return wallet - def __list_wallets(self, *, refresh_timeout: bool) -> list[WalletInterface]: + def __list_wallets(self) -> list[WalletInterface]: return [ self.__construct_wallet(name=wallet.name) - for wallet in self.__beekeeper.api.list_wallets(token=self.token, refresh_timeout=refresh_timeout).wallets + for wallet in self.__beekeeper.api.list_wallets(token=self.token).wallets ] def _enter(self) -> SessionInterface: diff --git a/beekeepy/beekeepy/_interface/synchronous/wallet.py b/beekeepy/beekeepy/_interface/synchronous/wallet.py index ccdfc817..38347828 100644 --- a/beekeepy/beekeepy/_interface/synchronous/wallet.py +++ b/beekeepy/beekeepy/_interface/synchronous/wallet.py @@ -40,12 +40,12 @@ class Wallet(WalletCommons[SyncRemoteBeekeeper, SyncWalletLocked, SyncDelayGuard @property def unlocked(self) -> UnlockedWallet | None: - if self.__is_unlocked(): + if self.is_unlocked(): return self.__construct_unlocked_wallet() return None def unlock(self, password: str) -> UnlockedWallet: - if not self.__is_unlocked(): + if not self.is_unlocked(): first_try = True while first_try or self._guard.error_occured(): first_try = False @@ -53,13 +53,11 @@ class Wallet(WalletCommons[SyncRemoteBeekeeper, SyncWalletLocked, SyncDelayGuard self._beekeeper.api.unlock(wallet_name=self.name, password=password, token=self.session_token) return self.__construct_unlocked_wallet() - def __is_unlocked(self) -> bool: - for wallet in self._beekeeper.api.list_wallets(token=self.session_token).wallets: - if wallet.name == self.name: - self._last_lock_state = wallet.unlocked - return self._last_lock_state - self._last_lock_state = False - return self._last_lock_state + def is_unlocked(self) -> bool: + return self._is_wallet_unlocked( + wallet_name=self.name, + wallets=(self._beekeeper.api.list_wallets(token=self.session_token)).wallets, + ) def __construct_unlocked_wallet(self) -> UnlockedWallet: wallet = UnlockedWallet( -- GitLab