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.update_receive_tab()
self.show()
run_hook('init_qt', self.gui_object)
run_hook('load_wallet', wallet)
def import_old_contacts(self):

View File

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

View File

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

View File

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

View File

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

View File

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