-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmanage_translation.py
More file actions
executable file
·104 lines (82 loc) · 4.32 KB
/
Copy pathmanage_translation.py
File metadata and controls
executable file
·104 lines (82 loc) · 4.32 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
#
# This python file contains utility scripts to manage Python docs Ukrainian translation.
# It has to be run inside the python-docs-uk git root directory.
import os
import re
from argparse import ArgumentParser
from pathlib import Path
from transifex.api import transifex_api
transifex_api.setup(auth=os.getenv('TX_TOKEN'))
RESOURCE_NAME_MAP = {'glossary_': 'glossary'}
LANG = 'uk'
ORGANISATION_ID = 'o:python-doc'
PROJECT_ID = 'o:python-doc:p:python-newest'
LANGUAGE_ID = f'l:{LANG}'
ORGANISATION = transifex_api.Organization.get(id=ORGANISATION_ID)
PROJECT = transifex_api.Project.get(id=PROJECT_ID)
LANGUAGE = transifex_api.Language.get(id=LANGUAGE_ID)
def _slug_to_file_path(slug: str) -> Path:
"""Set of rules how to transform slug to translation file path"""
file_path = RESOURCE_NAME_MAP.get(slug, slug) # Legacy slug to file mapping
file_path = file_path.replace('--', '/')
if re.match(r'\d+_\d+', file_path):
file_path = file_path.replace('_', '.')
file_path = file_path + '.po'
return Path(file_path)
def recreate_config() -> None:
"""Regenerate Transifex client config for all resources."""
resources = transifex_api.Resource.filter(project=PROJECT).all()
with open('.tx/config', 'w') as fo:
fo.writelines(('[main]\n', 'host = https://api.transifex.com\n',))
for resource in resources:
path = _slug_to_file_path(resource.slug)
fo.writelines((
'\n',
f'[{resource.id}]\n',
f'file_filter = {path}\n',
'type = PO\n',
'source_lang = en\n',
'minimum_perc = 0\n',
f'trans.{LANG} = {path}\n',
f'source_file = {path}\n',
f'resource_name = {resource.name}\n'
))
def recreate_resource_stats() -> None:
"""Create resource stats."""
stats = transifex_api.ResourceLanguageStats.filter(project=PROJECT, language=LANGUAGE).all()
with open('RESOURCE.md', 'w') as fo:
fo.writelines(('| Файл | Перекладено | Переглянуто | Вичитано |\n', '|:-----|:-----|:-----|:-----|\n'))
for stat in stats:
file_name = _slug_to_file_path(stat.id.split(':')[5])
translated_pct = round(100 * stat.attributes['translated_words'] / stat.attributes['total_words'], 1)
reviewed_pct = round(100 * stat.attributes['reviewed_words'] / stat.attributes['total_words'], 1)
proofread_pct = round(100 * stat.attributes['proofread_words'] / stat.attributes['total_words'], 1)
fo.writelines(f'| {file_name} | {translated_pct} % | {reviewed_pct} % | {proofread_pct} % |\n')
def recreate_team_stats() -> None:
"""Create contributor stats"""
members = transifex_api.TeamMembership.filter(organization=ORGANISATION, language=LANGUAGE).all()
users = {member.user.id: member.attributes['role'] for member in members}
translators = dict.fromkeys(users.keys(), 0)
reviewers = dict.fromkeys(users.keys(), 0)
proofreaders = dict.fromkeys(users.keys(), 0)
resources = transifex_api.Resource.filter(project=PROJECT).all()
for resource in resources:
translations = transifex_api.ResourceTranslation.filter(resource=resource, language=LANGUAGE).all()
for translation in translations:
if translation.relationships['translator']:
translators[translation.relationships['translator']['data']['id']] += 1
if translation.relationships['reviewer']:
reviewers[translation.relationships['reviewer']['data']['id']] += 1
if translation.relationships['proofreader']:
proofreaders[translation.relationships['proofreader']['data']['id']] += 1
with open('TEAM.md', 'w') as fo:
fo.writelines(('| | Роль | Переклав | Переглянув | Вичитав |\n', '|:---|:---|:---|:---|:---|\n',))
for user, role in users.items():
fo.writelines(f"| {user} | {role} | {translators[user]} | {reviewers[user]} | {proofreaders[user]} |\n")
if __name__ == "__main__":
RUNNABLE_SCRIPTS = ('recreate_config', 'recreate_resource_stats', 'recreate_team_stats')
parser = ArgumentParser()
parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS)
options = parser.parse_args()
eval(options.cmd[0])()