From ca9b48e2d6b5cd4a16892048b685b6548013bac2 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 5 Nov 2021 20:21:50 +0100 Subject: [PATCH] gui: add BaseElectrumGui base class for guis --- electrum/daemon.py | 4 ++-- electrum/gui/__init__.py | 19 +++++++++++++++++++ electrum/gui/kivy/__init__.py | 24 ++++++++++-------------- electrum/gui/qt/__init__.py | 12 ++++-------- electrum/gui/stdio.py | 7 ++++--- electrum/gui/text.py | 11 ++++------- 6 files changed, 43 insertions(+), 34 deletions(-) diff --git a/electrum/daemon.py b/electrum/daemon.py index ec4311816..6611b20e3 100644 --- a/electrum/daemon.py +++ b/electrum/daemon.py @@ -441,7 +441,7 @@ class PayServer(Logger): class Daemon(Logger): network: Optional[Network] - gui_object: Optional[Union['gui.qt.ElectrumGui', 'gui.kivy.ElectrumGui']] + gui_object: Optional['gui.BaseElectrumGui'] @profiler def __init__(self, config: SimpleConfig, fd=None, *, listen_jsonrpc=True): @@ -614,7 +614,7 @@ class Daemon(Logger): self.logger.info(f'launching GUI: {gui_name}') try: gui = __import__('electrum.gui.' + gui_name, fromlist=['electrum']) - self.gui_object = gui.ElectrumGui(config, self, plugins) + self.gui_object = gui.ElectrumGui(config=config, daemon=self, plugins=plugins) if not self._stop_entered: self.gui_object.main() else: diff --git a/electrum/gui/__init__.py b/electrum/gui/__init__.py index f0bef9a44..4d4ccdcdd 100644 --- a/electrum/gui/__init__.py +++ b/electrum/gui/__init__.py @@ -9,3 +9,22 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from . import qt from . import kivy + from electrum.simple_config import SimpleConfig + from electrum.daemon import Daemon + from electrum.plugin import Plugins + + +class BaseElectrumGui: + def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): + self.config = config + self.daemon = daemon + self.plugins = plugins + + def main(self) -> None: + raise NotImplementedError() + + def stop(self) -> None: + """Stops the GUI. + This method must be thread-safe. + """ + pass diff --git a/electrum/gui/kivy/__init__.py b/electrum/gui/kivy/__init__.py index a98ce1314..3d4421e50 100644 --- a/electrum/gui/kivy/__init__.py +++ b/electrum/gui/kivy/__init__.py @@ -44,6 +44,7 @@ except ImportError: kivy.require('1.8.0') from electrum.logging import Logger +from electrum.gui import BaseElectrumGui if TYPE_CHECKING: from electrum.simple_config import SimpleConfig @@ -51,25 +52,20 @@ if TYPE_CHECKING: from electrum.plugin import Plugins +class ElectrumGui(BaseElectrumGui, Logger): - -class ElectrumGui(Logger): - - def __init__(self, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): + def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): + BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins) Logger.__init__(self) self.logger.debug('ElectrumGUI: initialising') - self.daemon = daemon self.network = daemon.network - self.config = config - self.plugins = plugins def main(self): from .main_window import ElectrumWindow - w = ElectrumWindow(config=self.config, - network=self.network, - plugins = self.plugins, - gui_object=self) + w = ElectrumWindow( + config=self.config, + network=self.network, + plugins=self.plugins, + gui_object=self, + ) w.run() - - def stop(self): - pass diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py index 80b53187e..3c46a1d07 100644 --- a/electrum/gui/qt/__init__.py +++ b/electrum/gui/qt/__init__.py @@ -50,6 +50,7 @@ from electrum.util import (UserCancelled, profiler, send_exception_to_crash_repo from electrum.wallet import Wallet, Abstract_Wallet from electrum.wallet_db import WalletDB from electrum.logging import Logger +from electrum.gui import BaseElectrumGui from .installwizard import InstallWizard, WalletAlreadyOpenInMemory from .util import get_default_language, read_QIcon, ColorScheme, custom_message_box, MessageBoxMixin @@ -88,15 +89,16 @@ class QNetworkUpdatedSignalObject(QObject): network_updated_signal = pyqtSignal(str, object) -class ElectrumGui(Logger): +class ElectrumGui(BaseElectrumGui, Logger): network_dialog: Optional['NetworkDialog'] lightning_dialog: Optional['LightningDialog'] watchtower_dialog: Optional['WatchtowerDialog'] @profiler - def __init__(self, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): + def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): set_language(config.get('language', get_default_language())) + BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins) Logger.__init__(self) self.logger.info(f"Qt GUI starting up... Qt={QtCore.QT_VERSION_STR}, PyQt={QtCore.PYQT_VERSION_STR}") # Uncomment this call to verify objects are being properly @@ -109,9 +111,6 @@ class ElectrumGui(Logger): if hasattr(QGuiApplication, 'setDesktopFileName'): QGuiApplication.setDesktopFileName('electrum.desktop') self.gui_thread = threading.current_thread() - self.config = config - self.daemon = daemon - self.plugins = plugins self.windows = [] # type: List[ElectrumWindow] self.efilter = OpenFileEventFilter(self.windows) self.app = QElectrumApplication(sys.argv) @@ -430,8 +429,5 @@ class ElectrumGui(Logger): # on some platforms the exec_ call may not return, so use _cleanup_before_exit def stop(self): - """Stops the GUI. - This method is thread-safe. - """ self.logger.info('closing GUI') self.app.quit_signal.emit() diff --git a/electrum/gui/stdio.py b/electrum/gui/stdio.py index f39de82d5..05480f34b 100644 --- a/electrum/gui/stdio.py +++ b/electrum/gui/stdio.py @@ -3,6 +3,7 @@ import getpass import datetime import logging +from electrum.gui import BaseElectrumGui from electrum import util from electrum import WalletStorage, Wallet from electrum.wallet_db import WalletDB @@ -17,10 +18,10 @@ _ = lambda x:x # i18n # written by rofl0r, with some bits stolen from the text gui (ncurses) -class ElectrumGui: +class ElectrumGui(BaseElectrumGui): - def __init__(self, config, daemon, plugins): - self.config = config + def __init__(self, *, config, daemon, plugins): + BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins) self.network = daemon.network storage = WalletStorage(config.get_wallet_path()) if not storage.file_exists: diff --git a/electrum/gui/text.py b/electrum/gui/text.py index 6c6b27957..2d104fff2 100644 --- a/electrum/gui/text.py +++ b/electrum/gui/text.py @@ -9,6 +9,7 @@ import logging from typing import TYPE_CHECKING import electrum +from electrum.gui import BaseElectrumGui from electrum import util from electrum.util import format_satoshis from electrum.bitcoin import is_address, COIN @@ -28,11 +29,10 @@ if TYPE_CHECKING: _ = lambda x:x # i18n -class ElectrumGui: +class ElectrumGui(BaseElectrumGui): - def __init__(self, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): - - self.config = config + def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'): + BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins) self.network = daemon.network storage = WalletStorage(config.get_wallet_path()) if not storage.file_exists(): @@ -342,9 +342,6 @@ class ElectrumGui: curses.echo() curses.endwin() - def stop(self): - pass - def do_clear(self): self.str_amount = '' self.str_recipient = ''