Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion msgcheck/msgcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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_'),
Expand Down
18 changes: 15 additions & 3 deletions msgcheck/po.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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):
"""
Expand All @@ -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 = ''
Expand All @@ -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(
Expand All @@ -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 = {}
Expand All @@ -364,6 +371,7 @@ def read(self):
self._add_message(numline_msgid,
msgfuzzy,
msgfmt,
msgnoqa,
msg)

def compile(self):
Expand All @@ -389,6 +397,7 @@ def __init__(self):
self.checks = {
'compile': True,
'fuzzy': False,
'skip_noqa': False,
'lines': True,
'punct': True,
'whitespace': True,
Expand Down Expand Up @@ -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']:
Expand Down
1 change: 1 addition & 0 deletions tests/fr_errors.po
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
11 changes: 11 additions & 0 deletions tests/test_msgcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'), '')
Expand Down