Browse Source

@hook decorator for plugins

master
ThomasV 11 years ago
parent
commit
a3790372d8
  1. 32
      lib/plugins.py
  2. 6
      plugins/btchipwallet.py
  3. 3
      plugins/coinbase_buyback.py
  4. 6
      plugins/cosigner_pool.py
  5. 6
      plugins/exchange_rate.py
  6. 6
      plugins/labels.py
  7. 6
      plugins/trezor.py
  8. 4
      plugins/virtualkeyboard.py

32
lib/plugins.py

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

6
plugins/btchipwallet.py

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

3
plugins/coinbase_buyback.py

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

6
plugins/cosigner_pool.py

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

6
plugins/exchange_rate.py

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

6
plugins/labels.py

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

6
plugins/trezor.py

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

4
plugins/virtualkeyboard.py

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

Loading…
Cancel
Save