diff --git a/qutebrowser/browser/webengine/spell.py b/qutebrowser/browser/webengine/spell.py index b887a2295..5c2ed551b 100644 --- a/qutebrowser/browser/webengine/spell.py +++ b/qutebrowser/browser/webengine/spell.py @@ -32,6 +32,10 @@ dict_version_re = re.compile(r".+-(?P[0-9]+-[0-9]+?)\.bdic") def version(filename): """Extract the version number from the dictionary file name.""" match = dict_version_re.match(filename) + if match is None: + message.warning( + "Found a dictionary with a malformed name: {}".format(filename)) + return None return tuple(int(n) for n in match.group('version').split('-')) @@ -42,15 +46,23 @@ def dictionary_dir(): def local_files(code): - """Return all installed dictionaries for the given code.""" + """Return all installed dictionaries for the given code. + + The returned dictionaries are sorted by version, therefore the latest will + be the first element. The list will be empty if no dictionaries are found. + """ pathname = os.path.join(dictionary_dir(), '{}*.bdic'.format(code)) - matching_dicts = filter(dict_version_re.match, glob.glob(pathname)) - files = [] - for matching_dict in sorted(matching_dicts, key=version, reverse=True): - filename = os.path.basename(matching_dict) - log.config.debug('Found file for dict {}: {}'.format(code, filename)) - files.append(filename) - return files + matching_dicts = glob.glob(pathname) + versioned_dicts = [] + for matching_dict in matching_dicts: + parsed_version = version(matching_dict) + if parsed_version is not None: + filename = os.path.basename(matching_dict) + log.config.debug('Found file for dict {}: {}' + .format(code, filename)) + versioned_dicts.append((parsed_version, filename)) + return [filename for version, filename + in sorted(versioned_dicts, reverse=True)] def local_filename(code): diff --git a/tests/unit/browser/webengine/test_spell.py b/tests/unit/browser/webengine/test_spell.py index efe62fb20..ec2831cc6 100644 --- a/tests/unit/browser/webengine/test_spell.py +++ b/tests/unit/browser/webengine/test_spell.py @@ -17,15 +17,20 @@ # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see . -import pytest +import logging + from qutebrowser.browser.webengine import spell +from qutebrowser.utils import usertypes -def test_version(): +def test_version(message_mock, caplog): assert spell.version('en-US-8-0.bdic') == (8, 0) assert spell.version('pl-PL-3-0.bdic') == (3, 0) - with pytest.raises(ValueError): - spell.version('malformed_filename') + with caplog.at_level(logging.WARNING): + assert spell.version('malformed_filename') is None + msg = message_mock.getmsg(usertypes.MessageLevel.warning) + expected = ("Found a dictionary with a malformed name: malformed_filename") + assert msg.text == expected def test_local_filename_dictionary_does_not_exist(tmpdir, monkeypatch):