Warn about malformed dictionaries

This commit is contained in:
Michal Siedlaczek 2018-03-16 11:28:45 -04:00
parent 29eadf7141
commit f9e702bae5
2 changed files with 29 additions and 12 deletions

View File

@ -32,6 +32,10 @@ dict_version_re = re.compile(r".+-(?P<version>[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):

View File

@ -17,15 +17,20 @@
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
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):