From 5b000a871f319a0ec4ffd7d622b0824ec60fa431 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 22 Jun 2022 01:24:19 +0200 Subject: [PATCH] EventListener follow-ups: adapt left-out classes and minor clean-ups --- electrum/daemon.py | 8 +++++--- electrum/exchange_rate.py | 9 +++++---- electrum/gui/qt/settings_dialog.py | 16 ++++++++-------- electrum/gui/qt/util.py | 6 ++---- electrum/gui/stdio.py | 22 ++++++++++++++-------- electrum/util.py | 5 ++--- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/electrum/daemon.py b/electrum/daemon.py index 9221066d5..a73b82062 100644 --- a/electrum/daemon.py +++ b/electrum/daemon.py @@ -44,6 +44,7 @@ from .network import Network from .util import (json_decode, to_bytes, to_string, profiler, standardize_path, constant_time_compare) from .invoices import PR_PAID, PR_EXPIRED from .util import log_exceptions, ignore_exceptions, randrange, OldTaskGroup +from .util import EventListener, event_listener from .wallet import Wallet, Abstract_Wallet from .storage import WalletStorage from .wallet_db import WalletDB @@ -356,7 +357,7 @@ class WatchTowerServer(AuthenticatedServer): return await self.lnwatcher.sweepstore.add_sweep_tx(*args) -class PayServer(Logger): +class PayServer(Logger, EventListener): def __init__(self, daemon: 'Daemon', netaddress): Logger.__init__(self) @@ -364,14 +365,15 @@ class PayServer(Logger): self.daemon = daemon self.config = daemon.config self.pending = defaultdict(asyncio.Event) - util.register_callback(self.on_payment, ['request_status']) + self.register_callbacks() @property def wallet(self): # FIXME specify wallet somehow? return list(self.daemon.get_wallets().values())[0] - async def on_payment(self, evt, wallet, key, status): + @event_listener + async def on_event_request_status(self, wallet, key, status): if status == PR_PAID: self.pending[key].set() diff --git a/electrum/exchange_rate.py b/electrum/exchange_rate.py index 5e033e4ea..e2c4add95 100644 --- a/electrum/exchange_rate.py +++ b/electrum/exchange_rate.py @@ -17,7 +17,7 @@ from . import util from .bitcoin import COIN from .i18n import _ from .util import (ThreadJob, make_dir, log_exceptions, OldTaskGroup, - make_aiohttp_session, resource_path) + make_aiohttp_session, resource_path, EventListener, event_listener) from .network import Network from .simple_config import SimpleConfig from .logging import Logger @@ -496,13 +496,13 @@ def get_exchanges_by_ccy(history=True): return dictinvert(d) -class FxThread(ThreadJob): +class FxThread(ThreadJob, EventListener): def __init__(self, config: SimpleConfig, network: Optional[Network]): ThreadJob.__init__(self) self.config = config self.network = network - util.register_callback(self.set_proxy, ['proxy_set']) + self.register_callbacks() self.ccy = self.get_currency() self.history_used_spot = False self.ccy_combo = None @@ -513,7 +513,8 @@ class FxThread(ThreadJob): self.set_exchange(self.config_exchange()) make_dir(self.cache_dir) - def set_proxy(self, trigger_name, *args): + @event_listener + def on_event_proxy_set(self, *args): self._trigger.set() @staticmethod diff --git a/electrum/gui/qt/settings_dialog.py b/electrum/gui/qt/settings_dialog.py index 0623643d6..e2225ddb0 100644 --- a/electrum/gui/qt/settings_dialog.py +++ b/electrum/gui/qt/settings_dialog.py @@ -34,12 +34,12 @@ from PyQt5.QtWidgets import (QComboBox, QTabWidget, QDialog, from electrum.i18n import _, languages from electrum import util, coinchooser, paymentrequest -from electrum.util import base_units_list +from electrum.util import base_units_list, event_listener from electrum.gui import messages from .util import (ColorScheme, WindowModalDialog, HelpLabel, Buttons, - CloseButton) + CloseButton, QtEventListener) if TYPE_CHECKING: @@ -47,7 +47,7 @@ if TYPE_CHECKING: from .main_window import ElectrumWindow -class SettingsDialog(QDialog): +class SettingsDialog(QDialog, QtEventListener): def __init__(self, window: 'ElectrumWindow', config: 'SimpleConfig'): QDialog.__init__(self) @@ -60,7 +60,7 @@ class SettingsDialog(QDialog): self.fx = window.fx self.wallet = window.wallet - util.register_callback(self.on_network_callback, ['alias_received']) + self.register_callbacks() self.app.alias_received_signal.connect(self.set_alias_color) vbox = QVBoxLayout() @@ -559,10 +559,10 @@ class SettingsDialog(QDialog): vbox.addStretch(1) vbox.addLayout(Buttons(CloseButton(self))) self.setLayout(vbox) - - def on_network_callback(self, cb): - if cb == 'alias_received': - self.app.alias_received_signal.emit() + + @event_listener + def on_event_alias_received(self): + self.app.alias_received_signal.emit() def set_alias_color(self): if not self.config.get('alias'): diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py index b2a766658..5c6028d89 100644 --- a/electrum/gui/qt/util.py +++ b/electrum/gui/qt/util.py @@ -28,6 +28,7 @@ from PyQt5.QtWidgets import (QPushButton, QLabel, QMessageBox, QHBoxLayout, from electrum.i18n import _, languages from electrum.util import FileImportFailed, FileExportFailed, make_aiohttp_session, resource_path +from electrum.util import EventListener, event_listener from electrum.invoices import PR_UNPAID, PR_PAID, PR_EXPIRED, PR_INFLIGHT, PR_UNKNOWN, PR_FAILED, PR_ROUTING, PR_UNCONFIRMED from electrum.logging import Logger from electrum.qrreader import MissingQrDetectionLib @@ -1503,8 +1504,6 @@ class VTabWidget(QtWidgets.QTabWidget): return super().resizeEvent(e) -from electrum.util import EventListener, event_listener - class QtEventListener(EventListener): qt_callback_signal = QtCore.pyqtSignal(tuple) @@ -1523,8 +1522,7 @@ class QtEventListener(EventListener): # decorator for members of the QtEventListener class def qt_event_listener(func): - assert func.__name__.startswith('on_event_') - func._is_event_listener = True + func = event_listener(func) @wraps(func) def decorator(self, *args): self.qt_callback_signal.emit( (func,) + args) diff --git a/electrum/gui/stdio.py b/electrum/gui/stdio.py index ed5632efd..c693c586b 100644 --- a/electrum/gui/stdio.py +++ b/electrum/gui/stdio.py @@ -9,7 +9,7 @@ from electrum import util from electrum import WalletStorage, Wallet from electrum.wallet import Abstract_Wallet from electrum.wallet_db import WalletDB -from electrum.util import format_satoshis +from electrum.util import format_satoshis, EventListener, event_listener from electrum.bitcoin import is_address, COIN from electrum.transaction import PartialTxOutput from electrum.network import TxBroadcastError, BestEffortRequestFailed @@ -20,7 +20,7 @@ _ = lambda x:x # i18n # written by rofl0r, with some bits stolen from the text gui (ncurses) -class ElectrumGui(BaseElectrumGui): +class ElectrumGui(BaseElectrumGui, EventListener): def __init__(self, *, config, daemon, plugins): BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins) @@ -47,7 +47,7 @@ class ElectrumGui(BaseElectrumGui): self.wallet.start_network(self.network) self.contacts = self.wallet.contacts - util.register_callback(self.on_network, ['wallet_updated', 'network_updated', 'banner']) + self.register_callbacks() self.commands = [_("[h] - displays this help text"), \ _("[i] - display transaction history"), \ _("[o] - enter payment order"), \ @@ -59,11 +59,17 @@ class ElectrumGui(BaseElectrumGui): _("[q] - quit")] self.num_commands = len(self.commands) - def on_network(self, event, *args): - if event in ['wallet_updated', 'network_updated']: - self.updated() - elif event == 'banner': - self.print_banner() + @event_listener + def on_event_wallet_updated(self, wallet): + self.updated() + + @event_listener + def on_event_network_updated(self): + self.updated() + + @event_listener + def on_event_banner(self): + self.print_banner() def main_command(self): self.print_balance() diff --git a/electrum/util.py b/electrum/util.py index 9ad3bfefd..ec8c31b3e 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -1633,7 +1633,6 @@ register_callback = callback_mgr.register_callback unregister_callback = callback_mgr.unregister_callback - class EventListener: def _list_callbacks(self): @@ -1650,8 +1649,8 @@ class EventListener: def register_callbacks(self): for name, method in self._list_callbacks(): - _logger.info(f'registering callback {method}') - register_callback(method, [name]) + _logger.info(f'registering callback {method}') + register_callback(method, [name]) def unregister_callbacks(self): for name, method in self._list_callbacks():