Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • flashcode/msgcheck
1 result
Show changes
Commits on Source (6)
language: python
sudo: false
 
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
 
before_install:
- sudo apt-get update -qq
- sudo apt-get install -y gettext aspell aspell-fr libenchant-dev
cache:
- pip
- apt
addons:
apt:
packages:
- gettext
- aspell
- aspell-en
- aspell-fr
- libenchant-dev
 
install:
- "pip install pylint"
- "pip install -r requirements.txt"
- pip install -U pylint
- pip install -Ur requirements.txt
 
script:
- pylint --disable=W0511 msgcheck
Loading
Loading
Loading
Loading
@@ -265,7 +265,7 @@ class PoMessage(object):
continue
text = mstr if spelling == 'str' else mid
if self.fmt:
text = replace_formatters(text, ' ', self.fmt)
text = replace_formatters(text, self.fmt)
checkers[0].set_text(text)
misspelled = []
for err in checkers[0]:
Loading
Loading
Loading
Loading
@@ -24,11 +24,24 @@ Some utility functions for msgcheck.
 
from __future__ import print_function
 
from collections import defaultdict
import re
 
# TODO: add support for other languages
STR_FORMATTERS = {
'c': ('\\', '%', '#- +\'I.0123456789hlLqjzt', 'diouxXeEfFgGaAcsCSpnm'),
}
STR_FORMATTERS = defaultdict(list)
STR_FORMATTERS.update({
'c': (
(r'[\%]{2}', '%'),
(r'\%([ hlL\d\.\-\+\#\*]+)?[cdieEfgGosuxXpn]', r''),
),
'python': (
(r'[\%]{2}', '%'),
(r'\%([.\d]+)?[bcdeEfFgGnosxX]', r''),
(r'\%(\(([^)]*)\))([.\d]+)?[bcdeEfFgGnosxX]', r'\g<2>'),
(r'\{([^\:\}]*)?(:[^\}]*)?\}', r''),
),
})
 
 
def count_lines(string):
Loading
Loading
@@ -39,47 +52,10 @@ def count_lines(string):
return count
 
 
# pylint: disable=too-many-branches
def replace_formatters(string, replace, fmt):
"""
def replace_formatters(string, fmt):
r"""
Replace formatters (like "%s" or "%03d") with a replacement string.
"""
if fmt not in STR_FORMATTERS:
return string
formatters = STR_FORMATTERS[fmt]
formatter, escape = (False, False)
strformat = []
result = []
for char in string:
if formatter:
if char == formatters[1]:
result.append(char)
formatter = False
elif char in formatters[2]:
strformat.append(char)
elif char in formatters[3]:
result.append(replace)
formatter = False
else:
strformat.append(char)
result += strformat
formatter = False
elif escape:
result.append(formatters[0])
result.append(char)
escape = False
elif char == formatters[0]:
escape = True
elif char == formatters[1]:
formatter = True
strformat = [char]
else:
result.append(char)
if escape: # unterminated escaped char?
result.append(formatters[0])
elif formatter: # unterminated formatter?
result.append(replace)
return ''.join(result)
for pattern, repl in STR_FORMATTERS[fmt]:
string = re.sub(pattern, repl, string)
return string
Loading
Loading
@@ -96,21 +96,57 @@ class TestMsgCheck(unittest.TestCase):
# the file has 11 errors (with the fuzzy string)
self.assertEqual(len(result[0][1]), 11)
 
def test_replace_formatters(self):
"""Test removal of formatters in a string."""
self.assertEqual(replace_formatters('%', '', 'c'), '')
self.assertEqual(replace_formatters('\\', '', 'c'), '\\')
self.assertEqual(replace_formatters('%s', ' ', 'c'), ' ')
self.assertEqual(replace_formatters('%.02f', ' ', 'c'), ' ')
self.assertEqual(replace_formatters('%!%s%!', '', 'c'), '%!%!')
self.assertEqual(replace_formatters('%.02!', ' ', 'c'), '%.02!')
def test_replace_formatters_c(self):
"""Test removal of formatters in a C string."""
self.assertEqual(replace_formatters('%s', 'c'), '')
self.assertEqual(replace_formatters('%%', 'c'), '%')
self.assertEqual(replace_formatters('%.02f', 'c'), '')
self.assertEqual(replace_formatters('%!%s%!', 'c'), '%!%!')
self.assertEqual(replace_formatters('%.02!', 'c'), '%.02!')
self.assertEqual(
replace_formatters('%.3fThis is a %stest', ' ', 'c'),
' This is a test')
replace_formatters('%.3fThis is a %stest', 'c'),
'This is a test')
self.assertEqual(
replace_formatters('%.3fTest%s%d%%%.03f%luhere% s', '', 'c'),
replace_formatters('%.3fTest%s%d%%%.03f%luhere% s', 'c'),
'Test%here')
 
def test_replace_formatters_python(self):
"""Test removal of formatters in a python string."""
# str.__mod__()
self.assertEqual(replace_formatters('%s', 'python'), '')
self.assertEqual(replace_formatters('%b', 'python'), '')
self.assertEqual(replace_formatters('%%', 'python'), '%')
self.assertEqual(replace_formatters('%.02f', 'python'), '')
self.assertEqual(replace_formatters('%(sth)s', 'python'), 'sth')
self.assertEqual(replace_formatters('%(sth)02f', 'python'), 'sth')
# str.format()
conditions = (
('First, thou shalt count to {0}',
'First, thou shalt count to ',
'References first positional argument'),
('Bring me a {}',
'Bring me a ',
'Implicitly references the first positional argument'),
('From {} to {}',
'From to ',
'Same as "From {0} to {1}"'),
('My quest is {name}',
'My quest is ',
'References keyword argument \'name\''),
('Weight in tons {0.weight}',
'Weight in tons ',
'\'weight\' attribute of first positional arg'),
('Units destroyed: {players[0]}',
'Units destroyed: ',
'First element of keyword argument \'players\'.'),
)
for condition in conditions:
self.assertEqual(
replace_formatters(condition[0], 'python'),
condition[1],
condition[2],
)
def test_spelling_id(self):
"""Test spelling on source messages (English) of gettext files."""
po_check = PoCheck()
Loading
Loading