electrum-bitcoinprivate/lib/plugins.py

187 lines
5.0 KiB
Python
Raw Normal View History

2015-05-23 01:38:19 -07:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import traceback
import sys
import os
import imp
import pkgutil
2013-09-27 23:08:36 -07:00
from util import *
from i18n import _
2015-05-24 00:26:28 -07:00
from util import print_error, profiler
2013-09-23 07:14:28 -07:00
2015-05-23 01:38:19 -07:00
plugins = {}
descriptions = []
loader = None
2013-09-23 07:14:28 -07:00
2015-05-23 01:38:19 -07:00
def is_available(name, w):
for d in descriptions:
if d.get('name') == name:
break
else:
return False
deps = d.get('requires', [])
2015-05-24 01:06:53 -07:00
for dep, s in deps:
2015-05-23 01:38:19 -07:00
try:
__import__(dep)
except ImportError:
return False
wallet_types = d.get('requires_wallet_type')
if wallet_types:
if w.wallet_type not in wallet_types:
return False
return True
def plugin_loader(config, name):
global plugins
if plugins.get(name) is None:
print_error(_("Loading plugin by constructor:"), name)
p = loader(name)
plugins[name] = p.Plugin(config, name)
return plugins[name]
2015-05-24 00:26:28 -07:00
@profiler
def init_plugins(config, is_local, gui_name):
2015-05-23 01:38:19 -07:00
global plugins, descriptions, loader
if is_local:
2013-09-23 07:14:28 -07:00
fp, pathname, description = imp.find_module('plugins')
2015-05-23 01:38:19 -07:00
electrum_plugins = imp.load_module('electrum_plugins', fp, pathname, description)
loader = lambda name: imp.load_source('electrum_plugins.' + name, os.path.join(pathname, name + '.py'))
2013-09-23 07:14:28 -07:00
else:
2015-05-23 01:38:19 -07:00
electrum_plugins = __import__('electrum_plugins')
loader = lambda name: __import__('electrum_plugins.' + name, fromlist=['electrum_plugins'])
2013-09-23 07:14:28 -07:00
def register_wallet_type(name, x):
2015-05-23 01:38:19 -07:00
import wallet
2015-09-02 05:37:40 -07:00
x += (lambda: plugin_loader(config, name),)
2015-05-23 01:38:19 -07:00
wallet.wallet_types.append(x)
descriptions = electrum_plugins.descriptions
for item in descriptions:
name = item['name']
2015-05-24 00:26:28 -07:00
if gui_name not in item.get('available_for', []):
continue
2015-05-24 11:37:05 -07:00
x = item.get('registers_wallet_type')
if x:
register_wallet_type(name, x)
2015-05-23 01:38:19 -07:00
if not config.get('use_' + name):
continue
try:
p = loader(name)
plugins[name] = p.Plugin(config, name)
2013-11-09 21:23:57 -08:00
except Exception:
2015-05-24 01:06:53 -07:00
print_msg(_("Error: cannot initialize plugin"), name)
2013-09-23 07:14:28 -07:00
traceback.print_exc(file=sys.stdout)
2015-05-24 11:37:05 -07:00
2014-08-31 02:42:40 -07:00
hook_names = set()
hooks = {}
2013-09-23 07:14:28 -07:00
2014-08-31 02:42:40 -07:00
def hook(func):
hook_names.add(func.func_name)
2014-08-31 02:42:40 -07:00
return func
2013-09-23 07:14:28 -07:00
2014-08-31 02:42:40 -07:00
def run_hook(name, *args):
return _run_hook(name, False, *args)
def always_hook(name, *args):
return _run_hook(name, True, *args)
def _run_hook(name, always, *args):
2014-08-31 02:42:40 -07:00
results = []
f_list = hooks.get(name, [])
2014-08-31 02:42:40 -07:00
for p, f in f_list:
if name == 'load_wallet':
p.wallet = args[0]
2015-01-31 09:09:50 -08:00
if name == 'init_qt':
gui = args[0]
p.window = gui.main_window
if always or p.is_enabled():
try:
r = f(*args)
except Exception:
print_error("Plugin error")
traceback.print_exc(file=sys.stdout)
r = False
if r:
results.append(r)
if name == 'close_wallet':
p.wallet = None
if results:
assert len(results) == 1, results
return results[0]
2013-09-23 07:14:28 -07:00
2013-03-15 01:58:05 -07:00
class BasePlugin:
def __init__(self, config, name):
self.name = name
self.config = config
self.wallet = None
2014-08-31 02:42:40 -07:00
# add self to hooks
for k in dir(self):
if k in hook_names:
l = hooks.get(k, [])
l.append((self, getattr(self, k)))
hooks[k] = l
2013-03-17 03:52:58 -07:00
2015-05-23 01:38:19 -07:00
def close(self):
# remove self from hooks
for k in dir(self):
if k in hook_names:
l = hooks.get(k, [])
l.remove((self, getattr(self, k)))
hooks[k] = l
2015-03-31 03:21:20 -07:00
def print_error(self, *msg):
print_error("[%s]"%self.name, *msg)
2015-03-31 01:01:53 -07:00
2013-03-17 03:52:58 -07:00
def requires_settings(self):
return False
def enable(self):
self.set_enabled(True)
return True
def disable(self):
self.set_enabled(False)
return True
@hook
def load_wallet(self, wallet, window): pass
2014-09-04 07:37:51 -07:00
@hook
def close_wallet(self): pass
2014-09-04 07:37:51 -07:00
#def init(self): pass
2013-03-15 01:58:05 -07:00
def is_enabled(self):
return self.is_available() and self.config.get('use_'+self.name) is True
def is_available(self):
return True
def set_enabled(self, enabled):
self.config.set_key('use_'+self.name, enabled, True)
2013-03-17 03:29:01 -07:00
def settings_dialog(self):
pass