Skip to content
Snippets Groups Projects
Commit 98cc2fe0 authored by Sébastien Helleu's avatar Sébastien Helleu
Browse files

Add support of multiple personal word list files (multiple options -P/--pwl) (closes #5)

parent 1279d149
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -4,6 +4,10 @@
:lang: en
 
 
== Version 3.0 (under dev)
* add support of multiple personal word list files (multiple options -P/--pwl) (issue #5)
== Version 2.9 (2018-01-15)
 
* add option "-n" (or "--skip-noqa") to not check "noqa"-commented lines (issue #2, issue #7)
Loading
Loading
Loading
Loading
@@ -76,9 +76,10 @@ The script returns:
parser.add_argument('-d', '--dicts',
help='comma-separated list of extra dictionaries '
'to use (in addition to file language)')
parser.add_argument('-P', '--pwl',
help='file with personal list of words used when '
'checking spelling')
parser.add_argument('-P', '--pwl', action='append',
help='file(s) with personal list of words used when '
'checking spelling (this option can be given multiple '
'times)')
parser.add_argument('-m', '--only-misspelled', action='store_true',
help='display only misspelled words (no error, '
'line number and translation)')
Loading
Loading
Loading
Loading
@@ -29,6 +29,7 @@ import os
import re
import subprocess
import sys
import tempfile
 
# enchant module is optional, spelling is checked on demand
# (argument -s/--spell)
Loading
Loading
@@ -42,6 +43,19 @@ except ImportError:
from . utils import count_lines, replace_formatters
 
 
def build_temp_file_concat_files(filenames):
"""Build a temporary file with concatenation of multiple files."""
if not filenames:
return None
tmp_file = tempfile.NamedTemporaryFile()
for filename in filenames:
if not os.path.isfile(filename):
raise IOError('file "{0}" not found'.format(filename))
tmp_file.write(open(filename, 'rb').read())
tmp_file.flush()
return tmp_file
class PoReport(object): # pylint: disable=too-few-public-methods
"""A message in report (commonly an error in detected in gettext file)."""
 
Loading
Loading
@@ -420,15 +434,11 @@ class PoCheck(object):
if check in self.checks:
self.checks[check] = bool(state)
 
def set_spelling_options(self, spelling, dicts, pwl):
def set_spelling_options(self, spelling, dicts, pwl_files):
"""Set spelling options."""
self.spelling = spelling
self.dicts = dicts
self.pwl = pwl
# check if pwl file exists
if pwl and not os.path.isfile(pwl):
raise IOError('pwl file "{0}" not found'.format(pwl))
self.pwl = build_temp_file_concat_files(pwl_files)
 
# build extra checkers with dicts
self.extra_checkers = []
Loading
Loading
@@ -454,7 +464,7 @@ class PoCheck(object):
lang = po_file.props['language'] \
if self.spelling == 'str' else 'en'
try:
_dict = DictWithPWL(lang, self.pwl)
_dict = DictWithPWL(lang, self.pwl.name if self.pwl else None)
checker.append(SpellChecker(_dict))
except DictNotFoundError:
reports.append(PoReport(
Loading
Loading
Loading
Loading
@@ -36,8 +36,12 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
#, c-format
msgid "%.3fThsi is a test.\n"
msgstr "%.3fCeci est un test.\n"
msgid "%.3fThsi is a testone.\n"
msgstr "%.3fCeci est un testone.\n"
#, c-format
msgid "%.3fThis is a testtwo.\n"
msgstr "%.3fCeci est un testtwo.\n"
 
msgid "Test 1"
msgstr "Test 1"
Loading
Loading
Loading
Loading
@@ -36,8 +36,12 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
#, c-format
msgid "%.3fThis is a test.\n"
msgstr "%.3fCeci est un test.\n"
msgid "%.3fThis is a testone.\n"
msgstr "%.3fCeci est un testone.\n"
#, c-format
msgid "%.3fThis is a testtwo.\n"
msgstr "%.3fCeci est un testtwo.\n"
 
#, c-format
msgid "%.3fThis is another test.\n"
Loading
Loading
test
testone
testtwo
Loading
Loading
@@ -160,7 +160,33 @@ class TestMsgCheck(unittest.TestCase): # pylint: disable=too-many-public-method
def test_spelling_id(self):
"""Test spelling on source messages (English) of gettext files."""
po_check = PoCheck()
po_check.set_spelling_options('id', None, local_path('pwl.txt'))
pwl_files = [local_path('pwl1.txt')]
po_check.set_spelling_options('id', None, pwl_files)
result = po_check.check_files([local_path('fr_spelling_id.po')])
# be sure we have 1 file in result
self.assertEqual(len(result), 1)
# the file has 2 spelling errors: "Thsi" and "errro"
errors = result[0][1]
self.assertEqual(len(errors), 3)
for i, word in enumerate(('Thsi', 'testtwo', 'errro')):
self.assertEqual(errors[i].idmsg, 'spelling-id')
self.assertTrue(isinstance(errors[i].message, list))
self.assertEqual(len(errors[i].message), 1)
self.assertEqual(errors[i].message[0], word)
def test_spelling_id_multilpe_pwl(self):
"""
Test spelling on source messages (English) of gettext files
using multiple personal word lists.
"""
po_check = PoCheck()
pwl_files = [
local_path('pwl1.txt'),
local_path('pwl2.txt'),
]
po_check.set_spelling_options('id', None, pwl_files)
result = po_check.check_files([local_path('fr_spelling_id.po')])
 
# be sure we have 1 file in result
Loading
Loading
@@ -178,7 +204,39 @@ class TestMsgCheck(unittest.TestCase): # pylint: disable=too-many-public-method
def test_spelling_str(self):
"""Test spelling on translated messages of gettext files."""
po_check = PoCheck()
po_check.set_spelling_options('str', None, local_path('pwl.txt'))
pwl_files = [local_path('pwl1.txt')]
po_check.set_spelling_options('str', None, pwl_files)
result = po_check.check_files([local_path('fr_spelling_str.po'),
local_path('fr_language.po')])
# be sure we have 2 files in result
self.assertEqual(len(result), 2)
# first file has 3 spelling errors: "CecX", "aabbcc" and "xxyyzz"
errors = result[0][1]
self.assertEqual(len(errors), 4)
for i, word in enumerate(('testtwo', 'CecX', 'aabbcc', 'xxyyzz')):
self.assertEqual(errors[i].idmsg, 'spelling-str')
self.assertTrue(isinstance(errors[i].message, list))
self.assertEqual(len(errors[i].message), 1)
self.assertEqual(errors[i].message[0], word)
# second file has 1 error: dict/language "xyz" not found
errors = result[1][1]
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0].idmsg, 'dict')
def test_spelling_str_multiple_pwl(self):
"""
Test spelling on translated messages of gettext files
using multiple personal word lists.
"""
po_check = PoCheck()
pwl_files = [
local_path('pwl1.txt'),
local_path('pwl2.txt'),
]
po_check.set_spelling_options('str', None, pwl_files)
result = po_check.check_files([local_path('fr_spelling_str.po'),
local_path('fr_language.po')])
 
Loading
Loading
@@ -209,8 +267,8 @@ class TestMsgCheck(unittest.TestCase): # pylint: disable=too-many-public-method
"""Test spelling with a bad pwl option."""
po_check = PoCheck()
try:
po_check.set_spelling_options('str', None,
local_path('pwl_does_not_exist.txt'))
pwl_files = [local_path('pwl_does_not_exist.txt')]
po_check.set_spelling_options('str', None, pwl_files)
except IOError:
pass # this exception is expected
else:
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment