Skip to content

Mejora rendimiento y renderizado en find_in_po #1364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions scripts/find_in_po.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3

import argparse
import functools
from glob import glob
import multiprocessing
import os
from textwrap import fill

Expand All @@ -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():
Expand Down