Skip to content

Commit 3f9bb9a

Browse files
committed
Use Transifex SDK for utilities
1 parent 5f0a2d7 commit 3f9bb9a

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

manage_translation.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
from subprocess import call
2222
import sys
2323
from typing import Self, Generator, Iterable
24-
from urllib.parse import urlparse, parse_qs
2524
from warnings import warn
2625

2726
from polib import pofile
27+
from transifex.api import transifex_api
2828

2929
LANGUAGE = 'pl'
3030

@@ -84,15 +84,6 @@ def recreate_tx_config():
8484
)
8585

8686

87-
@dataclass
88-
class Resource:
89-
slug: str
90-
91-
@classmethod
92-
def from_api_v3_entry(cls, data: dict) -> Self:
93-
return cls(slug=data['attributes']['slug'])
94-
95-
9687
@dataclass
9788
class ResourceLanguageStatistics:
9889
name: str
@@ -102,52 +93,36 @@ class ResourceLanguageStatistics:
10293
translated_strings: int
10394

10495
@classmethod
105-
def from_api_v3_entry(cls, data: dict) -> Self:
96+
def from_api_entry(cls, data: transifex_api.ResourceLanguageStats) -> Self:
10697
return cls(
107-
name=data['id'].removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'),
108-
total_words=data['attributes']['total_words'],
109-
translated_words=data['attributes']['translated_words'],
110-
total_strings=data['attributes']['total_strings'],
111-
translated_strings=data['attributes']['translated_strings'],
98+
name=data.id.removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'),
99+
total_words=data.attributes['total_words'],
100+
translated_words=data.attributes['translated_words'],
101+
total_strings=data.attributes['total_strings'],
102+
translated_strings=data.attributes['translated_strings'],
112103
)
113104

114105

115-
def _get_from_api_v3_with_cursor(url: str, params: dict) -> Generator[dict, None, None]:
116-
from requests import get
117-
118-
cursor = None
106+
def _get_tx_token() -> str:
119107
if os.path.exists('.tx/api-key'):
120108
with open('.tx/api-key') as f:
121109
transifex_api_key = f.read()
122110
else:
123111
transifex_api_key = os.getenv('TX_TOKEN', '')
124-
while True:
125-
response = get(
126-
url,
127-
params=params | ({'page[cursor]': cursor} if cursor else {}),
128-
headers={'Authorization': f'Bearer {transifex_api_key}'}
129-
)
130-
response.raise_for_status()
131-
response_json = response.json()
132-
yield from response_json['data']
133-
if not response_json['links'].get('next'): # for stats no key, for list resources null
134-
break
135-
cursor, *_ = parse_qs(urlparse(response_json['links']['next']).query)['page[cursor]']
112+
return transifex_api_key
136113

137114

138-
def _get_resources() -> Generator[Resource, None, None]:
139-
resources = _get_from_api_v3_with_cursor(
140-
'https://rest.api.transifex.com/resources', {'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}'}
141-
)
142-
yield from (Resource.from_api_v3_entry(entry) for entry in resources)
115+
def _get_resources() -> Generator[transifex_api.Resource, None, None]:
116+
transifex_api.setup(auth=_get_tx_token())
117+
yield from transifex_api.Resource.filter(project=f'o:python-doc:p:{PROJECT_SLUG}').all()
143118

144119

145120
def get_resource_language_stats() -> Generator[ResourceLanguageStatistics, None, None]:
146-
resources = _get_from_api_v3_with_cursor(
147-
'https://rest.api.transifex.com/resource_language_stats',
148-
{'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'}
149-
)
150-
yield from (ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources)
121+
transifex_api.setup(auth=_get_tx_token())
122+
resources = transifex_api.ResourceLanguageStats.filter(
123+
project=f'o:python-doc:p:{PROJECT_SLUG}', language=f'l:{LANGUAGE}'
124+
).all()
125+
yield from (ResourceLanguageStatistics.from_api_entry(entry) for entry in resources)
151126

152127

153128
def progress_from_resources(resources: Iterable[ResourceLanguageStatistics]) -> float:

0 commit comments

Comments
 (0)