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 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. """Suggest a filename depending on the URL extension and page title.
Args: Args:
url: a string with the URL path url_path: a string with the URL path
title: the page title string title: the page title string
Returns None if the extension is not in the whitelist Return:
or if there is no page title. 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_whitelist = [".html", ".htm", ".php", ""]
_, ext = os.path.splitext(url) _, ext = os.path.splitext(url_path)
if ext.lower() in ext_whitelist and title: if ext.lower() in ext_whitelist and title:
suggested_fn = utils.sanitize_filename(title) suggested_fn = utils.sanitize_filename(title)
if not suggested_fn.endswith(ext): if not suggested_fn.lower().endswith(ext.lower()):
suggested_fn += ext suggested_fn += ext
else: else:
suggested_fn = None 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', [ @pytest.mark.parametrize('url, title, out', [
('https://qutebrowser.org/img/cheatsheet-big.png',
'cheatsheet-big.png (3342×2060)',
None),
('http://qutebrowser.org/INSTALL.html', ('http://qutebrowser.org/INSTALL.html',
'Installing qutebrowser | qutebrowser', 'Installing qutebrowser | qutebrowser',
'Installing qutebrowser _ qutebrowser.html'), 'Installing qutebrowser _ qutebrowser.html'),
('http://qutebrowser.org/INSTALL.html.html', ('http://qutebrowser.org/INSTALL.html',
'Installing qutebrowser | qutebrowser', 'Installing qutebrowser | qutebrowser.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/', ('http://qutebrowser.org/',
'qutebrowser | qutebrowser', 'qutebrowser | qutebrowser',
'qutebrowser _ qutebrowser'), 'qutebrowser _ qutebrowser'),
('https://github.com/qutebrowser/qutebrowser/releases', ('https://github.com/qutebrowser/qutebrowser/releases',
'Releases · qutebrowser/qutebrowser', 'Releases · qutebrowser/qutebrowser',
'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', ('http://qutebrowser.org/page-with-no-title.html',
'', '',
None), None),