From e3dfaa6a4b5a95066e65b50b220d3870ed530ce3 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 30 Nov 2014 19:22:35 +0100 Subject: [PATCH] Send cwd over IPC. Fixes #254. --- qutebrowser/app.py | 5 +++-- qutebrowser/utils/ipc.py | 17 +++++++++++++---- qutebrowser/utils/urlutils.py | 12 ++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 6d4ceacbc..7087fd9e7 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -258,7 +258,7 @@ class Application(QApplication): self.alert(window_to_raise) return win_id - def process_args(self, args, via_ipc=False): + def process_args(self, args, via_ipc=False, cwd=None): """Process commandline args. URLs to open have no prefix, commands to execute begin with a colon. @@ -266,6 +266,7 @@ class Application(QApplication): Args: args: A list of arguments to process. via_ipc: Whether the arguments were transmitted over IPC. + cwd: The cwd to use for fuzzy_url. """ if ipc and not args: win_id = self._get_window(via_ipc, force_window=True) @@ -288,7 +289,7 @@ class Application(QApplication): window=win_id) log.init.debug("Startup URL {}".format(cmd)) try: - url = urlutils.fuzzy_url(cmd) + url = urlutils.fuzzy_url(cmd, cwd) except urlutils.FuzzyUrlError as e: message.error(0, "Error in startup argument '{}': " "{}".format(cmd, e)) diff --git a/qutebrowser/utils/ipc.py b/qutebrowser/utils/ipc.py index 2254a9921..ccf6ae0d6 100644 --- a/qutebrowser/utils/ipc.py +++ b/qutebrowser/utils/ipc.py @@ -19,6 +19,7 @@ """Utilities for IPC with existing instances.""" +import os import json import getpass import binascii @@ -138,15 +139,22 @@ class IPCServer(QObject): log.ipc.debug("invalid data: {}".format( binascii.hexlify(data))) return + log.ipc.debug("Processing: {}".format(decoded)) try: - args = json.loads(decoded) + json_data = json.loads(decoded) except ValueError: log.ipc.error("Ignoring invalid IPC data.") log.ipc.debug("invalid json: {}".format(decoded.strip())) return - log.ipc.debug("Processing: {}".format(decoded)) + try: + args = json_data['args'] + cwd = json_data['cwd'] + except KeyError: + log.ipc.error("Ignoring invalid IPC data.") + log.ipc.debug("no args/cwd: {}".format(decoded.strip())) + return app = objreg.get('app') - app.process_args(args, via_ipc=True) + app.process_args(args, via_ipc=True, cwd=cwd) @pyqtSlot() def on_timeout(self): @@ -199,7 +207,8 @@ def send_to_running_instance(cmdlist): connected = socket.waitForConnected(100) if connected: log.ipc.info("Opening in existing instance") - line = json.dumps(cmdlist) + '\n' + json_data = {'args': cmdlist, 'cwd': os.getcwd()} + line = json.dumps(json_data) + '\n' data = line.encode('utf-8') log.ipc.debug("Writing: {}".format(data)) socket.writeData(data) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index c533a79b7..f66e7559b 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -124,27 +124,31 @@ def _is_url_dns(url): return not info.error() -def fuzzy_url(urlstr): +def fuzzy_url(urlstr, cwd=None): """Get a QUrl based on an user input which is URL or search term. Args: urlstr: URL to load as a string. + cwd: The current working directory, or None. Return: A target QUrl to a searchpage or the original URL. """ - path = os.path.abspath(os.path.expanduser(urlstr)) + if cwd: + path = os.path.join(cwd, os.path.expanduser(urlstr)) + else: + path = os.path.abspath(os.path.expanduser(urlstr)) stripped = urlstr.strip() if os.path.exists(path): log.url.debug("URL is a local file") url = QUrl.fromLocalFile(path) elif (not _has_explicit_scheme(QUrl(urlstr)) and - os.path.exists(os.path.abspath(path))): + os.path.exists(path)): # We do this here rather than in the first block because we first want # to make sure it's not an URL like http://, because os.path.abspath # would mangle that. log.url.debug("URL is a relative local file") - url = QUrl.fromLocalFile(os.path.abspath(path)) + url = QUrl.fromLocalFile(path) elif is_url(stripped): # probably an address log.url.debug("URL is a fuzzy address")