Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17f7d64

Browse files
authored
Mejora rendimiento y renderizado en find_in_po (#1364)
Este set de cambios introduce dos mejoras en el script find_in_po. En primer lugar usa multiprocessing para realizar la búsqueda de expresiones regulares en paralelo (buscando en distintos archivos al mismo tiempo). En segundo lugar, después de generar la tabla con los resultados, los resultados de la expresión regular son destacados al momento de ser impresos en el terminal para encontrarlos más fácilmente. Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent bc2f1d2 commit 17f7d64

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

scripts/find_in_po.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import functools
45
from glob import glob
6+
import multiprocessing
57
import os
68
from textwrap import fill
79

@@ -10,26 +12,46 @@
1012
from tabulate import tabulate # fades
1113

1214

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+
1332
def find_in_po(pattern):
14-
table = []
33+
pattern = regex.compile(pattern)
1534
try:
1635
_, columns = os.popen("stty size", "r").read().split()
1736
available_width = int(columns) // 2 - 3
1837
except:
1938
available_width = 80 // 2 - 3
2039

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)
3355

3456

3557
def parse_args():

0 commit comments

Comments
 (0)