diff --git a/scripts/find_in_po.py b/scripts/find_in_po.py index 523ebda427..025f4e6d8a 100755 --- a/scripts/find_in_po.py +++ b/scripts/find_in_po.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 import argparse +import functools from glob import glob +import multiprocessing import os from textwrap import fill @@ -10,26 +12,46 @@ from tabulate import tabulate # fades +REVERSE = '\033[7m' +NORMAL = '\033[m' + +def _get_file_entries(pattern, width, filename): + entries = [] + for entry in (entry for entry in polib.pofile(filename) if entry.msgstr): + match = pattern.search(entry.msgid) + if match: + add_str = entry.msgid + " ·filename: " + filename + "·" + entries.append( + [ + fill(add_str, width=width), + fill(entry.msgstr, width=width), + ] + ) + return entries + def find_in_po(pattern): - table = [] + pattern = regex.compile(pattern) try: _, columns = os.popen("stty size", "r").read().split() available_width = int(columns) // 2 - 3 except: available_width = 80 // 2 - 3 - for file in glob("**/*.po"): - pofile = polib.pofile(file) - for entry in pofile: - if entry.msgstr and regex.search(pattern, entry.msgid): - add_str = entry.msgid + " ·filename: " + file + "·" - table.append( - [ - fill(add_str, width=available_width), - fill(entry.msgstr, width=available_width), - ] - ) - print(tabulate(table, tablefmt="fancy_grid")) + # Find entries in parallel + get_file_entries = functools.partial(_get_file_entries, pattern, available_width) + pool = multiprocessing.Pool() + all_entries = pool.map(get_file_entries, glob("**/*.po")) + table = [entry for file_entries in all_entries for entry in file_entries] + + # Create table and highlight results + table = tabulate(table, tablefmt="fancy_grid") + for line in table.splitlines(): + match = pattern.search(line) + if match: + span = match.span() + line = (line[:span[0]] + REVERSE + line[span[0]:span[1]] + NORMAL + + line[span[1]:]) + print(line) def parse_args():