22
22
import sys
23
23
from textwrap import dedent
24
24
from typing import Self , Generator , Iterable
25
- from urllib .parse import urlparse , parse_qs
26
25
from warnings import warn
27
26
28
27
from polib import pofile
28
+ from transifex .api import transifex_api
29
29
30
30
LANGUAGE = 'pl'
31
31
@@ -81,15 +81,6 @@ def recreate_tx_config():
81
81
)
82
82
83
83
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
-
93
84
@dataclass
94
85
class ResourceLanguageStatistics :
95
86
name : str
@@ -99,52 +90,36 @@ class ResourceLanguageStatistics:
99
90
translated_strings : int
100
91
101
92
@classmethod
102
- def from_api_v3_entry (cls , data : dict ) -> Self :
93
+ def from_api_entry (cls , data : transifex_api . ResourceLanguageStats ) -> Self :
103
94
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' ],
109
100
)
110
101
111
102
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 :
116
104
if os .path .exists ('.tx/api-key' ):
117
105
with open ('.tx/api-key' ) as f :
118
106
transifex_api_key = f .read ()
119
107
else :
120
108
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
133
110
134
111
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 ()
140
115
141
116
142
117
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 )
148
123
149
124
150
125
def progress_from_resources (resources : Iterable [ResourceLanguageStatistics ]) -> float :
0 commit comments