@hook decorator for plugins

This commit is contained in:
ThomasV 2014-08-31 11:42:40 +02:00
parent ee49d14b55
commit a3790372d8
8 changed files with 46 additions and 23 deletions

View File

@ -29,29 +29,28 @@ def init_plugins(self):
traceback.print_exc(file=sys.stdout)
hook_names = set()
hooks = {}
def hook(func):
n = func.func_name
if n not in hook_names:
hook_names.add(n)
return func
def run_hook(name, *args):
global plugins
results = []
for p in plugins:
f_list = hooks.get(name,[])
for p, f in f_list:
if not p.is_enabled():
continue
f = getattr(p, name, None)
if not callable(f):
continue
try:
r = f(*args)
except Exception:
print_error("Plugin error")
traceback.print_exc(file=sys.stdout)
r = False
if r:
results.append(r)
@ -60,13 +59,18 @@ def run_hook(name, *args):
return results[0]
class BasePlugin:
def __init__(self, gui, name):
self.gui = gui
self.name = name
self.config = gui.config
# 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
def fullname(self):
return self.name
@ -86,7 +90,6 @@ class BasePlugin:
self.init()
return self.is_enabled()
def enable(self):
self.set_enabled(True)
@ -111,3 +114,4 @@ class BasePlugin:
def settings_dialog(self):
pass

View File

@ -12,7 +12,7 @@ from electrum_gui.qt.util import ok_cancel_buttons
from electrum.account import BIP32_Account
from electrum.bitcoin import EncodeBase58Check, DecodeBase58Check, public_key_to_bc_address, bc_address_to_hash_160
from electrum.i18n import _
from electrum.plugins import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.transaction import deserialize
from electrum.wallet import NewWallet
@ -76,12 +76,15 @@ class Plugin(BasePlugin):
def enable(self):
return BasePlugin.enable(self)
@hook
def load_wallet(self, wallet):
self.wallet = wallet
@hook
def add_wallet_types(self, wallet_types):
wallet_types.append(('btchip', _("BTChip wallet"), BTChipWallet))
@hook
def installwizard_restore(self, wizard, storage):
wallet = BTChipWallet(storage)
try:
@ -91,6 +94,7 @@ class Plugin(BasePlugin):
return
return wallet
@hook
def send_tx(self, tx):
try:
self.wallet.sign_transaction(tx, None, None)

View File

@ -22,7 +22,7 @@ try:
except ImportError as e:
loaded_qweb = False
from electrum import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _, set_language
from electrum.util import user_dir
from electrum.util import appdata_dir
@ -58,6 +58,7 @@ class Plugin(BasePlugin):
def enable(self):
return BasePlugin.enable(self)
@hook
def receive_tx(self, tx, wallet):
domain = wallet.get_account_addresses(None)
is_relevant, is_send, v, fee = tx.get_value(domain, wallet.prevout_values)

View File

@ -26,7 +26,7 @@ from PyQt4.QtCore import *
from electrum import bitcoin, util
from electrum import transaction
from electrum.plugins import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
import sys
@ -89,6 +89,7 @@ class Plugin(BasePlugin):
def description(self):
return description
@hook
def init(self):
self.win = self.gui.main_window
self.win.connect(self.win, SIGNAL('cosigner:receive'), self.on_receive)
@ -108,6 +109,7 @@ class Plugin(BasePlugin):
return True
return self.wallet.wallet_type in ['2of2', '2of3']
@hook
def load_wallet(self, wallet):
self.wallet = wallet
if not self.is_available():
@ -123,12 +125,14 @@ class Plugin(BasePlugin):
else:
self.cosigner_list.append((xpub, K, _hash))
@hook
def transaction_dialog(self, d):
self.send_button = b = QPushButton(_("Send to cosigner"))
b.clicked.connect(lambda: self.do_send(d.tx))
d.buttons.insertWidget(2, b)
self.transaction_dialog_update(d)
@hook
def transaction_dialog_update(self, d):
if d.tx.is_complete():
self.send_button.hide()

View File

@ -9,7 +9,7 @@ import threading
import time
import re
from decimal import Decimal
from electrum.plugins import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
from electrum_gui.qt.util import *
from electrum_gui.qt.amountedit import AmountEdit
@ -338,6 +338,7 @@ class Plugin(BasePlugin):
self.currencies = [self.fiat_unit()]
self.exchanges = [self.config.get('use_exchange', "Blockchain")]
@hook
def init(self):
self.win = self.gui.main_window
self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
@ -353,6 +354,7 @@ class Plugin(BasePlugin):
self.win.emit(SIGNAL("refresh_currencies()"))
self.win.emit(SIGNAL("refresh_currencies_combo()"))
@hook
def get_fiat_balance_text(self, btc_balance, r):
# return balance as: 1.23 USD
r[0] = self.create_fiat_balance_text(Decimal(btc_balance) / 100000000)
@ -364,6 +366,7 @@ class Plugin(BasePlugin):
if quote:
r[0] = "%s"%quote
@hook
def get_fiat_status_text(self, btc_balance, r2):
# return status as: (1.23 USD) 1 BTC~123.45 USD
text = ""
@ -391,6 +394,7 @@ class Plugin(BasePlugin):
quote_text = "%.2f %s" % (quote_balance, quote_currency)
return quote_text
@hook
def load_wallet(self, wallet):
self.wallet = wallet
tx_list = {}

View File

@ -16,7 +16,7 @@ import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
import aes
import base64
from electrum.plugins import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
from electrum_gui.qt import HelpButton, EnterButton
@ -43,11 +43,12 @@ class Plugin(BasePlugin):
return decoded_message
@hook
def init(self):
self.target_host = 'labelectrum.herokuapp.com'
self.window = self.gui.main_window
@hook
def load_wallet(self, wallet):
self.wallet = wallet
if self.wallet.get_master_public_key():
@ -77,6 +78,7 @@ class Plugin(BasePlugin):
def requires_settings(self):
return True
@hook
def set_label(self, item,label, changed):
if not changed:
return

View File

@ -11,7 +11,7 @@ from electrum_gui.qt.util import ok_cancel_buttons, EnterButton
from electrum.account import BIP32_Account
from electrum.bitcoin import EncodeBase58Check, public_key_to_bc_address, bc_address_to_hash_160
from electrum.i18n import _
from electrum.plugins import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.transaction import deserialize
from electrum.wallet import NewWallet
@ -74,12 +74,15 @@ class Plugin(BasePlugin):
def enable(self):
return BasePlugin.enable(self)
@hook
def load_wallet(self, wallet):
self.wallet = wallet
@hook
def add_wallet_types(self, wallet_types):
wallet_types.append(('trezor', _("Trezor wallet"), TrezorWallet))
@hook
def installwizard_restore(self, wizard, storage):
if storage.get('wallet_type') != 'trezor':
return
@ -91,6 +94,7 @@ class Plugin(BasePlugin):
return
return wallet
@hook
def send_tx(self, tx):
try:
self.wallet.sign_transaction(tx, None, None)

View File

@ -1,5 +1,5 @@
from PyQt4.QtGui import *
from electrum import BasePlugin
from electrum.plugins import BasePlugin, hook
from electrum.i18n import _
class Plugin(BasePlugin):
@ -15,7 +15,7 @@ class Plugin(BasePlugin):
self.vkb = None
self.vkb_index = 0
@hook
def password_dialog(self, pw, grid, pos):
vkb_button = QPushButton(_("+"))
vkb_button.setFixedWidth(20)