Skip to content
Snippets Groups Projects
Unverified Commit 2ef22239 authored by Sébastien Helleu's avatar Sébastien Helleu Committed by GitHub
Browse files

Merge pull request #7 from amureki/noqa_flag

Add skip_noqa argument to skip "noqa"-commented lines (fix #2)
parents d699d998 5654e5d1
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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,
Loading
Loading
Loading
Loading
@@ -65,6 +65,8 @@ The script returns:
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',
Loading
Loading
@@ -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_'),
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
@@ -136,6 +136,7 @@ class PoMessage(object):
self.messages.append((msg.get('msgid', ''), msg.get('msgstr', '')))
self.fuzzy = fuzzy
self.fmt = fmt
self.noqa = noqa
 
def check_lines(self):
"""
Loading
Loading
@@ -297,7 +298,8 @@ class PoFile(object):
}
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.
"""
Loading
Loading
@@ -314,7 +316,7 @@ class PoFile(object):
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):
"""
Loading
Loading
@@ -323,6 +325,7 @@ class PoFile(object):
self.msgs = []
numline, numline_msgid = (0, 0)
fuzzy, msgfuzzy = (False, False)
noqa, msgnoqa = (False, False)
fmt, msgfmt = (None, None)
msg = {}
msgcurrent = ''
Loading
Loading
@@ -337,6 +340,7 @@ class PoFile(object):
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(
Loading
Loading
@@ -351,9 +355,12 @@ class PoFile(object):
self._add_message(numline_msgid,
msgfuzzy,
msgfmt,
msgnoqa,
msg)
msgfuzzy = fuzzy
msgnoqa = noqa
fuzzy = False
noqa = False
msgfmt = fmt
fmt = None
msg = {}
Loading
Loading
@@ -364,6 +371,7 @@ class PoFile(object):
self._add_message(numline_msgid,
msgfuzzy,
msgfmt,
msgnoqa,
msg)
 
def compile(self):
Loading
Loading
@@ -389,6 +397,7 @@ class PoCheck(object):
self.checks = {
'compile': True,
'fuzzy': False,
'skip_noqa': False,
'lines': True,
'punct': True,
'whitespace': True,
Loading
Loading
@@ -478,7 +487,10 @@ class PoCheck(object):
 
# 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']:
Loading
Loading
Loading
Loading
@@ -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."
 
Loading
Loading
Loading
Loading
@@ -96,6 +96,17 @@ class TestMsgCheck(unittest.TestCase):
# 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'), '')
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