electrum-bitcoinprivate/lib/plugins.py

112 lines
2.8 KiB
Python
Raw Normal View History

2013-09-23 07:14:28 -07:00
from util import print_error
import traceback, sys
2013-09-27 23:08:36 -07:00
from util import *
from i18n import _
2013-09-23 07:14:28 -07:00
plugins = []
def init_plugins(config, local):
2013-09-23 07:14:28 -07:00
import imp, pkgutil, __builtin__, os
global plugins
if local:
2013-09-23 07:14:28 -07:00
fp, pathname, description = imp.find_module('plugins')
plugin_names = [name for a, name, b in pkgutil.iter_modules([pathname])]
plugin_names = filter( lambda name: os.path.exists(os.path.join(pathname,name+'.py')), plugin_names)
imp.load_module('electrum_plugins', fp, pathname, description)
plugin_modules = map(lambda name: imp.load_source('electrum_plugins.'+name, os.path.join(pathname,name+'.py')), plugin_names)
else:
import electrum_plugins
plugin_names = [name for a, name, b in pkgutil.iter_modules(electrum_plugins.__path__)]
plugin_modules = [ __import__('electrum_plugins.'+name, fromlist=['electrum_plugins']) for name in plugin_names]
for name, p in zip(plugin_names, plugin_modules):
try:
plugins.append( p.Plugin(config, name) )
2013-11-09 21:23:57 -08:00
except Exception:
2013-09-23 07:14:28 -07:00
print_msg(_("Error: cannot initialize plugin"),p)
traceback.print_exc(file=sys.stdout)
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):
n = func.func_name
if n not in hook_names:
hook_names.add(n)
return func
2013-09-23 07:14:28 -07:00
2014-08-31 02:42:40 -07:00
def run_hook(name, *args):
results = []
f_list = hooks.get(name,[])
for p, f in f_list:
if name == 'load_wallet':
p.wallet = args[0]
if not p.is_enabled():
2013-09-23 07:14:28 -07:00
continue
try:
r = f(*args)
2013-11-09 21:23:57 -08:00
except Exception:
2013-09-23 07:14:28 -07:00
print_error("Plugin error")
traceback.print_exc(file=sys.stdout)
2014-08-16 06:40:21 -07:00
r = False
if r:
results.append(r)
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
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
def fullname(self):
return self.name
def description(self):
return 'undefined'
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
2014-09-04 07:37:51 -07:00
def init_qt(self, gui): pass
def load_wallet(self, wallet): pass
#def init(self): pass
def close(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