t@hook decorator for plugins - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit a3790372d859add85de119aa7dc2d8a49880bb66 DIR parent ee49d14b55d5c342272aea344d7b3b77ad076cfd HTML Author: ThomasV <thomasv@gitorious> Date: Sun, 31 Aug 2014 11:42:40 +0200 @hook decorator for plugins Diffstat: M lib/plugins.py | 32 +++++++++++++++++-------------- M plugins/btchipwallet.py | 6 +++++- M plugins/coinbase_buyback.py | 3 ++- M plugins/cosigner_pool.py | 6 +++++- M plugins/exchange_rate.py | 6 +++++- M plugins/labels.py | 6 ++++-- M plugins/trezor.py | 6 +++++- M plugins/virtualkeyboard.py | 4 ++-- 8 files changed, 46 insertions(+), 23 deletions(-) --- DIR diff --git a/lib/plugins.py b/lib/plugins.py t@@ -29,29 +29,28 @@ def init_plugins(self): traceback.print_exc(file=sys.stdout) +hook_names = set() +hooks = {} -def run_hook(name, *args): - - global plugins +def hook(func): + n = func.func_name + if n not in hook_names: + hook_names.add(n) + return func - results = [] - - for p in plugins: +def run_hook(name, *args): + results = [] + 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) t@@ -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 t@@ -86,7 +90,6 @@ class BasePlugin: self.init() return self.is_enabled() - def enable(self): self.set_enabled(True) t@@ -111,3 +114,4 @@ class BasePlugin: def settings_dialog(self): pass + DIR diff --git a/plugins/btchipwallet.py b/plugins/btchipwallet.py t@@ -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 t@@ -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: t@@ -91,6 +94,7 @@ class Plugin(BasePlugin): return return wallet + @hook def send_tx(self, tx): try: self.wallet.sign_transaction(tx, None, None) DIR diff --git a/plugins/coinbase_buyback.py b/plugins/coinbase_buyback.py t@@ -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 t@@ -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) DIR diff --git a/plugins/cosigner_pool.py b/plugins/cosigner_pool.py t@@ -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 t@@ -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) t@@ -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(): t@@ -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() DIR diff --git a/plugins/exchange_rate.py b/plugins/exchange_rate.py t@@ -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 t@@ -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) t@@ -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) t@@ -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 = "" t@@ -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 = {} DIR diff --git a/plugins/labels.py b/plugins/labels.py t@@ -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 t@@ -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(): t@@ -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 DIR diff --git a/plugins/trezor.py b/plugins/trezor.py t@@ -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 t@@ -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 t@@ -91,6 +94,7 @@ class Plugin(BasePlugin): return return wallet + @hook def send_tx(self, tx): try: self.wallet.sign_transaction(tx, None, None) DIR diff --git a/plugins/virtualkeyboard.py b/plugins/virtualkeyboard.py t@@ -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): t@@ -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)