electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 3ec0ceba3e7c65404d2579c64fac9449cbc450f1
parent 67d3d6b5b5426e0c13bc80437a0b3716e8446134
Author: ThomasV <thomasv@electrum.org>
Date:   Thu, 13 Sep 2018 16:25:56 +0200

add option to leave daemon running after GUI is closed

Diffstat:
Melectrum/commands.py | 1+
Melectrum/daemon.py | 36+++++++++++++++++-------------------
Melectrum/gui/qt/__init__.py | 17+++++++++++++----
Mrun_electrum | 4++--
4 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/electrum/commands.py b/electrum/commands.py @@ -853,6 +853,7 @@ def get_parser(): parser_gui.add_argument("-o", "--offline", action="store_true", dest="offline", default=False, help="Run offline") parser_gui.add_argument("-m", action="store_true", dest="hide_gui", default=False, help="hide GUI on startup") parser_gui.add_argument("-L", "--lang", dest="language", default=None, help="default language used in GUI") + parser_gui.add_argument("--daemon", action="store_true", dest="daemon", default=False, help="keep daemon running after GUI is closed") add_network_options(parser_gui) add_global_options(parser_gui) # daemon diff --git a/electrum/daemon.py b/electrum/daemon.py @@ -120,7 +120,7 @@ def get_rpc_credentials(config): class Daemon(DaemonThread): - def __init__(self, config, fd, is_gui): + def __init__(self, config, fd): DaemonThread.__init__(self) self.config = config if config.get('offline'): @@ -133,12 +133,11 @@ class Daemon(DaemonThread): self.gui = None self.wallets = {} # Setup JSONRPC server - self.init_server(config, fd, is_gui) + self.init_server(config, fd) - def init_server(self, config, fd, is_gui): + def init_server(self, config, fd): host = config.get('rpchost', '127.0.0.1') port = config.get('rpcport', 0) - rpc_user, rpc_password = get_rpc_credentials(config) try: server = VerifyingJSONRPCServer((host, port), logRequests=False, @@ -153,14 +152,12 @@ class Daemon(DaemonThread): self.server = server server.timeout = 0.1 server.register_function(self.ping, 'ping') - if is_gui: - server.register_function(self.run_gui, 'gui') - else: - server.register_function(self.run_daemon, 'daemon') - self.cmd_runner = Commands(self.config, None, self.network) - for cmdname in known_commands: - server.register_function(getattr(self.cmd_runner, cmdname), cmdname) - server.register_function(self.run_cmdline, 'run_cmdline') + server.register_function(self.run_gui, 'gui') + server.register_function(self.run_daemon, 'daemon') + self.cmd_runner = Commands(self.config, None, self.network) + for cmdname in known_commands: + server.register_function(getattr(self.cmd_runner, cmdname), cmdname) + server.register_function(self.run_cmdline, 'run_cmdline') def ping(self): return True @@ -215,13 +212,12 @@ class Daemon(DaemonThread): def run_gui(self, config_options): config = SimpleConfig(config_options) if self.gui: - #if hasattr(self.gui, 'new_window'): - # path = config.get_wallet_path() - # self.gui.new_window(path, config.get('url')) - # response = "ok" - #else: - # response = "error: current GUI does not support multiple windows" - response = "error: Electrum GUI already running" + if hasattr(self.gui, 'new_window'): + path = config.get_wallet_path() + self.gui.new_window(path, config.get('url')) + response = "ok" + else: + response = "error: current GUI does not support multiple windows" else: response = "Error: Electrum is running in daemon mode. Please stop the daemon first." return response @@ -299,6 +295,8 @@ class Daemon(DaemonThread): self.on_stop() def stop(self): + if self.gui: + self.gui.stop() self.print_error("stopping, removing lockfile") remove_lockfile(get_lockfile(self.config)) DaemonThread.stop(self) diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py @@ -45,7 +45,7 @@ from electrum.base_wizard import GoBack # from electrum.synchronizer import Synchronizer # from electrum.verifier import SPV # from electrum.util import DebugMem -from electrum.util import (UserCancelled, print_error, +from electrum.util import (UserCancelled, PrintError, WalletFileException, BitcoinException) # from electrum.wallet import Abstract_Wallet @@ -86,7 +86,7 @@ class QNetworkUpdatedSignalObject(QObject): network_updated_signal = pyqtSignal(str, object) -class ElectrumGui: +class ElectrumGui(PrintError): def __init__(self, config, daemon, plugins): set_language(config.get('language')) @@ -128,7 +128,7 @@ class ElectrumGui: self.app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) except BaseException as e: use_dark_theme = False - print_error('Error setting dark theme: {}'.format(e)) + self.print_error('Error setting dark theme: {}'.format(e)) # Even if we ourselves don't set the dark theme, # the OS/window manager/etc might set *a dark theme*. # Hence, try to choose colors accordingly: @@ -222,7 +222,7 @@ class ElectrumGui: except UserCancelled: pass except GoBack as e: - print_error('[start_new_window] Exception caught (GoBack)', e) + self.print_error('[start_new_window] Exception caught (GoBack)', e) except (WalletFileException, BitcoinException) as e: traceback.print_exc(file=sys.stderr) d = QMessageBox(QMessageBox.Warning, _('Error'), @@ -267,6 +267,7 @@ class ElectrumGui: if not self.windows: self.config.save_last_wallet(window.wallet) run_hook('on_close_window', window) + self.daemon.stop_wallet(window.wallet.storage.path) def init_network(self): # Show network dialog if config does not exist @@ -309,6 +310,14 @@ class ElectrumGui: self.tray.hide() self.app.aboutToQuit.connect(clean_up) + # keep daemon running after close + if self.config.get('daemon'): + self.app.setQuitOnLastWindowClosed(False) + # main loop self.app.exec_() # on some platforms the exec_ call may not return, so use clean_up() + + def stop(self): + self.print_error('closing GUI') + self.app.quit() diff --git a/run_electrum b/run_electrum @@ -415,7 +415,7 @@ if __name__ == '__main__': fd, server = daemon.get_fd_or_server(config) if fd is not None: plugins = init_plugins(config, config.get('gui', 'qt')) - d = daemon.Daemon(config, fd, True) + d = daemon.Daemon(config, fd) d.start() d.init_gui(config, plugins) sys.exit(0) @@ -436,7 +436,7 @@ if __name__ == '__main__': print_stderr("starting daemon (PID %d)" % pid) sys.exit(0) init_plugins(config, 'cmdline') - d = daemon.Daemon(config, fd, False) + d = daemon.Daemon(config, fd) d.start() if config.get('websocket_server'): from electrum import websockets