lbry-sdk/torba/client/basemanager.py

81 lines
2.5 KiB
Python
Raw Normal View History

2018-10-14 22:16:51 -04:00
import asyncio
import logging
from typing import Type, MutableSequence, MutableMapping
2018-05-25 02:03:25 -04:00
2018-11-04 01:55:50 -04:00
from torba.client.baseledger import BaseLedger, LedgerRegistry
from torba.client.wallet import Wallet, WalletStorage
2018-05-25 02:03:25 -04:00
log = logging.getLogger(__name__)
2018-05-25 02:03:25 -04:00
class BaseWalletManager:
2018-05-25 02:03:25 -04:00
def __init__(self, wallets: MutableSequence[Wallet] = None,
ledgers: MutableMapping[Type[BaseLedger], BaseLedger] = None) -> None:
2018-06-14 15:17:59 -04:00
self.wallets = wallets or []
self.ledgers = ledgers or {}
2018-05-25 02:03:25 -04:00
self.running = False
@classmethod
def from_config(cls, config: dict) -> 'BaseWalletManager':
2018-07-01 17:20:17 -04:00
manager = cls()
for ledger_id, ledger_config in config.get('ledgers', {}).items():
manager.get_or_create_ledger(ledger_id, ledger_config)
2018-05-25 02:03:25 -04:00
for wallet_path in config.get('wallets', []):
wallet_storage = WalletStorage(wallet_path)
wallet = Wallet.from_storage(wallet_storage, manager)
2018-07-01 17:20:17 -04:00
manager.wallets.append(wallet)
2018-05-25 02:03:25 -04:00
return manager
2018-06-12 10:02:04 -04:00
def get_or_create_ledger(self, ledger_id, ledger_config=None):
ledger_class = LedgerRegistry.get_ledger_class(ledger_id)
2018-05-25 02:03:25 -04:00
ledger = self.ledgers.get(ledger_class)
if ledger is None:
2018-06-14 15:17:59 -04:00
ledger = ledger_class(ledger_config or {})
2018-05-25 02:03:25 -04:00
self.ledgers[ledger_class] = ledger
return ledger
2018-07-04 21:17:45 -04:00
def import_wallet(self, path):
2018-06-14 15:17:59 -04:00
storage = WalletStorage(path)
wallet = Wallet.from_storage(storage, self)
self.wallets.append(wallet)
return wallet
2018-06-07 23:47:46 -04:00
2018-10-17 10:57:24 -04:00
async def get_detailed_accounts(self, **kwargs):
2018-08-30 11:50:11 -04:00
ledgers = {}
for i, account in enumerate(self.accounts):
2018-10-17 10:57:24 -04:00
details = await account.get_details(**kwargs)
details['is_default'] = i == 0
2018-08-30 11:50:11 -04:00
ledger_id = account.ledger.get_id()
ledgers.setdefault(ledger_id, [])
ledgers[ledger_id].append(details)
return ledgers
2018-06-07 23:47:46 -04:00
2018-05-25 02:03:25 -04:00
@property
def default_wallet(self):
for wallet in self.wallets:
return wallet
@property
def default_account(self):
for wallet in self.wallets:
return wallet.default_account
2018-07-16 23:58:29 -04:00
@property
def accounts(self):
for wallet in self.wallets:
for account in wallet.accounts:
yield account
2018-10-14 22:16:51 -04:00
async def start(self):
2018-05-25 02:03:25 -04:00
self.running = True
2018-10-14 22:16:51 -04:00
await asyncio.gather(*(
2018-05-25 02:03:25 -04:00
l.start() for l in self.ledgers.values()
2018-10-14 22:16:51 -04:00
))
2018-05-25 02:03:25 -04:00
2018-10-14 22:16:51 -04:00
async def stop(self):
await asyncio.gather(*(
2018-05-25 02:03:25 -04:00
l.stop() for l in self.ledgers.values()
2018-10-14 22:16:51 -04:00
))
2018-05-25 02:03:25 -04:00
self.running = False