Improve function docstring, add more tests

This commit is contained in:
Iordanis Grigoriou 2017-07-06 21:37:11 +02:00
parent 3bfafb5e50
commit 82d194cf2e
2 changed files with 19 additions and 11 deletions

View File

@ -182,20 +182,22 @@ def transform_path(path):
return path
def suggested_fn_from_title(url, title=None):
def suggested_fn_from_title(url_path, title=None):
"""Suggest a filename depending on the URL extension and page title.
Args:
url: a string with the URL path
url_path: 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.
Return:
The download filename based on the title, or None if the extension is
not found in the whitelist (or if there is no page title).
"""
ext_whitelist = [".html", ".htm", ".php", ""]
_, ext = os.path.splitext(url)
_, ext = os.path.splitext(url_path)
if ext.lower() in ext_whitelist and title:
suggested_fn = utils.sanitize_filename(title)
if not suggested_fn.endswith(ext):
if not suggested_fn.lower().endswith(ext.lower()):
suggested_fn += ext
else:
suggested_fn = None

View File

@ -31,21 +31,27 @@ def test_download_model(qapp, qtmodeltester, config_stub, cookiejar_and_cache):
@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',
('http://qutebrowser.org/INSTALL.html',
'Installing qutebrowser | qutebrowser.html',
'Installing qutebrowser _ qutebrowser.html'),
('http://qutebrowser.org/INSTALL.HTML',
'Installing qutebrowser | qutebrowser',
'Installing qutebrowser _ qutebrowser.HTML'),
('http://qutebrowser.org/INSTALL.html',
'Installing qutebrowser | qutebrowser.HTML',
'Installing qutebrowser _ qutebrowser.HTML'),
('http://qutebrowser.org/',
'qutebrowser | qutebrowser',
'qutebrowser _ qutebrowser'),
('https://github.com/qutebrowser/qutebrowser/releases',
'Releases · qutebrowser/qutebrowser',
'Releases · qutebrowser_qutebrowser'),
('https://qutebrowser.org/img/cheatsheet-big.png',
'cheatsheet-big.png (3342×2060)',
None),
('http://qutebrowser.org/page-with-no-title.html',
'',
None),