From 4aa7649c0a9b780dd1897f8017f6a7da40db5331 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Tue, 23 Feb 2016 07:52:45 +0100 Subject: [PATCH 01/14] Implemented heurisitc on multiline paste --- qutebrowser/browser/commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 586573948..faa5be6ee 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -820,8 +820,10 @@ class CommandDispatcher: raise cmdexc.CommandError("{} is empty.".format(target)) log.misc.debug("{} contained: '{}'".format(target, text.replace('\n', '\\n'))) - text_urls = enumerate(u for u in text.split('\n') if u.strip()) - for i, text_url in text_urls: + text_urls = [u for u in text.split('\n') if u.strip()] + if len(text_urls) > 1 and not urlutils.is_url(text_urls[0]): + text_urls = [text] + for i, text_url in enumerate(text_urls): if not window and i > 0: tab = False bg = True From 594b0d29106f49862b6fb69587c8cea76051bf8f Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 07:59:51 +0100 Subject: [PATCH 02/14] Refactors path validity check to its own function --- qutebrowser/utils/urlutils.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index e70a09f2d..54a55dd9b 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -348,6 +348,40 @@ def raise_cmdexc_if_invalid(url): if not url.isValid(): raise cmdexc.CommandError(get_errstring(url)) +def get_path_if_valid(pathstr, cwd = None, relative=False, check_exists = False): + """Check if path is a valid path + + Args: + pathstr: The path as string + + Return: + True if it is a valid path, False otherwise. + """ + log.url.debug("Checking if '{}' is a path".format(pathstr)) + + pathstr = pathstr.strip() + expanded = os.path.expanduser(pathstr) + + if os.path.isabs(expanded): + path = expanded + elif relative and cwd: + path = os.path.join(cwd, expanded) + elif relative: + try: + path = os.path.abspath(expanded) + except OSError: + path = None + else: + path = None + + if check_exists: + if path is not None and os.path.exists(path): + log.url.debug("URL is a local file") + else: + path = None + + return path + def filename_from_url(url): """Get a suitable filename from an URL. From 4500bc24d4073f1d7fc615f96024167400743ae3 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 08:01:06 +0100 Subject: [PATCH 03/14] fuzzy_url uses path check function --- qutebrowser/utils/urlutils.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 54a55dd9b..723f2c62f 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -171,22 +171,10 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True): A target QUrl to a search page or the original URL. """ urlstr = urlstr.strip() - expanded = os.path.expanduser(urlstr) + path = get_path_if_valid(urlstr, cwd = cwd, relative = relative, + check_exists = True) - if os.path.isabs(expanded): - path = expanded - elif relative and cwd: - path = os.path.join(cwd, expanded) - elif relative: - try: - path = os.path.abspath(expanded) - except OSError: - path = None - else: - path = None - - if path is not None and os.path.exists(path): - log.url.debug("URL is a local file") + if path is not None: url = QUrl.fromLocalFile(path) elif (not do_search) or is_url(urlstr): # probably an address From 006d8760c4fe620f854fbf7d5ca75cdb4838a6c8 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 08:01:44 +0100 Subject: [PATCH 04/14] Adds path checking to the multiline url heuristic --- qutebrowser/browser/commands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index faa5be6ee..44418864f 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -821,7 +821,8 @@ class CommandDispatcher: log.misc.debug("{} contained: '{}'".format(target, text.replace('\n', '\\n'))) text_urls = [u for u in text.split('\n') if u.strip()] - if len(text_urls) > 1 and not urlutils.is_url(text_urls[0]): + if len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) \ + and urlutils.get_path_if_valid(text_urls[0], check_exists = True) is None: text_urls = [text] for i, text_url in enumerate(text_urls): if not window and i > 0: From 702b235981cb431149308260b12b7db50e7a2966 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 10:00:49 +0100 Subject: [PATCH 05/14] Fixes formatting to make pylint happy. --- qutebrowser/browser/commands.py | 3 ++- qutebrowser/utils/urlutils.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 44418864f..fd1e12990 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -822,7 +822,8 @@ class CommandDispatcher: text.replace('\n', '\\n'))) text_urls = [u for u in text.split('\n') if u.strip()] if len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) \ - and urlutils.get_path_if_valid(text_urls[0], check_exists = True) is None: + and urlutils.get_path_if_valid(text_urls[0], + check_exists=True) is not None: text_urls = [text] for i, text_url in enumerate(text_urls): if not window and i > 0: diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 723f2c62f..3f79cb87c 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -171,8 +171,8 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True): A target QUrl to a search page or the original URL. """ urlstr = urlstr.strip() - path = get_path_if_valid(urlstr, cwd = cwd, relative = relative, - check_exists = True) + path = get_path_if_valid(urlstr, cwd=cwd, relative=relative, + check_exists=True) if path is not None: url = QUrl.fromLocalFile(path) @@ -336,7 +336,7 @@ def raise_cmdexc_if_invalid(url): if not url.isValid(): raise cmdexc.CommandError(get_errstring(url)) -def get_path_if_valid(pathstr, cwd = None, relative=False, check_exists = False): +def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): """Check if path is a valid path Args: From 67ebdc6eb6994d937c7fbbabcc15e9bc964ad2ea Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Fri, 26 Feb 2016 10:02:41 +0100 Subject: [PATCH 06/14] Fixes the position of the logging line --- qutebrowser/utils/urlutils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 3f79cb87c..42f222a42 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -345,9 +345,8 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): Return: True if it is a valid path, False otherwise. """ - log.url.debug("Checking if '{}' is a path".format(pathstr)) - pathstr = pathstr.strip() + log.url.debug("Checking if '{}' is a path".format(pathstr)) expanded = os.path.expanduser(pathstr) if os.path.isabs(expanded): From 79ad65ee649e8357881c5a17a49af4e7077c238d Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sat, 27 Feb 2016 12:36:13 +0100 Subject: [PATCH 07/14] Fixes flake8 errors --- qutebrowser/utils/urlutils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 42f222a42..e7e2ebe97 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -336,11 +336,12 @@ def raise_cmdexc_if_invalid(url): if not url.isValid(): raise cmdexc.CommandError(get_errstring(url)) + def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): - """Check if path is a valid path + """Check if path is a valid path. Args: - pathstr: The path as string + pathstr: The path as string. Return: True if it is a valid path, False otherwise. From 0ab44c4f4a0b85662d8be482dc1fe0a7353ee1d0 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:41:20 +0100 Subject: [PATCH 08/14] Reformats code as requested --- qutebrowser/browser/commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index fd1e12990..312e7c534 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -821,9 +821,9 @@ class CommandDispatcher: log.misc.debug("{} contained: '{}'".format(target, text.replace('\n', '\\n'))) text_urls = [u for u in text.split('\n') if u.strip()] - if len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) \ - and urlutils.get_path_if_valid(text_urls[0], - check_exists=True) is not None: + if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and + urlutils.get_path_if_valid( + text_urls[0], check_exists=True) is None): text_urls = [text] for i, text_url in enumerate(text_urls): if not window and i > 0: From d8ad0a14af122e237bd63f9de53913930ede7385 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:42:14 +0100 Subject: [PATCH 09/14] Fixes wrong documentation --- qutebrowser/utils/urlutils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index e7e2ebe97..73f8d6569 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -342,9 +342,13 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): Args: pathstr: The path as string. + cwd: The current working directory, or None. + relative: Whether to resolve relative files. + check_exists: Whether to check if the file + actually exists of filesystem. Return: - True if it is a valid path, False otherwise. + The path if it is a valid path, None otherwise. """ pathstr = pathstr.strip() log.url.debug("Checking if '{}' is a path".format(pathstr)) From 89ac5cba6226b7989b50ce3b3f29fadc1673737c Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:43:38 +0100 Subject: [PATCH 10/14] Adds test to reach 100% coverage on urlutils --- tests/unit/utils/test_urlutils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index bb2778765..99e1eac56 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -238,6 +238,18 @@ class TestFuzzyUrl: with pytest.raises(urlutils.InvalidUrlError): urlutils.fuzzy_url(url, do_search=True) + @pytest.mark.parametrize('path, check_exists', [ + ('/foo', False), + ('/bar', True), + ]) + def test_get_path_existing(self, path, check_exists, os_mock): + """Test with an absolute path.""" + os_mock.path.exists.return_value = False + expected = None if check_exists else path + + url = urlutils.get_path_if_valid(path, check_exists=check_exists) + assert url == expected + @pytest.mark.parametrize('url, special', [ ('file:///tmp/foo', True), From a9fdf09a047b29158d7d97d389e42979793fa92f Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:44:26 +0100 Subject: [PATCH 11/14] Adds test of the heuristic --- tests/integration/features/yankpaste.feature | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/integration/features/yankpaste.feature b/tests/integration/features/yankpaste.feature index b1e329ce9..ce6ec2b1a 100644 --- a/tests/integration/features/yankpaste.feature +++ b/tests/integration/features/yankpaste.feature @@ -126,6 +126,18 @@ Feature: Yanking and pasting. - data/hello2.txt - data/hello3.txt + Scenario: Pasting multiline text + Given I have a fresh instance + When I set searchengines -> DEFAULT to http://localhost:(port)/data/hello.txt?q={} + And I put the following lines into the clipboard: + this url: + http://qutebrowser.org + should not open + And I run :paste -t + Then the following tabs should be open: + - about:blank + - data/hello.txt?q=this%20url%3A%0Ahttp%3A//qutebrowser.org%0Ashould%20not%20open (active) + Scenario: Pasting multiple urls in a background tab Given I open about:blank When I run :tab-only From 25bc2dc1db772d614d38824664a77566d8786e2d Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:45:02 +0100 Subject: [PATCH 12/14] Changes formatters to print multiline text on one line --- qutebrowser/browser/commands.py | 3 +-- qutebrowser/utils/urlutils.py | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 312e7c534..8b820c078 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -818,8 +818,7 @@ class CommandDispatcher: text = utils.get_clipboard(selection=sel) if not text.strip(): raise cmdexc.CommandError("{} is empty.".format(target)) - log.misc.debug("{} contained: '{}'".format(target, - text.replace('\n', '\\n'))) + log.misc.debug("{} contained: {!r}".format(target, text)) text_urls = [u for u in text.split('\n') if u.strip()] if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and urlutils.get_path_if_valid( diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 73f8d6569..ac9a49c7e 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -80,7 +80,7 @@ def _parse_search_term(s): engine = None term = s - log.url.debug("engine {}, term '{}'".format(engine, term)) + log.url.debug("engine {}, term {!r}".format(engine, term)) return (engine, term) @@ -93,7 +93,7 @@ def _get_search_url(txt): Return: The search URL as a QUrl. """ - log.url.debug("Finding search engine for '{}'".format(txt)) + log.url.debug("Finding search engine for {!r}".format(txt)) engine, term = _parse_search_term(txt) assert term if engine is None: @@ -186,7 +186,7 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True): url = _get_search_url(urlstr) except ValueError: # invalid search engine url = qurl_from_user_input(urlstr) - log.url.debug("Converting fuzzy term {} to URL -> {}".format( + log.url.debug("Converting fuzzy term {!r} to URL -> {}".format( urlstr, url.toDisplayString())) if do_search and config.get('general', 'auto-search') and urlstr: qtutils.ensure_valid(url) @@ -234,7 +234,7 @@ def is_url(urlstr): """ autosearch = config.get('general', 'auto-search') - log.url.debug("Checking if '{}' is a URL (autosearch={}).".format( + log.url.debug("Checking if {!r} is a URL (autosearch={}).".format( urlstr, autosearch)) urlstr = urlstr.strip() @@ -351,7 +351,7 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False): The path if it is a valid path, None otherwise. """ pathstr = pathstr.strip() - log.url.debug("Checking if '{}' is a path".format(pathstr)) + log.url.debug("Checking if {!r} is a path".format(pathstr)) expanded = os.path.expanduser(pathstr) if os.path.isabs(expanded): From 8f593d948cff3b4d2522af294dc10f562406effe Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Sun, 28 Feb 2016 23:56:23 +0100 Subject: [PATCH 13/14] Fixing flake8 error --- qutebrowser/browser/commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 8b820c078..c34c766a2 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -821,8 +821,8 @@ class CommandDispatcher: log.misc.debug("{} contained: {!r}".format(target, text)) text_urls = [u for u in text.split('\n') if u.strip()] if (len(text_urls) > 1 and not urlutils.is_url(text_urls[0]) and - urlutils.get_path_if_valid( - text_urls[0], check_exists=True) is None): + urlutils.get_path_if_valid( + text_urls[0], check_exists=True) is None): text_urls = [text] for i, text_url in enumerate(text_urls): if not window and i > 0: From 9286fadeee4bd59269817420fe43f09fda270336 Mon Sep 17 00:00:00 2001 From: Tarcisio Fedrizzi Date: Mon, 29 Feb 2016 00:39:37 +0100 Subject: [PATCH 14/14] Adds wait to paste multiline text test --- tests/integration/features/yankpaste.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/features/yankpaste.feature b/tests/integration/features/yankpaste.feature index ce6ec2b1a..8f6c6552f 100644 --- a/tests/integration/features/yankpaste.feature +++ b/tests/integration/features/yankpaste.feature @@ -134,6 +134,7 @@ Feature: Yanking and pasting. http://qutebrowser.org should not open And I run :paste -t + And I wait until data/hello.txt?q=this%20url%3A%0Ahttp%3A//qutebrowser.org%0Ashould%20not%20open is loaded Then the following tabs should be open: - about:blank - data/hello.txt?q=this%20url%3A%0Ahttp%3A//qutebrowser.org%0Ashould%20not%20open (active)