-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathfind_in_po.py
More file actions
executable file
·66 lines (52 loc) · 1.78 KB
/
find_in_po.py
File metadata and controls
executable file
·66 lines (52 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import argparse
import functools
from glob import glob
import multiprocessing
from shutil import get_terminal_size
from textwrap import fill
import regex # fades
import polib # fades
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):
pattern = regex.compile(pattern)
columns = get_terminal_size().columns
available_width = columns // 2 - 3
# 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():
parser = argparse.ArgumentParser(description="Find translated words.")
parser.add_argument("pattern")
return parser.parse_args()
def main():
args = parse_args()
find_in_po(args.pattern)
if __name__ == "__main__":
main()