Skip to content

Commit 5d2af94

Browse files
committed
Use polib to get number of translators
It's roughly 60 times slower, but more readable? Fix number, now we are matching my email if available
1 parent 0558ed4 commit 5d2af94

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ f'''![build](https://github.com/python/python-docs-pl/workflows/.github/workflow
1717
![build](https://github.com/python/python-docs-pl/workflows/.github/workflows/update-and-build.yml/badge.svg)
1818
![48.35% przełącznika języków](https://img.shields.io/badge/przełącznik_języków-48.35%25-0.svg)
1919
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/całość-3.51%25-0.svg)
20-
![19 tłumaczy](https://img.shields.io/badge/tłumaczy-19-0.svg)
20+
![18 tłumaczy](https://img.shields.io/badge/tłumaczy-18-0.svg)
2121
<!-- [[[end]]] -->
2222

2323
Jeśli znalazłeś(-aś) błąd lub masz sugestię,

manage_translation.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
# * regenerate_tx_config: recreate configuration for all resources.
1313

1414
from argparse import ArgumentParser
15-
from collections import Counter
1615
import os
16+
from collections import Counter
1717
from dataclasses import dataclass
18+
from pathlib import Path
1819
from re import match
1920
from subprocess import call, run
2021
import sys
22+
from timeit import timeit
2123
from typing import Self, Callable
2224
from urllib.parse import urlparse, parse_qs
2325

26+
from polib import pofile
27+
2428
LANGUAGE = 'pl'
2529

2630

@@ -168,14 +172,16 @@ def progress_from_resources(resources: list[ResourceLanguageStatistics], filter_
168172

169173

170174
def get_number_of_translators():
171-
process = run(
172-
['grep', '-ohP', r'(?<=^# )(.+)(?=, \d+$)', '-r', '.'],
173-
capture_output=True,
174-
text=True,
175-
)
176-
translators = [match('(.*)( <.*>)?', t).group(1) for t in process.stdout.splitlines()]
177-
unique_translators = Counter(translators).keys()
178-
return len(unique_translators)
175+
translators = set()
176+
for file in Path().rglob('*.po'):
177+
header = pofile(file).header.splitlines()
178+
for translator_record in header[header.index('Translators:') + 1:]:
179+
translator, _ = translator_record.split(', ')
180+
if (email_match := match('.* <(.*)>', translator)):
181+
translators.add(email_match.group(1))
182+
else:
183+
translators.add(translator)
184+
return len(translators)
179185

180186

181187
def language_switcher(entry: ResourceLanguageStatistics) -> bool:
@@ -184,10 +190,13 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
184190

185191

186192
if __name__ == "__main__":
187-
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config')
188-
189-
parser = ArgumentParser()
190-
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
191-
options = parser.parse_args()
192-
193-
eval(options.cmd)()
193+
# RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config')
194+
#
195+
# parser = ArgumentParser()
196+
# parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
197+
# options = parser.parse_args()
198+
#
199+
# eval(options.cmd)()
200+
print(timeit(get_number_of_translators, number=10))
201+
202+
print(timeit(get_number_of_translators_old, number=10))

0 commit comments

Comments
 (0)