Browse Source

gui: add BaseElectrumGui base class for guis

patch-4
SomberNight 3 years ago
parent
commit
ca9b48e2d6
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 4
      electrum/daemon.py
  2. 19
      electrum/gui/__init__.py
  3. 24
      electrum/gui/kivy/__init__.py
  4. 12
      electrum/gui/qt/__init__.py
  5. 7
      electrum/gui/stdio.py
  6. 11
      electrum/gui/text.py

4
electrum/daemon.py

@ -441,7 +441,7 @@ class PayServer(Logger):
class Daemon(Logger): class Daemon(Logger):
network: Optional[Network] network: Optional[Network]
gui_object: Optional[Union['gui.qt.ElectrumGui', 'gui.kivy.ElectrumGui']] gui_object: Optional['gui.BaseElectrumGui']
@profiler @profiler
def __init__(self, config: SimpleConfig, fd=None, *, listen_jsonrpc=True): 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}') self.logger.info(f'launching GUI: {gui_name}')
try: try:
gui = __import__('electrum.gui.' + gui_name, fromlist=['electrum']) 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: if not self._stop_entered:
self.gui_object.main() self.gui_object.main()
else: else:

19
electrum/gui/__init__.py

@ -9,3 +9,22 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from . import qt from . import qt
from . import kivy 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

24
electrum/gui/kivy/__init__.py

@ -44,6 +44,7 @@ except ImportError:
kivy.require('1.8.0') kivy.require('1.8.0')
from electrum.logging import Logger from electrum.logging import Logger
from electrum.gui import BaseElectrumGui
if TYPE_CHECKING: if TYPE_CHECKING:
from electrum.simple_config import SimpleConfig from electrum.simple_config import SimpleConfig
@ -51,25 +52,20 @@ if TYPE_CHECKING:
from electrum.plugin import Plugins from electrum.plugin import Plugins
class ElectrumGui(BaseElectrumGui, Logger):
def __init__(self, *, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'):
class ElectrumGui(Logger): BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
def __init__(self, config: 'SimpleConfig', daemon: 'Daemon', plugins: 'Plugins'):
Logger.__init__(self) Logger.__init__(self)
self.logger.debug('ElectrumGUI: initialising') self.logger.debug('ElectrumGUI: initialising')
self.daemon = daemon
self.network = daemon.network self.network = daemon.network
self.config = config
self.plugins = plugins
def main(self): def main(self):
from .main_window import ElectrumWindow from .main_window import ElectrumWindow
w = ElectrumWindow(config=self.config, w = ElectrumWindow(
network=self.network, config=self.config,
plugins = self.plugins, network=self.network,
gui_object=self) plugins=self.plugins,
gui_object=self,
)
w.run() w.run()
def stop(self):
pass

12
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 import Wallet, Abstract_Wallet
from electrum.wallet_db import WalletDB from electrum.wallet_db import WalletDB
from electrum.logging import Logger from electrum.logging import Logger
from electrum.gui import BaseElectrumGui
from .installwizard import InstallWizard, WalletAlreadyOpenInMemory from .installwizard import InstallWizard, WalletAlreadyOpenInMemory
from .util import get_default_language, read_QIcon, ColorScheme, custom_message_box, MessageBoxMixin 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) network_updated_signal = pyqtSignal(str, object)
class ElectrumGui(Logger): class ElectrumGui(BaseElectrumGui, Logger):
network_dialog: Optional['NetworkDialog'] network_dialog: Optional['NetworkDialog']
lightning_dialog: Optional['LightningDialog'] lightning_dialog: Optional['LightningDialog']
watchtower_dialog: Optional['WatchtowerDialog'] watchtower_dialog: Optional['WatchtowerDialog']
@profiler @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())) set_language(config.get('language', get_default_language()))
BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
Logger.__init__(self) Logger.__init__(self)
self.logger.info(f"Qt GUI starting up... Qt={QtCore.QT_VERSION_STR}, PyQt={QtCore.PYQT_VERSION_STR}") 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 # Uncomment this call to verify objects are being properly
@ -109,9 +111,6 @@ class ElectrumGui(Logger):
if hasattr(QGuiApplication, 'setDesktopFileName'): if hasattr(QGuiApplication, 'setDesktopFileName'):
QGuiApplication.setDesktopFileName('electrum.desktop') QGuiApplication.setDesktopFileName('electrum.desktop')
self.gui_thread = threading.current_thread() self.gui_thread = threading.current_thread()
self.config = config
self.daemon = daemon
self.plugins = plugins
self.windows = [] # type: List[ElectrumWindow] self.windows = [] # type: List[ElectrumWindow]
self.efilter = OpenFileEventFilter(self.windows) self.efilter = OpenFileEventFilter(self.windows)
self.app = QElectrumApplication(sys.argv) 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 # on some platforms the exec_ call may not return, so use _cleanup_before_exit
def stop(self): def stop(self):
"""Stops the GUI.
This method is thread-safe.
"""
self.logger.info('closing GUI') self.logger.info('closing GUI')
self.app.quit_signal.emit() self.app.quit_signal.emit()

7
electrum/gui/stdio.py

@ -3,6 +3,7 @@ import getpass
import datetime import datetime
import logging import logging
from electrum.gui import BaseElectrumGui
from electrum import util from electrum import util
from electrum import WalletStorage, Wallet from electrum import WalletStorage, Wallet
from electrum.wallet_db import WalletDB 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) # written by rofl0r, with some bits stolen from the text gui (ncurses)
class ElectrumGui: class ElectrumGui(BaseElectrumGui):
def __init__(self, config, daemon, plugins): def __init__(self, *, config, daemon, plugins):
self.config = config BaseElectrumGui.__init__(self, config=config, daemon=daemon, plugins=plugins)
self.network = daemon.network self.network = daemon.network
storage = WalletStorage(config.get_wallet_path()) storage = WalletStorage(config.get_wallet_path())
if not storage.file_exists: if not storage.file_exists:

11
electrum/gui/text.py

@ -9,6 +9,7 @@ import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import electrum import electrum
from electrum.gui import BaseElectrumGui
from electrum import util from electrum import util
from electrum.util import format_satoshis from electrum.util import format_satoshis
from electrum.bitcoin import is_address, COIN from electrum.bitcoin import is_address, COIN
@ -28,11 +29,10 @@ if TYPE_CHECKING:
_ = lambda x:x # i18n _ = lambda x:x # i18n
class ElectrumGui: class ElectrumGui(BaseElectrumGui):
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)
self.config = config
self.network = daemon.network self.network = daemon.network
storage = WalletStorage(config.get_wallet_path()) storage = WalletStorage(config.get_wallet_path())
if not storage.file_exists(): if not storage.file_exists():
@ -342,9 +342,6 @@ class ElectrumGui:
curses.echo() curses.echo()
curses.endwin() curses.endwin()
def stop(self):
pass
def do_clear(self): def do_clear(self):
self.str_amount = '' self.str_amount = ''
self.str_recipient = '' self.str_recipient = ''

Loading…
Cancel
Save