1616from collections import Counter
1717import os
1818from dataclasses import dataclass
19- from re import match , search
19+ from re import match
2020from subprocess import call , run
2121import 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
2525LANGUAGE = '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+
164171def _get_number_of_translators ():
165172 process = run (
166173 ['grep' , '-ohP' , r'(?<=^# )(.+)(?=, \d+$)' , '-r' , '.' ],
@@ -173,22 +180,13 @@ def _get_number_of_translators():
173180
174181
175182def 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
200198
201- 
199+ 
202200
203201
204202Jeśli znalazłeś(-aś) błąd lub masz sugestię,
0 commit comments