Skip to content

Commit 36aa7f8

Browse files
committed
Use Transifex SDK for utilities
1 parent 8f5c067 commit 36aa7f8

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

manage_translation.py

+17-42
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import sys
2323
from textwrap import dedent
2424
from typing import Self, Generator, Iterable
25-
from urllib.parse import urlparse, parse_qs
2625
from warnings import warn
2726

2827
from polib import pofile
28+
from transifex.api import transifex_api
2929

3030
LANGUAGE = 'pl'
3131

@@ -81,15 +81,6 @@ def recreate_tx_config():
8181
)
8282

8383

84-
@dataclass
85-
class Resource:
86-
slug: str
87-
88-
@classmethod
89-
def from_api_v3_entry(cls, data: dict) -> Self:
90-
return cls(slug=data['attributes']['slug'])
91-
92-
9384
@dataclass
9485
class ResourceLanguageStatistics:
9586
name: str
@@ -99,52 +90,36 @@ class ResourceLanguageStatistics:
9990
translated_strings: int
10091

10192
@classmethod
102-
def from_api_v3_entry(cls, data: dict) -> Self:
93+
def from_api_entry(cls, data: transifex_api.ResourceLanguageStats) -> Self:
10394
return cls(
104-
name=data['id'].removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'),
105-
total_words=data['attributes']['total_words'],
106-
translated_words=data['attributes']['translated_words'],
107-
total_strings=data['attributes']['total_strings'],
108-
translated_strings=data['attributes']['translated_strings'],
95+
name=data.id.removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'),
96+
total_words=data.attributes['total_words'],
97+
translated_words=data.attributes['translated_words'],
98+
total_strings=data.attributes['total_strings'],
99+
translated_strings=data.attributes['translated_strings'],
109100
)
110101

111102

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

134111

135-
def _get_resources() -> Generator[Resource, None, None]:
136-
resources = _get_from_api_v3_with_cursor(
137-
'https://rest.api.transifex.com/resources', {'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}'}
138-
)
139-
yield from (Resource.from_api_v3_entry(entry) for entry in resources)
112+
def _get_resources() -> Generator[transifex_api.Resource, None, None]:
113+
transifex_api.setup(auth=_get_tx_token())
114+
yield from transifex_api.Resource.filter(project=f'o:python-doc:p:{PROJECT_SLUG}').all()
140115

141116

142117
def get_resource_language_stats() -> Generator[ResourceLanguageStatistics, None, None]:
143-
resources = _get_from_api_v3_with_cursor(
144-
'https://rest.api.transifex.com/resource_language_stats',
145-
{'filter[project]': f'o:python-doc:p:{PROJECT_SLUG}', 'filter[language]': f'l:{LANGUAGE}'}
146-
)
147-
yield from (ResourceLanguageStatistics.from_api_v3_entry(entry) for entry in resources)
118+
transifex_api.setup(auth=_get_tx_token())
119+
resources = transifex_api.ResourceLanguageStats.filter(
120+
project=f'o:python-doc:p:{PROJECT_SLUG}', language=f'l:{LANGUAGE}'
121+
).all()
122+
yield from (ResourceLanguageStatistics.from_api_entry(entry) for entry in resources)
148123

149124

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

0 commit comments

Comments
 (0)