1
0

make plugins available without the GUI

This commit is contained in:
ThomasV
2015-05-23 10:38:19 +02:00
parent 89c277de9d
commit 8f98ea4aca
15 changed files with 244 additions and 160 deletions

View File

@@ -1,34 +1,89 @@
from util import print_error
import traceback, sys
#!/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
from util import *
from i18n import _
from util import print_error
plugins = []
plugins = {}
descriptions = []
loader = None
def init_plugins(config, local):
import imp, pkgutil, __builtin__, os
global plugins
if local:
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)
def is_available(name, w):
for d in descriptions:
if d.get('name') == name:
break
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):
return False
deps = d.get('requires', [])
for dep in deps:
try:
plugins.append( p.Plugin(config, name) )
except Exception:
print_msg(_("Error: cannot initialize plugin"),p)
traceback.print_exc(file=sys.stdout)
__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 init_plugins(config, is_local, is_gui):
global plugins, descriptions, loader
if is_local:
fp, pathname, description = imp.find_module('plugins')
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'))
else:
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
import wallet
try:
p = loader(name)
plugins[name] = p.Plugin(config, name)
except:
return
x = plugins[name].get_wallet_type()
wallet.wallet_types.append(x)
descriptions = electrum_plugins.descriptions
for item in descriptions:
name = item['name']
if item.get('registers_wallet_type'):
register_wallet_type(name)
if not config.get('use_' + name):
continue
try:
p = loader(name)
plugins[name] = p.Plugin(config, name)
except Exception:
print_msg(_("Error: cannot initialize plugin"), p)
traceback.print_exc(file=sys.stdout)
hook_names = set()
hooks = {}
@@ -81,15 +136,17 @@ class BasePlugin:
l.append((self, getattr(self, k)))
hooks[k] = l
def fullname(self):
return self.name
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
def print_error(self, *msg):
print_error("[%s]"%self.name, *msg)
def description(self):
return 'undefined'
def requires_settings(self):
return False
@@ -111,8 +168,6 @@ class BasePlugin:
#def init(self): pass
def close(self): pass
def is_enabled(self):
return self.is_available() and self.config.get('use_'+self.name) is True