|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
3 | 3 | import argparse
|
| 4 | +import functools |
4 | 5 | from glob import glob
|
| 6 | +import multiprocessing |
5 | 7 | import os
|
6 | 8 | from textwrap import fill
|
7 | 9 |
|
|
10 | 12 | from tabulate import tabulate # fades
|
11 | 13 |
|
12 | 14 |
|
| 15 | +REVERSE = '\033[7m' |
| 16 | +NORMAL = '\033[m' |
| 17 | + |
| 18 | +def _get_file_entries(pattern, width, filename): |
| 19 | + entries = [] |
| 20 | + for entry in (entry for entry in polib.pofile(filename) if entry.msgstr): |
| 21 | + match = pattern.search(entry.msgid) |
| 22 | + if match: |
| 23 | + add_str = entry.msgid + " ·filename: " + filename + "·" |
| 24 | + entries.append( |
| 25 | + [ |
| 26 | + fill(add_str, width=width), |
| 27 | + fill(entry.msgstr, width=width), |
| 28 | + ] |
| 29 | + ) |
| 30 | + return entries |
| 31 | + |
13 | 32 | def find_in_po(pattern):
|
14 |
| - table = [] |
| 33 | + pattern = regex.compile(pattern) |
15 | 34 | try:
|
16 | 35 | _, columns = os.popen("stty size", "r").read().split()
|
17 | 36 | available_width = int(columns) // 2 - 3
|
18 | 37 | except:
|
19 | 38 | available_width = 80 // 2 - 3
|
20 | 39 |
|
21 |
| - for file in glob("**/*.po"): |
22 |
| - pofile = polib.pofile(file) |
23 |
| - for entry in pofile: |
24 |
| - if entry.msgstr and regex.search(pattern, entry.msgid): |
25 |
| - add_str = entry.msgid + " ·filename: " + file + "·" |
26 |
| - table.append( |
27 |
| - [ |
28 |
| - fill(add_str, width=available_width), |
29 |
| - fill(entry.msgstr, width=available_width), |
30 |
| - ] |
31 |
| - ) |
32 |
| - print(tabulate(table, tablefmt="fancy_grid")) |
| 40 | + # Find entries in parallel |
| 41 | + get_file_entries = functools.partial(_get_file_entries, pattern, available_width) |
| 42 | + pool = multiprocessing.Pool() |
| 43 | + all_entries = pool.map(get_file_entries, glob("**/*.po")) |
| 44 | + table = [entry for file_entries in all_entries for entry in file_entries] |
| 45 | + |
| 46 | + # Create table and highlight results |
| 47 | + table = tabulate(table, tablefmt="fancy_grid") |
| 48 | + for line in table.splitlines(): |
| 49 | + match = pattern.search(line) |
| 50 | + if match: |
| 51 | + span = match.span() |
| 52 | + line = (line[:span[0]] + REVERSE + line[span[0]:span[1]] + NORMAL + |
| 53 | + line[span[1]:]) |
| 54 | + print(line) |
33 | 55 |
|
34 | 56 |
|
35 | 57 | def parse_args():
|
|
0 commit comments