lazy plugin constructor

This commit is contained in:
ThomasV 2015-05-24 20:37:05 +02:00
parent 71046371ec
commit 778297697a
6 changed files with 25 additions and 19 deletions

View File

@ -235,6 +235,7 @@ class ElectrumWindow(QMainWindow):
self.clear_receive_tab() self.clear_receive_tab()
self.update_receive_tab() self.update_receive_tab()
self.show() self.show()
run_hook('init_qt', self.gui_object)
run_hook('load_wallet', wallet) run_hook('load_wallet', wallet)
def import_old_contacts(self): def import_old_contacts(self):

View File

@ -60,15 +60,18 @@ def init_plugins(config, is_local, gui_name):
electrum_plugins = __import__('electrum_plugins') electrum_plugins = __import__('electrum_plugins')
loader = lambda name: __import__('electrum_plugins.' + name, fromlist=['electrum_plugins']) loader = lambda name: __import__('electrum_plugins.' + name, fromlist=['electrum_plugins'])
def register_wallet_type(name): def constructor(name, storage):
# fixme: load plugins only if really needed if plugins.get(name) is None:
import wallet
try: try:
p = loader(name) p = loader(name)
plugins[name] = p.Plugin(config, name) plugins[name] = p.Plugin(config, name)
except: except:
return return
x = plugins[name].get_wallet_type() return plugins[name].constructor(storage)
def register_wallet_type(name, x, constructor):
import wallet
x += (lambda storage: constructor(name, storage),)
wallet.wallet_types.append(x) wallet.wallet_types.append(x)
descriptions = electrum_plugins.descriptions descriptions = electrum_plugins.descriptions
@ -76,8 +79,9 @@ def init_plugins(config, is_local, gui_name):
name = item['name'] name = item['name']
if gui_name not in item.get('available_for', []): if gui_name not in item.get('available_for', []):
continue continue
if item.get('registers_wallet_type'): x = item.get('registers_wallet_type')
register_wallet_type(name) if x:
register_wallet_type(name, x, constructor)
if not config.get('use_' + name): if not config.get('use_' + name):
continue continue
try: try:
@ -87,6 +91,7 @@ def init_plugins(config, is_local, gui_name):
print_msg(_("Error: cannot initialize plugin"), name) print_msg(_("Error: cannot initialize plugin"), name)
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
hook_names = set() hook_names = set()
hooks = {} hooks = {}

View File

@ -32,7 +32,7 @@ descriptions = [
'description': _('Provides support for BTChip hardware wallet'), 'description': _('Provides support for BTChip hardware wallet'),
'requires': [('btchip', 'github.com/btchip/btchip-python')], 'requires': [('btchip', 'github.com/btchip/btchip-python')],
'requires_wallet_type': ['btchip'], 'requires_wallet_type': ['btchip'],
'registers_wallet_type': True, 'registers_wallet_type': ('hardware', 'btchip', _("BTChip wallet")),
'available_for': ['qt'], 'available_for': ['qt'],
}, },
{ {
@ -88,7 +88,7 @@ descriptions = [
'available_for': ['qt'], 'available_for': ['qt'],
'requires': [('trezorlib','github.com/trezor/python-trezor')], 'requires': [('trezorlib','github.com/trezor/python-trezor')],
'requires_wallet_type': ['trezor'], 'requires_wallet_type': ['trezor'],
'registers_wallet_type': True, 'registers_wallet_type': ('hardware', 'trezor', _("Trezor wallet")),
'available_for': ['qt', 'cmdline'], 'available_for': ['qt', 'cmdline'],
}, },
{ {
@ -100,7 +100,7 @@ descriptions = [
" <a href=\"https://api.trustedcoin.com/#/electrum-help\">https://api.trustedcoin.com/#/electrum-help</a>" " <a href=\"https://api.trustedcoin.com/#/electrum-help\">https://api.trustedcoin.com/#/electrum-help</a>"
]), ]),
'requires_wallet_type': ['2fa'], 'requires_wallet_type': ['2fa'],
'registers_wallet_type': True, 'registers_wallet_type': ('twofactor', '2fa', _("Wallet with two-factor authentication")),
'available_for': ['qt', 'cmdline'], 'available_for': ['qt', 'cmdline'],
}, },
{ {

View File

@ -40,8 +40,8 @@ class Plugin(BasePlugin):
self._is_available = self._init() self._is_available = self._init()
self.wallet = None self.wallet = None
def get_wallet_type(self): def constructor(self, s):
return ('hardware', 'btchip', _("BTChip wallet"), BTChipWallet) return BTChipWallet(s)
def _init(self): def _init(self):
return BTCHIP return BTCHIP

View File

@ -47,8 +47,8 @@ class Plugin(BasePlugin):
self._requires_settings = True self._requires_settings = True
self.wallet = None self.wallet = None
def get_wallet_type(self): def constructor(self, s):
return ('hardware', 'trezor', _("Trezor wallet"), TrezorWallet) return TrezorWallet(s)
def _init(self): def _init(self):
return TREZOR return TREZOR

View File

@ -214,8 +214,8 @@ class Plugin(BasePlugin):
self.billing_info = None self.billing_info = None
self.is_billing = False self.is_billing = False
def get_wallet_type(self): def constructor(self, s):
return ('twofactor', '2fa', _("Wallet with two-factor authentication"), Wallet_2fa) return Wallet_2fa(s)
def is_available(self): def is_available(self):
if not self.wallet: if not self.wallet: