ipc: Refactor _get_socketname to take better args.

This commit is contained in:
Florian Bruhin 2015-08-27 22:32:29 +02:00
parent 0cd265296e
commit ff6e96347b

View File

@ -36,11 +36,13 @@ WRITE_TIMEOUT = 1000
READ_TIMEOUT = 5000 READ_TIMEOUT = 5000
def _get_socketname(args): def _get_socketname(basedir, user=None):
"""Get a socketname to use.""" """Get a socketname to use."""
parts = ['qutebrowser', getpass.getuser()] if user is None:
if args.basedir is not None: user = getpass.getuser()
md5 = hashlib.md5(args.basedir.encode('utf-8')) parts = ['qutebrowser', user]
if basedir is not None:
md5 = hashlib.md5(basedir.encode('utf-8'))
parts.append(md5.hexdigest()) parts.append(md5.hexdigest())
return '-'.join(parts) return '-'.join(parts)
@ -106,7 +108,7 @@ class IPCServer(QObject):
""" """
super().__init__(parent) super().__init__(parent)
self.ignored = False self.ignored = False
self._socketname = _get_socketname(args) self._socketname = _get_socketname(args.basedir)
self._remove_server() self._remove_server()
self._timer = usertypes.Timer(self, 'ipc-timeout') self._timer = usertypes.Timer(self, 'ipc-timeout')
self._timer.setInterval(READ_TIMEOUT) self._timer.setInterval(READ_TIMEOUT)
@ -252,7 +254,7 @@ def send_to_running_instance(args):
True if connecting was successful, False if no connection was made. True if connecting was successful, False if no connection was made.
""" """
socket = QLocalSocket() socket = QLocalSocket()
socketname = _get_socketname(args) socketname = _get_socketname(args.basedir)
log.ipc.debug("Connecting to {}".format(socketname)) log.ipc.debug("Connecting to {}".format(socketname))
socket.connectToServer(socketname) socket.connectToServer(socketname)
connected = socket.waitForConnected(100) connected = socket.waitForConnected(100)