Skip to content

Commit 2ef2223

Browse files
authored
Merge pull request #7 from amureki/noqa_flag
Add skip_noqa argument to skip "noqa"-commented lines (fix #2)
2 parents d699d99 + 5654e5d commit 2ef2223

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Options:
4747
* `-h`, `--help`: display help message and exit
4848
* `-c`, `--no-compile`: do not check compilation of file (with `msgfmt -c`)
4949
* `-f`, `--fuzzy`: check fuzzy strings
50+
* `-n`, `--skip-noqa`: do not check "noqa"-commented lines
5051
* `-l`, `--no-lines`: do not check number of lines
5152
* `-p`, `--no-punct`: do not check punctuation at end of strings
5253
* `-s id|str`, `--spelling id|str`: check spelling (`id` = source messages,

msgcheck/msgcheck.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def msgcheck_parser():
6565
help='do not check compilation of file')
6666
parser.add_argument('-f', '--fuzzy', action='store_true',
6767
help='check fuzzy strings')
68+
parser.add_argument('-n', '--skip-noqa', action='store_true',
69+
help='do not check "noqa"-commented lines')
6870
parser.add_argument('-l', '--no-lines', action='store_true',
6971
help='do not check number of lines')
7072
parser.add_argument('-p', '--no-punct', action='store_true',
@@ -112,7 +114,7 @@ def main():
112114

113115
# create checker and set boolean options
114116
po_check = PoCheck()
115-
for option in ('no_compile', 'fuzzy', 'no_lines', 'no_punct',
117+
for option in ('no_compile', 'fuzzy', 'skip_noqa', 'no_lines', 'no_punct',
116118
'no_whitespace', 'no_whitespace_eol', 'extract'):
117119
if args.__dict__[option]:
118120
po_check.set_check(option.lstrip('no_'),

msgcheck/po.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class PoMessage(object):
110110
"""
111111

112112
# pylint: disable=too-many-arguments
113-
def __init__(self, filename, line, msg, charset, fuzzy, fmt):
113+
def __init__(self, filename, line, msg, charset, fuzzy, fmt, noqa):
114114
"""Build a PO message."""
115115
self.filename = filename
116116
self.line = line
@@ -136,6 +136,7 @@ def __init__(self, filename, line, msg, charset, fuzzy, fmt):
136136
self.messages.append((msg.get('msgid', ''), msg.get('msgstr', '')))
137137
self.fuzzy = fuzzy
138138
self.fmt = fmt
139+
self.noqa = noqa
139140

140141
def check_lines(self):
141142
"""
@@ -297,7 +298,8 @@ def __init__(self, filename):
297298
}
298299
self.msgs = []
299300

300-
def _add_message(self, numline_msgid, fuzzy, fmt, msg):
301+
# pylint: disable=too-many-arguments
302+
def _add_message(self, numline_msgid, fuzzy, fmt, noqa, msg):
301303
"""
302304
Add a message from PO file in list of messages.
303305
"""
@@ -314,7 +316,7 @@ def _add_message(self, numline_msgid, fuzzy, fmt, msg):
314316
if match:
315317
self.props['charset'] = match.group(1)
316318
self.msgs.append(PoMessage(self.filename, numline_msgid, msg,
317-
self.props['charset'], fuzzy, fmt))
319+
self.props['charset'], fuzzy, fmt, noqa))
318320

319321
def read(self):
320322
"""
@@ -323,6 +325,7 @@ def read(self):
323325
self.msgs = []
324326
numline, numline_msgid = (0, 0)
325327
fuzzy, msgfuzzy = (False, False)
328+
noqa, msgnoqa = (False, False)
326329
fmt, msgfmt = (None, None)
327330
msg = {}
328331
msgcurrent = ''
@@ -337,6 +340,7 @@ def read(self):
337340
match = re.search(r'([a-z-]+)-format', line, re.IGNORECASE)
338341
fmt = match.group(1) if match else None
339342
if line.startswith('#'):
343+
noqa = 'noqa' in line
340344
continue
341345
if line.startswith('msg'):
342346
match = re.match(
@@ -351,9 +355,12 @@ def read(self):
351355
self._add_message(numline_msgid,
352356
msgfuzzy,
353357
msgfmt,
358+
msgnoqa,
354359
msg)
355360
msgfuzzy = fuzzy
361+
msgnoqa = noqa
356362
fuzzy = False
363+
noqa = False
357364
msgfmt = fmt
358365
fmt = None
359366
msg = {}
@@ -364,6 +371,7 @@ def read(self):
364371
self._add_message(numline_msgid,
365372
msgfuzzy,
366373
msgfmt,
374+
msgnoqa,
367375
msg)
368376

369377
def compile(self):
@@ -389,6 +397,7 @@ def __init__(self):
389397
self.checks = {
390398
'compile': True,
391399
'fuzzy': False,
400+
'skip_noqa': False,
392401
'lines': True,
393402
'punct': True,
394403
'whitespace': True,
@@ -478,7 +487,10 @@ def check_pofile(self, po_file):
478487

479488
# check all messages
480489
check_fuzzy = self.checks['fuzzy']
490+
skip_noqa = self.checks['skip_noqa']
481491
for msg in po_file.msgs:
492+
if skip_noqa and msg.noqa:
493+
continue
482494
if msg.fuzzy and not check_fuzzy:
483495
continue
484496
if self.checks['extract']:

tests/fr_errors.po

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ msgstr "Test 2 sur deux lignes.\nLigne 2.\nLigne 3."
4848
msgid "Tested 1."
4949
msgstr "Testé 1"
5050

51+
#, noqa
5152
msgid "Tested 2"
5253
msgstr "Testé 2."
5354

tests/test_msgcheck.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ def test_checks_fuzzy(self):
9696
# the file has 11 errors (with the fuzzy string)
9797
self.assertEqual(len(result[0][1]), 11)
9898

99+
def test_checks_noqa(self):
100+
"""Test checks on a gettext file ignoring `noqa`-commented lines."""
101+
po_check = PoCheck()
102+
po_check.set_check('skip_noqa', True)
103+
result = po_check.check_files([local_path('fr_errors.po')])
104+
# be sure we have one file in result
105+
self.assertEqual(len(result), 1)
106+
107+
# the file has 9 errors (`noqa` was skipped)
108+
self.assertEqual(len(result[0][1]), 9)
109+
99110
def test_replace_formatters_c(self):
100111
"""Test removal of formatters in a C string."""
101112
self.assertEqual(replace_formatters('%s', 'c'), '')

0 commit comments

Comments
 (0)