From 2dc0115c8129eddcba117c8f9f868986b34b0c9c Mon Sep 17 00:00:00 2001 From: Michal Siedlaczek Date: Fri, 3 Nov 2017 19:20:31 -0400 Subject: [PATCH] Load the newest version of the dictionary. --- qutebrowser/browser/webengine/spell.py | 20 +++++++++++++++++--- tests/unit/browser/webengine/test_spell.py | 9 +++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/qutebrowser/browser/webengine/spell.py b/qutebrowser/browser/webengine/spell.py index a915b1d52..994982315 100644 --- a/qutebrowser/browser/webengine/spell.py +++ b/qutebrowser/browser/webengine/spell.py @@ -21,10 +21,22 @@ import glob import os +import re +from qutebrowser.utils import log from PyQt5.QtCore import QLibraryInfo +def version(filename): + """Extract the version number from the dictionary file name.""" + version_re = re.compile(r""" + .+(?P[0-9]+-[0-9]+)\.bdic + """, re.VERBOSE) + match = version_re.match(filename) + assert match is not None, 'the given dictionary file name is malformed' + return [int(n) for n in match.group('version').split('-')] + + def dictionary_dir(): """Return the path (str) to the QtWebEngine's dictionaries directory.""" datapath = QLibraryInfo.location(QLibraryInfo.DataPath) @@ -32,14 +44,16 @@ def dictionary_dir(): def installed_file(code): - """Return the installed dictionary for the given code. + """Return the newest installed dictionary for the given code. - Return the filename of the installed dictionary or None - if the dictionary is not installed. + Return the filename of the installed dictionary with the highest version + number or None if the dictionary is not installed. """ pathname = os.path.join(dictionary_dir(), '{}*.bdic'.format(code)) matching_dicts = glob.glob(pathname) if matching_dicts: + log.config.debug('Found files for dict {}: {}'.format(code, matching_dicts)) + matching_dicts = sorted(matching_dicts, key=version) with_extension = os.path.basename(matching_dicts[0]) return os.path.splitext(with_extension)[0] else: diff --git a/tests/unit/browser/webengine/test_spell.py b/tests/unit/browser/webengine/test_spell.py index 2aebeffd5..39f6e8601 100644 --- a/tests/unit/browser/webengine/test_spell.py +++ b/tests/unit/browser/webengine/test_spell.py @@ -21,6 +21,11 @@ from qutebrowser.browser.webengine import spell +def test_version(): + assert spell.version('en-US-8-0.bdic') == [8, 0] + assert spell.version('pl-PL-3-0.bdic') == [3, 0] + + def test_installed_file_dictionary_does_not_exist(tmpdir, monkeypatch): monkeypatch.setattr( spell, 'dictionary_dir', lambda: '/some-non-existing-dir') @@ -34,7 +39,7 @@ def test_installed_file_dictionary_not_installed(tmpdir, monkeypatch): def test_installed_file_dictionary_installed(tmpdir, monkeypatch): monkeypatch.setattr(spell, 'dictionary_dir', lambda: str(tmpdir)) - for lang_file in ['en-US-7-1.bdic', 'pl-PL-3-0.bdic']: + for lang_file in ['en-US-11-0.bdic', 'en-US-7-1.bdic', 'pl-PL-3-0.bdic']: (tmpdir / lang_file).ensure() - assert spell.installed_file('en-US') == 'en-US-7-1' + assert spell.installed_file('en-US') == 'en-US-11-0' assert spell.installed_file('pl-PL') == 'pl-PL-3-0'