Refactor suggested_fn_from_title, add unit tests
This commit is contained in:
parent
57e4d4978b
commit
3bfafb5e50
@ -1444,14 +1444,9 @@ class CommandDispatcher:
|
||||
else:
|
||||
qnam = tab.networkaccessmanager()
|
||||
|
||||
# Downloads of URLs with file extensions in the whitelist will use
|
||||
# the page title as the filename.
|
||||
ext_whitelist = [".html", ".htm", ".php", ""]
|
||||
_, ext = os.path.splitext(self._current_url().path())
|
||||
if ext.lower() in ext_whitelist and tab.title():
|
||||
suggested_fn = utils.sanitize_filename(tab.title()) + ext
|
||||
else:
|
||||
suggested_fn = None
|
||||
suggested_fn = downloads.suggested_fn_from_title(
|
||||
self._current_url().path(), tab.title()
|
||||
)
|
||||
|
||||
download_manager.get(
|
||||
self._current_url(),
|
||||
|
@ -182,6 +182,26 @@ def transform_path(path):
|
||||
return path
|
||||
|
||||
|
||||
def suggested_fn_from_title(url, title=None):
|
||||
"""Suggest a filename depending on the URL extension and page title.
|
||||
Args:
|
||||
url: a string with the URL path
|
||||
title: the page title string
|
||||
|
||||
Returns None if the extension is not in the whitelist
|
||||
or if there is no page title.
|
||||
"""
|
||||
ext_whitelist = [".html", ".htm", ".php", ""]
|
||||
_, ext = os.path.splitext(url)
|
||||
if ext.lower() in ext_whitelist and title:
|
||||
suggested_fn = utils.sanitize_filename(title)
|
||||
if not suggested_fn.endswith(ext):
|
||||
suggested_fn += ext
|
||||
else:
|
||||
suggested_fn = None
|
||||
return suggested_fn
|
||||
|
||||
|
||||
class NoFilenameError(Exception):
|
||||
|
||||
"""Raised when we can't find out a filename in DownloadTarget."""
|
||||
|
@ -36,13 +36,6 @@ Feature: Downloading things from a website.
|
||||
And I wait until the download is finished
|
||||
Then the downloaded file qutebrowser.png should exist
|
||||
|
||||
Scenario: Using :download with no URL and no page title
|
||||
When I set storage -> prompt-download-directory to false
|
||||
And I open data/downloads/download with no title.html
|
||||
And I run :download
|
||||
And I wait until the download is finished
|
||||
Then the downloaded file download with no title.html should exist
|
||||
|
||||
Scenario: Using hints
|
||||
When I set storage -> prompt-download-directory to false
|
||||
And I open data/downloads/downloads.html
|
||||
|
@ -30,6 +30,30 @@ def test_download_model(qapp, qtmodeltester, config_stub, cookiejar_and_cache):
|
||||
qtmodeltester.check(model)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('url, title, out', [
|
||||
('https://qutebrowser.org/img/cheatsheet-big.png',
|
||||
'cheatsheet-big.png (3342×2060)',
|
||||
None),
|
||||
('http://qutebrowser.org/INSTALL.html',
|
||||
'Installing qutebrowser | qutebrowser',
|
||||
'Installing qutebrowser _ qutebrowser.html'),
|
||||
('http://qutebrowser.org/INSTALL.html.html',
|
||||
'Installing qutebrowser | qutebrowser',
|
||||
'Installing qutebrowser _ qutebrowser.html'),
|
||||
('http://qutebrowser.org/',
|
||||
'qutebrowser | qutebrowser',
|
||||
'qutebrowser _ qutebrowser'),
|
||||
('https://github.com/qutebrowser/qutebrowser/releases',
|
||||
'Releases · qutebrowser/qutebrowser',
|
||||
'Releases · qutebrowser_qutebrowser'),
|
||||
('http://qutebrowser.org/page-with-no-title.html',
|
||||
'',
|
||||
None),
|
||||
])
|
||||
def test_page_titles(url, title, out):
|
||||
assert downloads.suggested_fn_from_title(url, title) == out
|
||||
|
||||
|
||||
class TestDownloadTarget:
|
||||
|
||||
def test_base(self):
|
||||
|
Loading…
Reference in New Issue
Block a user