Load the newest version of the dictionary.

This commit is contained in:
Michal Siedlaczek 2017-11-03 19:20:31 -04:00
parent 16ad9182f1
commit 2dc0115c81
2 changed files with 24 additions and 5 deletions

View File

@ -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<version>[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:

View File

@ -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'