Move some IPC code from app.py to ipc.py.

This commit is contained in:
Florian Bruhin 2015-08-27 23:04:27 +02:00
parent ff6e96347b
commit a7bf0744e0
2 changed files with 44 additions and 21 deletions

View File

@ -91,29 +91,18 @@ def run(args):
objreg.register('signal-handler', signal_handler)
try:
sent = ipc.send_to_running_instance(args)
if sent:
sys.exit(usertypes.Exit.ok)
log.init.debug("Starting IPC server...")
server = ipc.IPCServer(args, qApp)
objreg.register('ipc-server', server)
server.got_args.connect(lambda args, cwd:
process_pos_args(args, cwd=cwd, via_ipc=True))
except ipc.AddressInUseError as e:
# This could be a race condition...
log.init.debug("Got AddressInUseError, trying again.")
time.sleep(500)
sent = ipc.send_to_running_instance(args)
if sent:
sys.exit(usertypes.Exit.ok)
else:
ipc.display_error(e, args)
sys.exit(usertypes.Exit.err_ipc)
except ipc.Error as e:
ipc.display_error(e, args)
server = ipc.send_or_listen(args)
except ipc.Error:
# ipc.send_or_listen already displays the error message for us.
# We didn't really initialize much so far, so we just quit hard.
sys.exit(usertypes.Exit.err_ipc)
if server is None:
sys.exit(usertypes.Exit.ok)
else:
server.got_args.connect(lambda args, cwd:
process_pos_args(args, cwd=cwd, via_ipc=True))
init(args, crash_handler)
ret = qt_mainloop()
return ret

View File

@ -28,7 +28,7 @@ import hashlib
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from PyQt5.QtNetwork import QLocalSocket, QLocalServer, QAbstractSocket
from qutebrowser.utils import log, usertypes, error
from qutebrowser.utils import log, usertypes, error, objreg
CONNECT_TIMEOUT = 100
@ -291,3 +291,37 @@ def display_error(exc, args):
error.handle_fatal_exc(
exc, args, "Error while connecting to running instance!",
post_text="Maybe another instance is running but frozen?")
def send_or_listen(args):
"""Send the args to a running instance or start a new IPCServer.
Args:
args: The argparse namespace.
Return:
The IPCServer instance if no running instance was detected.
None if an instance was running and received our request.
"""
try:
sent = send_to_running_instance(args)
if sent:
return None
log.init.debug("Starting IPC server...")
server = IPCServer(args)
objreg.register('ipc-server', server)
return server
except AddressInUseError as e:
# This could be a race condition...
log.init.debug("Got AddressInUseError, trying again.")
time.sleep(500)
sent = send_to_running_instance(args)
if sent:
return None
else:
display_error(e, args)
raise
except Error as e:
display_error(e, args)
raise