diff --git a/README.adoc b/README.adoc index 253dbbe..22003ab 100644 --- a/README.adoc +++ b/README.adoc @@ -47,6 +47,7 @@ Options: * `-h`, `--help`: display help message and exit * `-c`, `--no-compile`: do not check compilation of file (with `msgfmt -c`) * `-f`, `--fuzzy`: check fuzzy strings +* `-n`, `--skip-noqa`: do not check "noqa"-commented lines * `-l`, `--no-lines`: do not check number of lines * `-p`, `--no-punct`: do not check punctuation at end of strings * `-s id|str`, `--spelling id|str`: check spelling (`id` = source messages, diff --git a/msgcheck/msgcheck.py b/msgcheck/msgcheck.py index dc65d6d..d2152c0 100644 --- a/msgcheck/msgcheck.py +++ b/msgcheck/msgcheck.py @@ -65,6 +65,8 @@ def msgcheck_parser(): help='do not check compilation of file') parser.add_argument('-f', '--fuzzy', action='store_true', help='check fuzzy strings') + parser.add_argument('-n', '--skip-noqa', action='store_true', + help='do not check "noqa"-commented lines') parser.add_argument('-l', '--no-lines', action='store_true', help='do not check number of lines') parser.add_argument('-p', '--no-punct', action='store_true', @@ -112,7 +114,7 @@ def main(): # create checker and set boolean options po_check = PoCheck() - for option in ('no_compile', 'fuzzy', 'no_lines', 'no_punct', + for option in ('no_compile', 'fuzzy', 'skip_noqa', 'no_lines', 'no_punct', 'no_whitespace', 'no_whitespace_eol', 'extract'): if args.__dict__[option]: po_check.set_check(option.lstrip('no_'), diff --git a/msgcheck/po.py b/msgcheck/po.py index 093df8a..e979669 100644 --- a/msgcheck/po.py +++ b/msgcheck/po.py @@ -110,7 +110,7 @@ class PoMessage(object): """ # pylint: disable=too-many-arguments - def __init__(self, filename, line, msg, charset, fuzzy, fmt): + def __init__(self, filename, line, msg, charset, fuzzy, fmt, noqa): """Build a PO message.""" self.filename = filename self.line = line @@ -136,6 +136,7 @@ def __init__(self, filename, line, msg, charset, fuzzy, fmt): self.messages.append((msg.get('msgid', ''), msg.get('msgstr', ''))) self.fuzzy = fuzzy self.fmt = fmt + self.noqa = noqa def check_lines(self): """ @@ -297,7 +298,8 @@ def __init__(self, filename): } self.msgs = [] - def _add_message(self, numline_msgid, fuzzy, fmt, msg): + # pylint: disable=too-many-arguments + def _add_message(self, numline_msgid, fuzzy, fmt, noqa, msg): """ Add a message from PO file in list of messages. """ @@ -314,7 +316,7 @@ def _add_message(self, numline_msgid, fuzzy, fmt, msg): if match: self.props['charset'] = match.group(1) self.msgs.append(PoMessage(self.filename, numline_msgid, msg, - self.props['charset'], fuzzy, fmt)) + self.props['charset'], fuzzy, fmt, noqa)) def read(self): """ @@ -323,6 +325,7 @@ def read(self): self.msgs = [] numline, numline_msgid = (0, 0) fuzzy, msgfuzzy = (False, False) + noqa, msgnoqa = (False, False) fmt, msgfmt = (None, None) msg = {} msgcurrent = '' @@ -337,6 +340,7 @@ def read(self): match = re.search(r'([a-z-]+)-format', line, re.IGNORECASE) fmt = match.group(1) if match else None if line.startswith('#'): + noqa = 'noqa' in line continue if line.startswith('msg'): match = re.match( @@ -351,9 +355,12 @@ def read(self): self._add_message(numline_msgid, msgfuzzy, msgfmt, + msgnoqa, msg) msgfuzzy = fuzzy + msgnoqa = noqa fuzzy = False + noqa = False msgfmt = fmt fmt = None msg = {} @@ -364,6 +371,7 @@ def read(self): self._add_message(numline_msgid, msgfuzzy, msgfmt, + msgnoqa, msg) def compile(self): @@ -389,6 +397,7 @@ def __init__(self): self.checks = { 'compile': True, 'fuzzy': False, + 'skip_noqa': False, 'lines': True, 'punct': True, 'whitespace': True, @@ -478,7 +487,10 @@ def check_pofile(self, po_file): # check all messages check_fuzzy = self.checks['fuzzy'] + skip_noqa = self.checks['skip_noqa'] for msg in po_file.msgs: + if skip_noqa and msg.noqa: + continue if msg.fuzzy and not check_fuzzy: continue if self.checks['extract']: diff --git a/tests/fr_errors.po b/tests/fr_errors.po index f2d453d..7d1c81f 100644 --- a/tests/fr_errors.po +++ b/tests/fr_errors.po @@ -48,6 +48,7 @@ msgstr "Test 2 sur deux lignes.\nLigne 2.\nLigne 3." msgid "Tested 1." msgstr "Testé 1" +#, noqa msgid "Tested 2" msgstr "Testé 2." diff --git a/tests/test_msgcheck.py b/tests/test_msgcheck.py index b1ec57a..69564fe 100644 --- a/tests/test_msgcheck.py +++ b/tests/test_msgcheck.py @@ -96,6 +96,17 @@ def test_checks_fuzzy(self): # the file has 11 errors (with the fuzzy string) self.assertEqual(len(result[0][1]), 11) + def test_checks_noqa(self): + """Test checks on a gettext file ignoring `noqa`-commented lines.""" + po_check = PoCheck() + po_check.set_check('skip_noqa', True) + result = po_check.check_files([local_path('fr_errors.po')]) + # be sure we have one file in result + self.assertEqual(len(result), 1) + + # the file has 9 errors (`noqa` was skipped) + self.assertEqual(len(result[0][1]), 9) + def test_replace_formatters_c(self): """Test removal of formatters in a C string.""" self.assertEqual(replace_formatters('%s', 'c'), '')