Skip to content

Commit ca24dde

Browse files
authored
Calculate total progress in repo, refactor regexes to removeprefix & … (#27)
1 parent de4320c commit ca24dde

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Polskie tłumaczenie dokumentacji Pythona
22
========================================
33
![build](https://github.com/python/python-docs-pl/workflows/.github/workflows/update-and-build.yml/badge.svg)
44
![48.35% przełącznika języków](https://img.shields.io/badge/przełącznik_języków-48.35%25-0.svg)
5-
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/dynamic/json.svg?label=całość&query=$.pl&url=http://gce.zhsj.me/python/newest)
5+
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/całość-3.51%25-0.svg)
66
![19 tłumaczy](https://img.shields.io/badge/tłumaczy-19-0.svg)
77

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

manage_translation.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from collections import Counter
1717
import os
1818
from dataclasses import dataclass
19-
from re import match, search
19+
from re import match
2020
from subprocess import call, run
2121
import sys
22-
from typing import Self
23-
from urllib.parse import unquote
22+
from typing import Self, Callable
23+
from urllib.parse import urlparse, parse_qs
2424

2525
LANGUAGE = 'pl'
2626

@@ -112,15 +112,15 @@ class ResourceLanguageStatistics:
112112
@classmethod
113113
def from_api_v3_entry(cls, data: dict) -> Self:
114114
return cls(
115-
name=search('r:([^:]*)', data['id']).group(1),
115+
name=data['id'].removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'),
116116
total_words=data['attributes']['total_words'],
117117
translated_words=data['attributes']['translated_words'],
118118
total_strings=data['attributes']['total_strings'],
119119
translated_strings=data['attributes']['translated_strings'],
120120
)
121121

122122

123-
def _get_from_api_v3_with_cursor(url: str, params: dict):
123+
def _get_from_api_v3_with_cursor(url: str, params: dict) -> list[dict]:
124124
from requests import get
125125

126126
resources = []
@@ -129,7 +129,7 @@ def _get_from_api_v3_with_cursor(url: str, params: dict):
129129
with open('.tx/api-key') as f:
130130
transifex_api_key = f.read()
131131
else:
132-
transifex_api_key = os.getenv('TX_TOKEN')
132+
transifex_api_key = os.getenv('TX_TOKEN', '')
133133
while True:
134134
response = get(
135135
url,
@@ -142,25 +142,32 @@ def _get_from_api_v3_with_cursor(url: str, params: dict):
142142
resources.extend(response_list)
143143
if not response_json['links'].get('next'): # for stats no key, for list resources null
144144
break
145-
cursor = unquote(search('page\[cursor]=([^&]*)', response_json['links']['next']).group(1))
145+
cursor, *_ = parse_qs(urlparse(response_json['links']['next']).query)['page[cursor]']
146146
return resources
147147

148148

149-
def _get_resources():
149+
def _get_resources() -> list[Resource]:
150150
resources = _get_from_api_v3_with_cursor(
151151
'https://rest.api.transifex.com/resources', {'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}'}
152152
)
153153
return [Resource.from_api_v3_entry(entry) for entry in resources]
154154

155155

156-
def _get_resource_language_stats():
156+
def _get_resource_language_stats() -> list[ResourceLanguageStatistics]:
157157
resources = _get_from_api_v3_with_cursor(
158158
'https://rest.api.transifex.com/resource_language_stats',
159159
{'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'}
160160
)
161161
return [ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources]
162162

163163

164+
def _progress_from_resources(resources: list[ResourceLanguageStatistics], filter_function: Callable):
165+
filtered = filter(filter_function, resources)
166+
pairs = ((e.translated_words, e.total_words) for e in filtered)
167+
translated_total, total_total = (sum(counts) for counts in zip(*pairs))
168+
return translated_total / total_total * 100
169+
170+
164171
def _get_number_of_translators():
165172
process = run(
166173
['grep', '-ohP', r'(?<=^# )(.+)(?=, \d+$)', '-r', '.'],
@@ -173,22 +180,13 @@ def _get_number_of_translators():
173180

174181

175182
def recreate_readme():
176-
def language_switcher(entry):
177-
return (
178-
entry.name.startswith('bugs')
179-
or entry.name.startswith('tutorial')
180-
or entry.name.startswith('library--functions')
181-
)
182-
183-
def average(averages, weights):
184-
return sum([a * w for a, w in zip(averages, weights)]) / sum(weights)
183+
def language_switcher(entry: ResourceLanguageStatistics) -> bool:
184+
language_switcher_resources_prefixes = ('bugs', 'tutorial', 'library--functions')
185+
return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes)
185186

186187
resources = _get_resource_language_stats()
187-
filtered = list(filter(language_switcher, resources))
188-
average_list = [e.translated_words / e.total_words for e in filtered]
189-
weights_list = [e.total_words for e in filtered]
190-
191-
language_switcher_status = average(average_list, weights=weights_list) * 100
188+
language_switcher_status = _progress_from_resources(resources, language_switcher)
189+
total_progress_status = _progress_from_resources(resources, lambda _: True)
192190
number_of_translators = _get_number_of_translators()
193191

194192
with open('README.md', 'w') as file:
@@ -198,7 +196,7 @@ def average(averages, weights):
198196
========================================
199197
![build](https://github.com/python/python-docs-pl/workflows/.github/workflows/update-and-build.yml/badge.svg)
200198
![{language_switcher_status:.2f}% przełącznika języków](https://img.shields.io/badge/przełącznik_języków-{language_switcher_status:.2f}%25-0.svg)
201-
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/dynamic/json.svg?label=całość&query=$.{LANGUAGE}&url=http://gce.zhsj.me/python/newest)
199+
![postęp tłumaczenia całości dokumentacji](https://img.shields.io/badge/całość-{total_progress_status:.2f}%25-0.svg)
202200
![{number_of_translators} tłumaczy](https://img.shields.io/badge/tłumaczy-{number_of_translators}-0.svg)
203201
204202
Jeśli znalazłeś(-aś) błąd lub masz sugestię,

0 commit comments

Comments
 (0)