From 510b0fc8af113f15e577f87d209328a80a547e16 Mon Sep 17 00:00:00 2001 From: Christophe Nanteuil <35002064+christopheNan@users.noreply.github.com> Date: Sat, 1 Feb 2020 16:01:23 +0100 Subject: [PATCH] Add ability to search in translated text. (#3) --- pogrep.py | 12 +++++++++--- test_pogrep.py | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pogrep.py b/pogrep.py index 85e42d7..f1a1542 100644 --- a/pogrep.py +++ b/pogrep.py @@ -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__": diff --git a/test_pogrep.py b/test_pogrep.py index 7559529..fc5a579 100644 --- a/test_pogrep.py +++ b/test_pogrep.py @@ -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: