20
20
from re import match
21
21
from subprocess import call
22
22
import sys
23
- from typing import Self , Callable
23
+ from typing import Self , Generator , Iterable
24
24
from urllib .parse import urlparse , parse_qs
25
25
from warnings import warn
26
26
@@ -124,10 +124,9 @@ def from_api_v3_entry(cls, data: dict) -> Self:
124
124
)
125
125
126
126
127
- def _get_from_api_v3_with_cursor (url : str , params : dict ) -> list [dict ]:
127
+ def _get_from_api_v3_with_cursor (url : str , params : dict ) -> Generator [dict , None , None ]:
128
128
from requests import get
129
129
130
- resources = []
131
130
cursor = None
132
131
if os .path .exists ('.tx/api-key' ):
133
132
with open ('.tx/api-key' ) as f :
@@ -142,51 +141,46 @@ def _get_from_api_v3_with_cursor(url: str, params: dict) -> list[dict]:
142
141
)
143
142
response .raise_for_status ()
144
143
response_json = response .json ()
145
- response_list = response_json ['data' ]
146
- resources .extend (response_list )
144
+ yield from response_json ['data' ]
147
145
if not response_json ['links' ].get ('next' ): # for stats no key, for list resources null
148
146
break
149
147
cursor , * _ = parse_qs (urlparse (response_json ['links' ]['next' ]).query )['page[cursor]' ]
150
- return resources
151
148
152
149
153
- def _get_resources () -> list [Resource ]:
150
+ def _get_resources () -> Generator [Resource , None , None ]:
154
151
resources = _get_from_api_v3_with_cursor (
155
152
'https://rest.api.transifex.com/resources' , {'filter[project]' : f'o:python-doc:p:{ PROJECT_SLUG } ' }
156
153
)
157
- return [ Resource .from_api_v3_entry (entry ) for entry in resources ]
154
+ yield from ( Resource .from_api_v3_entry (entry ) for entry in resources )
158
155
159
156
160
- def get_resource_language_stats () -> list [ResourceLanguageStatistics ]:
157
+ def get_resource_language_stats () -> Generator [ResourceLanguageStatistics , None , None ]:
161
158
resources = _get_from_api_v3_with_cursor (
162
159
'https://rest.api.transifex.com/resource_language_stats' ,
163
160
{'filter[project]' : f'o:python-doc:p:{ PROJECT_SLUG } ' , 'filter[language]' : f'l:{ LANGUAGE } ' }
164
161
)
165
- return [ ResourceLanguageStatistics .from_api_v3_entry (entry ) for entry in resources ]
162
+ yield from ( ResourceLanguageStatistics .from_api_v3_entry (entry ) for entry in resources )
166
163
167
164
168
- def progress_from_resources (resources : list [ResourceLanguageStatistics ], filter_function : Callable ) -> float :
169
- filtered = filter (filter_function , resources )
170
- pairs = ((e .translated_words , e .total_words ) for e in filtered )
165
+ def progress_from_resources (resources : Iterable [ResourceLanguageStatistics ]) -> float :
166
+ pairs = ((e .translated_words , e .total_words ) for e in resources )
171
167
translated_total , total_total = (sum (counts ) for counts in zip (* pairs ))
172
168
return translated_total / total_total * 100
173
169
174
170
175
171
def get_number_of_translators ():
176
- translators = _fetch_translators ()
172
+ translators = set ( _fetch_translators () )
177
173
_remove_aliases (translators )
178
174
_check_for_new_aliases (translators )
179
175
return len (translators )
180
176
181
177
182
- def _fetch_translators () -> set [str ]:
183
- translators = set ()
178
+ def _fetch_translators () -> Generator [str , None , None ]:
184
179
for file in Path ().rglob ('*.po' ):
185
180
header = pofile (file ).header .splitlines ()
186
181
for translator_record in header [header .index ('Translators:' ) + 1 :]:
187
182
translator , _year = translator_record .split (', ' )
188
- translators .add (translator )
189
- return translators
183
+ yield translator
190
184
191
185
192
186
def _remove_aliases (translators : set [str ]) -> None :
0 commit comments