|
3 | 3 |
|
4 | 4 | importjson
|
5 | 5 | importos
|
| 6 | +importconfigparser |
6 | 7 | importurllib.request
|
7 | 8 | fromdatetimeimportdatetime,timezone
|
8 | 9 |
|
9 |
| -key=os.environ.get('TX_TOKEN') |
10 |
| -language=os.environ.get('PYDOC_LANGUAGE') |
11 |
| -project=os.environ.get('PYDOC_TX_PROJECT') |
12 |
| - |
13 |
| -url="https://rest.api.transifex.com/resource_language_stats?filter[project]=o%3Apython-doc%3Ap%3A{tx_project}&filter[language]=l%3A{langcode}".format(tx_project=project,langcode=language) |
14 |
| - |
15 |
| -headers= { |
16 |
| -"accept":"application/vnd.api+json", |
17 |
| -"authorization":"Bearer "+key |
18 |
| -} |
19 |
| - |
20 |
| -total=0 |
21 |
| -translated=0 |
22 |
| - |
23 |
| -while(url): |
24 |
| -request=urllib.request.Request(url=url,headers=headers) |
25 |
| -withurllib.request.urlopen(request)asresponse: |
26 |
| -data=json.loads(response.read().decode("utf-8")) |
27 |
| -url=data['links'].get('next') |
28 |
| -forresourseindata['data']: |
29 |
| -translated=translated+resourse['attributes']['translated_strings'] |
30 |
| -total=total+resourse['attributes']['total_strings'] |
31 |
| - |
32 |
| -p='{:.2%}'.format(translated/total) |
33 |
| -print(json.dumps({ |
34 |
| -'translation':p, |
35 |
| -'total':total, |
36 |
| -'updated_at':datetime.now(timezone.utc).isoformat(timespec='seconds')+'Z', |
37 |
| - })) |
| 10 | +# Get language and project from environment variables |
| 11 | +language=os.environ.get("PYDOC_LANGUAGE") |
| 12 | +project=os.environ.get("PYDOC_TX_PROJECT") |
| 13 | +iflanguageisNone: |
| 14 | +raiseValueError("The PYDOC_LANGUAGE environment variable must be set.") |
| 15 | +ifprojectisNone: |
| 16 | +raiseValueError("The PYDOC_TX_PROJECT environment variable must be set.") |
| 17 | + |
| 18 | + |
| 19 | +# Try to read API token from TX_TOKEN env and then from ~/.transifexrc |
| 20 | +defget_transifex_token(): |
| 21 | +key=os.environ.get("TX_TOKEN") |
| 22 | +ifkeyisNone: |
| 23 | +config=configparser.ConfigParser() |
| 24 | +config.read(os.path.expanduser("~/.transifexrc")) |
| 25 | +try: |
| 26 | +key=config["https://www.transifex.com"]["token"] |
| 27 | +exceptKeyError: |
| 28 | +raiseValueError("Unable to retrieve Transifex API token.") |
| 29 | +returnkey |
| 30 | + |
| 31 | + |
| 32 | +# API URL setup |
| 33 | +url_template= ( |
| 34 | +"https://rest.api.transifex.com/resource_language_stats" |
| 35 | +"?filter[project]=o%3Apython-doc%3Ap%3A{project}" |
| 36 | +"&filter[language]=l%3A{language}" |
| 37 | +) |
| 38 | + |
| 39 | +# Get the authorization key |
| 40 | +key=get_transifex_token() |
| 41 | + |
| 42 | +url=url_template.format(project=project,language=language) |
| 43 | + |
| 44 | +headers= {"accept":"application/vnd.api+json","authorization":f"Bearer{key}"} |
| 45 | + |
| 46 | +# Initialize counters |
| 47 | +total_strings=0 |
| 48 | +translated_strings=0 |
| 49 | + |
| 50 | + |
| 51 | +# Function to make an API request and handle potential errors |
| 52 | +deffetch_data(url): |
| 53 | +request=urllib.request.Request(url=url,headers=headers) |
| 54 | +try: |
| 55 | +withurllib.request.urlopen(request)asresponse: |
| 56 | +returnjson.loads(response.read().decode("utf-8")) |
| 57 | +excepturllib.error.URLErrorase: |
| 58 | +raiseConnectionError(f"Error fetching data:{e}") |
| 59 | +exceptjson.JSONDecodeErrorase: |
| 60 | +raiseValueError(f"Error decoding JSON response:{e}") |
| 61 | + |
| 62 | + |
| 63 | +# Fetch and process translation stats |
| 64 | +whileurl: |
| 65 | +data=fetch_data(url) |
| 66 | +url=data["links"].get("next") |
| 67 | +forresourceindata["data"]: |
| 68 | +translated_strings+=resource["attributes"]["translated_strings"] |
| 69 | +total_strings+=resource["attributes"]["total_strings"] |
| 70 | + |
| 71 | +# Calculate translation percentage |
| 72 | +iftotal_strings==0: |
| 73 | +raiseValueError("Total strings cannot be zero.") |
| 74 | + |
| 75 | +percentage=f"{(translated_strings/total_strings):.2%}" |
| 76 | + |
| 77 | +# Print the result as JSON |
| 78 | +print( |
| 79 | +json.dumps( |
| 80 | + { |
| 81 | +"translation":percentage, |
| 82 | +"total":total_strings, |
| 83 | +"updated_at":datetime.now(timezone.utc).isoformat(timespec="seconds") |
| 84 | ++"Z", |
| 85 | + } |
| 86 | + ) |
| 87 | +) |