Skip to content

Commit 5fafb71

Browse files
committed
Add argument id/str for -s/--spelling to check messages or translations
1 parent 2d48619 commit 5fafb71

4 files changed

Lines changed: 26 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Options:
3939
* `-f`, `--fuzzy`: check fuzzy strings
4040
* `-l`, `--no-lines`: do not check number of lines
4141
* `-p`, `--no-punct`: do not check punctuation at end of strings
42-
* `-s`, `--spelling`: check spelling
42+
* `-s id|str`, `--spelling id|str`: check spelling (`id` = source messages,
43+
`str` = translations)
4344
* `-d <dicts>`, `--dicts <dicts>`: comma-separated list of extra dictionaries
4445
to use (in addition to file language)
4546
* `-P <file>`, `--pwl <file>`: file with personal word list used when checking

msgcheck/msgcheck.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* whitespace at beginning/end of strings
2727
* trailing whitespace at end of lines inside strings
2828
* punctuation at end of strings
29-
* spelling
29+
* spelling (messages and translations)
3030
"""
3131

3232
from __future__ import print_function
@@ -69,7 +69,7 @@ def msgcheck_parser():
6969
help='do not check number of lines')
7070
parser.add_argument('-p', '--no-punct', action='store_true',
7171
help='do not check punctuation at end of strings')
72-
parser.add_argument('-s', '--spelling', action='store_true',
72+
parser.add_argument('-s', '--spelling', choices=['id', 'str'],
7373
help='check spelling')
7474
parser.add_argument('-d', '--dicts',
7575
help='comma-separated list of extra dictionaries '
@@ -110,18 +110,17 @@ def main():
110110
args = parser.parse_args(
111111
shlex.split(os.getenv('MSGCHECK_OPTIONS') or '') + sys.argv[1:])
112112

113-
# create checker and set options
113+
# create checker and set boolean options
114114
po_check = PoCheck()
115115
for option in ('no_compile', 'fuzzy', 'no_lines', 'no_punct',
116-
'no_whitespace', 'no_whitespace_eol', 'spelling',
117-
'extract'):
116+
'no_whitespace', 'no_whitespace_eol', 'extract'):
118117
if args.__dict__[option]:
119118
po_check.set_check(option.lstrip('no_'),
120119
not option.startswith('no_'))
121120

122121
# check all files
123122
try:
124-
po_check.set_spelling_options(args.dicts, args.pwl)
123+
po_check.set_spelling_options(args.spelling, args.dicts, args.pwl)
125124
result = po_check.check_files(args.file)
126125
except (ImportError, IOError) as exc:
127126
print('FATAL:', exc, sep=' ')
@@ -136,7 +135,7 @@ def main():
136135
if not args.quiet:
137136
if args.only_misspelled:
138137
print('\n'.join([report.message for report in reports
139-
if report.idmsg == 'spelling']))
138+
if report.idmsg.startswith('spelling-')]))
140139
else:
141140
print('\n'.join([str(report) for report in reports]))
142141
else:

msgcheck/po.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def check_whitespace_eol(self):
253253
break
254254
return errors
255255

256-
def check_spelling(self, checkers):
256+
def check_spelling(self, spelling, checkers):
257257
"""
258258
Check spelling.
259259
Return a list with errors detected.
@@ -264,7 +264,7 @@ def check_spelling(self, checkers):
264264
for mid, mstr in self.messages:
265265
if not mid or not mstr:
266266
continue
267-
checkers[0].set_text(mstr)
267+
checkers[0].set_text(mstr if spelling == 'str' else mid)
268268
misspelled = []
269269
for err in checkers[0]:
270270
misspelled_word = True
@@ -275,8 +275,8 @@ def check_spelling(self, checkers):
275275
if misspelled_word:
276276
misspelled.append(err.word)
277277
for word in misspelled:
278-
errors.append(PoReport(word, 'spelling', self.filename,
279-
self.line, mid, mstr))
278+
errors.append(PoReport(word, 'spelling-' + spelling,
279+
self.filename, self.line, mid, mstr))
280280
return errors
281281

282282

@@ -383,11 +383,11 @@ def __init__(self):
383383
'punct': True,
384384
'whitespace': True,
385385
'whitespace_eol': True,
386-
'spelling': False,
387386
'extract': False,
388387
}
389388

390389
# spelling options
390+
self.spelling = None
391391
self.dicts = None
392392
self.extra_checkers = []
393393
self.pwl = None
@@ -403,8 +403,9 @@ def set_check(self, check, state):
403403
if check in self.checks:
404404
self.checks[check] = bool(state)
405405

406-
def set_spelling_options(self, dicts, pwl):
406+
def set_spelling_options(self, spelling, dicts, pwl):
407407
"""Set spelling options."""
408+
self.spelling = spelling
408409
self.dicts = dicts
409410
self.pwl = pwl
410411

@@ -429,17 +430,19 @@ def set_spelling_options(self, dicts, pwl):
429430
def _get_language_checker(self, po_file, reports):
430431
"""Get checker for PO file language."""
431432
checker = []
432-
if self.checks['spelling']:
433+
if self.spelling:
433434
if not ENCHANT_FOUND:
434435
raise ImportError('Enchant module not found (please install '
435436
'"pyenchant")')
437+
lang = po_file.props['language'] \
438+
if self.spelling == 'str' else 'en'
436439
try:
437-
_dict = DictWithPWL(po_file.props['language'], self.pwl)
440+
_dict = DictWithPWL(lang, self.pwl)
438441
checker.append(SpellChecker(_dict))
439442
except DictNotFoundError:
440443
reports.append(PoReport(
441444
'enchant dictionary not found for language "{0}"'
442-
''.format(po_file.props['language']),
445+
''.format(lang),
443446
'dict', po_file.filename,
444447
po_file.props['language_numline']))
445448
checker = []
@@ -479,9 +482,9 @@ def check_pofile(self, po_file):
479482
reports += msg.check_whitespace()
480483
if self.checks['whitespace_eol']:
481484
reports += msg.check_whitespace_eol()
482-
if self.checks['spelling']:
485+
if self.spelling:
483486
reports += msg.check_spelling(
484-
checker + self.extra_checkers)
487+
self.spelling, checker + self.extra_checkers)
485488

486489
return reports
487490

tests/test_msgcheck.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def test_checks_fuzzy(self):
9898
def test_spelling(self):
9999
"""Test spelling on gettext files."""
100100
po_check = PoCheck()
101-
po_check.set_check('spelling', True)
102-
po_check.set_spelling_options(None, local_path('pwl.txt'))
101+
po_check.set_spelling_options('str', None, local_path('pwl.txt'))
103102
result = po_check.check_files([local_path('fr_spelling.po'),
104103
local_path('fr_language.po')])
105104

@@ -110,7 +109,7 @@ def test_spelling(self):
110109
errors = result[0][1]
111110
self.assertEquals(len(errors), 2)
112111
for i, word in enumerate(('aabbcc', 'xxyyzz')):
113-
self.assertEquals(errors[i].idmsg, 'spelling')
112+
self.assertEquals(errors[i].idmsg, 'spelling-str')
114113
self.assertEquals(errors[i].message, word)
115114

116115
# second file has 1 error: dict/language "xyz" not found
@@ -121,15 +120,14 @@ def test_spelling(self):
121120
def test_spelling_bad_dict(self):
122121
"""Test spelling with a bad dict option."""
123122
po_check = PoCheck()
124-
po_check.set_spelling_options('xxx', None)
123+
po_check.set_spelling_options('str', 'xxx', None)
125124
self.assertEquals(len(po_check.extra_checkers), 0)
126125

127126
def test_spelling_bad_pwl(self):
128127
"""Test spelling with a bad pwl option."""
129128
po_check = PoCheck()
130-
po_check.set_check('spelling', True)
131129
try:
132-
po_check.set_spelling_options(None,
130+
po_check.set_spelling_options('str', None,
133131
local_path('pwl_does_not_exist.txt'))
134132
except IOError:
135133
pass # this exception is expected

0 commit comments

Comments
 (0)