From 3da21a32d2b31c4f486dac6f42fdadd0656ddf44 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 23 Mar 2017 14:59:22 +0100 Subject: [PATCH] treat E: and E:\ the same when downloading Fixes #2305 --- qutebrowser/browser/downloads.py | 2 ++ qutebrowser/browser/webkit/mhtml.py | 2 ++ qutebrowser/utils/utils.py | 19 +++++++++++++++++++ tests/unit/utils/test_utils.py | 13 +++++++++++++ 4 files changed, 36 insertions(+) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 51ccdbfeb..8820ffb9c 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -583,6 +583,8 @@ class AbstractDownloadItem(QObject): """ global last_used_directory filename = os.path.expanduser(filename) + if sys.platform == "win32": + filename = utils.expand_windows_drive(filename) self._ensure_can_set_filename(filename) self._filename = create_full_filename(self.basename, filename) diff --git a/qutebrowser/browser/webkit/mhtml.py b/qutebrowser/browser/webkit/mhtml.py index bc9ea2695..79b0dd320 100644 --- a/qutebrowser/browser/webkit/mhtml.py +++ b/qutebrowser/browser/webkit/mhtml.py @@ -554,6 +554,8 @@ def start_download_checked(target, tab): dest = utils.force_encoding(target.filename, encoding) dest = os.path.expanduser(dest) + if sys.platform == "win32": + dest = utils.expand_windows_drive(dest) # See if we already have an absolute path path = downloads.create_full_filename(default_name, dest) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index fd4466e2a..2aca78168 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -20,6 +20,7 @@ """Other utilities which don't fit anywhere else.""" import io +import re import sys import enum import json @@ -845,3 +846,21 @@ def open_file(filename, cmdline=None): def unused(_arg): """Function which does nothing to avoid pylint complaining.""" pass + + +def expand_windows_drive(path): + r"""Expand a drive-path like E: into E:\. + + Does nothing for other paths. + + Args: + path: The path to expand. + """ + # Usually, "E:" on Windows refers to the current working directory on drive + # E:\. The correct way to specifify drive E: is "E:\", but most users + # probably don't use the "multiple working directories" feature and expect + # "E:" and "E:\" to be equal. + if re.match(r'[A-Z]:$', path, re.IGNORECASE): + return path + "\\" + else: + return path diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 12330aa73..c60b640d6 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -916,3 +916,16 @@ class TestOpenFile: def test_unused(): utils.unused(None) + + +@pytest.mark.parametrize('path, expected', [ + ('E:', 'E:\\'), + ('e:', 'e:\\'), + ('E:foo', 'E:foo'), + ('E:\\', 'E:\\'), + ('E:\\foo', 'E:\\foo'), + ('foo:', 'foo:'), + ('foo:bar', 'foo:bar'), +]) +def test_expand_windows_drive(path, expected): + assert utils.expand_windows_drive(path) == expected