Add ability to search in translated text. (#3)

This commit is contained in:
Christophe Nanteuil 2020-02-01 16:01:23 +01:00 committed by GitHub
parent fd43d33e3a
commit 510b0fc8af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -47,7 +47,7 @@ def colorize(text, pattern, prefixes):
return result
def find_in_po(pattern, path, linenum, file_match, no_messages):
def find_in_po(pattern, path, linenum, file_match, no_messages, in_translation=False, not_in_source=False):
table = []
prefixes = []
term_width = get_terminal_size()[0]
@ -59,7 +59,8 @@ def find_in_po(pattern, path, linenum, file_match, no_messages):
print("{} doesn't seem to be a .po file".format(filename), file=sys.stderr)
continue
for entry in pofile:
if entry.msgstr and regex.search(pattern, entry.msgid):
if entry.msgstr and ((not not_in_source and regex.search(pattern, entry.msgid))
or (in_translation and regex.search(pattern, entry.msgstr))):
if file_match:
print(MAGENTA + filename + NO_COLOR)
break
@ -129,6 +130,10 @@ def parse_args():
help="Read all files under each directory, recursively, following symbolic links only "
"if they are on the command line. Note that if no file operand is given, pogrep searches "
"the working directory.")
parser.add_argument("--translation", action="store_true",
help="search pattern in translated text (result printed on the right column")
parser.add_argument("--no-source", action="store_true",
help="do NOT search pattern in original text (result printed on the left column")
parser.add_argument("--exclude-dir",
help="Skip any command-line directory with a name suffix that matches the pattern. "
"When searching recursively, skip any subdirectory whose base name matches GLOB. "
@ -147,7 +152,8 @@ def main():
if args.ignore_case:
args.pattern = r"(?i)" + args.pattern
files = process_path(args.path, args.recursive, args.exclude_dir)
find_in_po(args.pattern, files, args.line_number, args.files_with_matches, args.no_messages)
find_in_po(args.pattern, files, args.line_number, args.files_with_matches, args.no_messages,
args.translation, args.no_source)
if __name__ == "__main__":

View File

@ -37,7 +37,7 @@ class TestLateral(fake_filesystem_unittest.TestCase):
with open('about.po', 'w') as f:
f.write(POText)
def testLeft(self):
def testInSource(self):
tmp_stdout = StringIO()
pattern = 'About'
with contextlib.redirect_stdout(tmp_stdout):
@ -46,7 +46,7 @@ class TestLateral(fake_filesystem_unittest.TestCase):
output = tmp_stdout.getvalue()
self.assertIn("about.po", output, "Original text should contain " + pattern)
def testNotInLeft(self):
def testNotInSource(self):
tmp_stdout = StringIO()
pattern = 'propos'
with contextlib.redirect_stdout(tmp_stdout):
@ -55,6 +55,24 @@ class TestLateral(fake_filesystem_unittest.TestCase):
output = tmp_stdout.getvalue()
self.assertTrue(len(output) == 0, "Original text should not contain " + pattern)
def testInTranslation(self):
tmp_stdout = StringIO()
pattern = 'propos'
with contextlib.redirect_stdout(tmp_stdout):
find_in_po(pattern, ('about.po',),
linenum=False, file_match=True, no_messages=False, in_translation=True, not_in_source=True)
output = tmp_stdout.getvalue()
self.assertIn("about.po", output, "Translated text should contain " + pattern)
def testNotInTranslation(self):
tmp_stdout = StringIO()
pattern = 'About'
with contextlib.redirect_stdout(tmp_stdout):
find_in_po(pattern, ('about.po',),
linenum=False, file_match=True, no_messages=False, in_translation=True, not_in_source=True)
output = tmp_stdout.getvalue()
self.assertTrue(len(output) == 0, "Translated text should not contain " + pattern)
class TestColorize(TestCase):
def setUp(self) -> None: