Skip to content

Commit 5762f9f

Browse files
authored
Count translators with Python and polib, remove alias and look for new ones (#32)
* Count translators with Python and polib, remove aliases and look for new ones * Convert ValueError to UserWarning * Remove superfluous fsdecode() call
1 parent d13aeb2 commit 5762f9f

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

.github/workflows/update-lint-and-build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
with:
2020
python-version: 3
2121
- run: sudo apt-get install -y gettext
22-
- run: pip install requests cogapp
22+
- run: pip install requests cogapp polib
2323
- uses: actions/checkout@master
2424
with:
2525
ref: ${{ matrix.version }}
@@ -31,7 +31,7 @@ jobs:
3131
- run: ./manage_translation.py fetch
3232
env:
3333
TX_TOKEN: ${{ secrets.TX_TOKEN }}
34-
- run: cog -rP README.md
34+
- run: python -Werror -m cogapp -rP README.md
3535
env:
3636
TX_TOKEN: ${{ secrets.TX_TOKEN }}
3737
- run: git config --local user.email github-actions@github.com

README.md

+1-1
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.63% przełącznika języków](https://img.shields.io/badge/przełącznik_języków-48.63%25-0.svg)
1919
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/całość-3.52%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

+35-10
Original file line numberDiff line numberDiff line change
@@ -12,14 +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
1716
from dataclasses import dataclass
17+
from difflib import SequenceMatcher
18+
from itertools import combinations
19+
from pathlib import Path
1820
from re import match
19-
from subprocess import call, run
21+
from subprocess import call
2022
import sys
2123
from typing import Self, Callable
2224
from urllib.parse import urlparse, parse_qs
25+
from warnings import warn
26+
27+
from polib import pofile
2328

2429
LANGUAGE = 'pl'
2530

@@ -168,14 +173,34 @@ def progress_from_resources(resources: list[ResourceLanguageStatistics], filter_
168173

169174

170175
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)
176+
translators = _fetch_translators()
177+
_remove_aliases(translators)
178+
_check_for_new_aliases(translators)
179+
return len(translators)
180+
181+
182+
def _fetch_translators() -> set[str]:
183+
translators = set()
184+
for file in Path().rglob('*.po'):
185+
header = pofile(file).header.splitlines()
186+
for translator_record in header[header.index('Translators:') + 1:]:
187+
translator, _year = translator_record.split(', ')
188+
translators.add(translator)
189+
return translators
190+
191+
192+
def _remove_aliases(translators: set[str]) -> None:
193+
for alias, main in (("m_aciek <maciej.olko@gmail.com>", "Maciej Olko <maciej.olko@gmail.com>"),):
194+
translators.remove(alias)
195+
assert main in translators
196+
197+
198+
def _check_for_new_aliases(translators) -> None:
199+
for pair in combinations(translators, 2):
200+
if (ratio := SequenceMatcher(lambda x: x in '<>@', *pair).ratio()) > 0.64:
201+
warn(
202+
f"{pair} are similar ({ratio:.3f}). Please add them to aliases list or bump the limit."
203+
)
179204

180205

181206
def language_switcher(entry: ResourceLanguageStatistics) -> bool:

0 commit comments

Comments
 (0)