2019-08-07 12:18:26 +02:00
|
|
|
#! /usr/bin/env python3
|
2018-12-13 14:50:33 +01:00
|
|
|
#
|
|
|
|
# Uses the Transifex API to get a list of enabled languages,
|
|
|
|
# and outputs CMake settings for inclusion into CMakeLists.txt.
|
2020-01-27 16:35:56 +01:00
|
|
|
#
|
|
|
|
# This is a Python3 script.
|
|
|
|
#
|
|
|
|
# Run it with a -v command-line option to get extra output on
|
|
|
|
# actual translation percentages.
|
2018-12-13 14:50:33 +01:00
|
|
|
import sys
|
2020-07-31 10:46:54 +02:00
|
|
|
import argparse
|
2018-12-13 14:50:33 +01:00
|
|
|
|
2020-07-31 12:07:01 +02:00
|
|
|
class TXError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TransifexGetter(object):
|
2018-12-13 14:50:33 +01:00
|
|
|
"""
|
2020-07-31 12:07:01 +02:00
|
|
|
Get language data from Transifex.
|
|
|
|
|
|
|
|
The object does all the work in __init__, after that
|
|
|
|
the only relevant data is .languages, a dictionary
|
|
|
|
of language data.
|
2018-12-13 14:50:33 +01:00
|
|
|
"""
|
2020-07-31 12:07:01 +02:00
|
|
|
def __init__(self):
|
|
|
|
token = self.get_tx_credentials()
|
|
|
|
if token is None:
|
|
|
|
raise TXError("Could not get Transifex API token")
|
|
|
|
|
|
|
|
import requests
|
|
|
|
r = requests.get("https://api.transifex.com/organizations/calamares/projects/calamares/resources/calamares/", auth=("api", token))
|
|
|
|
if r.status_code != 200:
|
|
|
|
raise TXError("Could not get Transifex data from API")
|
|
|
|
|
|
|
|
j = r.json()
|
|
|
|
self.languages = j["stats"]
|
|
|
|
|
|
|
|
|
|
|
|
def get_tx_credentials(self):
|
|
|
|
"""
|
|
|
|
Gets the API token out of the user's .transifexrc (this is supposed
|
|
|
|
to be secure).
|
|
|
|
"""
|
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
txconfig_name = os.path.expanduser("~/.transifexrc")
|
|
|
|
try:
|
|
|
|
with open(txconfig_name, "r") as f:
|
|
|
|
parser = configparser.ConfigParser()
|
|
|
|
parser.read_file(f)
|
|
|
|
|
|
|
|
return parser.get("https://www.transifex.com", "password")
|
|
|
|
except IOError as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-07-31 12:17:07 +02:00
|
|
|
class BogusGetter(object):
|
|
|
|
"""
|
|
|
|
Fake language data.
|
|
|
|
|
|
|
|
This object pretends to retrieve data, and returns fixed language lists and percentages,
|
|
|
|
for testing purposes without hitting Transifex servers all the time.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.languages = dict()
|
|
|
|
for lang, completion in ( ("sq", 100), ("ar", 44), ("as", 28), ("de", 15), ("da", 4), ("ts", 82) ):
|
|
|
|
self.languages[lang] = dict(translated=dict(stringcount=686, percentage=(completion/100.0)))
|
2018-12-13 14:50:33 +01:00
|
|
|
|
|
|
|
|
2020-07-31 12:22:40 +02:00
|
|
|
class PrintOutputter(object):
|
|
|
|
"""
|
|
|
|
Output via print-statements.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def print(self, s):
|
|
|
|
print(s)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, e, v, tb):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def output_langs(all_langs, outputter, label, filterfunc):
|
2018-12-13 14:50:33 +01:00
|
|
|
"""
|
|
|
|
Output (via print) all of the languages in @p all_langs
|
|
|
|
that satisfy the translation-percentage filter @p filterfunc.
|
|
|
|
Prints a CMake set() command with the @p label as part
|
|
|
|
of the variable name.
|
|
|
|
|
|
|
|
Performs line-wrapping.
|
|
|
|
"""
|
|
|
|
these_langs = [l for s, l in all_langs if filterfunc(s)]
|
2019-02-12 11:26:47 +01:00
|
|
|
out = " ".join(["set( _tx_%s" % label, " ".join(sorted(these_langs)), ")"])
|
2018-12-13 14:50:33 +01:00
|
|
|
width = 68
|
|
|
|
prefix = ""
|
|
|
|
|
|
|
|
while len(out) > width - len(prefix):
|
|
|
|
chunk = out[:out[:width].rfind(" ")]
|
2020-07-31 12:22:40 +02:00
|
|
|
outputter.print("%s%s" % (prefix, chunk))
|
2018-12-13 14:50:33 +01:00
|
|
|
out = out[len(chunk)+1:]
|
|
|
|
prefix = " "
|
2020-07-31 12:22:40 +02:00
|
|
|
outputter.print("%s%s" % (prefix, out))
|
2018-12-13 14:50:33 +01:00
|
|
|
|
2020-07-31 12:07:01 +02:00
|
|
|
|
2020-07-31 12:22:40 +02:00
|
|
|
def get_tx_stats(languages, outputter, verbose):
|
2018-12-13 14:50:33 +01:00
|
|
|
"""
|
|
|
|
Does an API request to Transifex with the given API @p token, getting
|
|
|
|
the translation statistics for the main body of texts. Then prints
|
|
|
|
out CMake settings to replace the _tx_* variables in CMakeLists.txt
|
|
|
|
according to standard criteria.
|
2020-01-27 16:35:56 +01:00
|
|
|
|
|
|
|
If @p verbose is True, prints out language stats as well.
|
2018-12-13 14:50:33 +01:00
|
|
|
"""
|
2019-02-12 11:26:47 +01:00
|
|
|
suppressed_languages = ( "es_ES", ) # In Transifex, but not used
|
2019-08-02 09:32:31 +02:00
|
|
|
# Some languages go into the "incomplete" list by definition,
|
|
|
|
# regardless of their completion status: this can have various reasons.
|
2020-01-27 16:35:56 +01:00
|
|
|
#
|
|
|
|
# Note that Esperanto (eo) is special-cased in CMakeLists.txt
|
|
|
|
# during the build; recent Qt releases *do* support the language,
|
|
|
|
# and it's at-the-least ok.
|
2019-08-02 09:32:31 +02:00
|
|
|
incomplete_languages = (
|
2020-01-27 16:35:56 +01:00
|
|
|
"eo", # Not supported by QLocale < 5.12.1
|
2019-08-02 09:32:31 +02:00
|
|
|
)
|
2019-02-12 11:26:47 +01:00
|
|
|
|
2018-12-13 14:50:33 +01:00
|
|
|
all_langs = []
|
2020-07-31 12:22:40 +02:00
|
|
|
outputter.print("# Total %d languages" % len(languages))
|
2018-12-13 14:50:33 +01:00
|
|
|
for lang_name in languages:
|
2019-02-12 11:26:47 +01:00
|
|
|
if lang_name in suppressed_languages:
|
|
|
|
continue
|
2018-12-13 14:50:33 +01:00
|
|
|
stats = languages[lang_name]["translated"]["percentage"]
|
2020-01-27 16:35:56 +01:00
|
|
|
# Make the by-definition-incomplete languages have a percentage
|
|
|
|
# lower than zero; this way they end up sorted (in -v output)
|
|
|
|
# at the bottom but you can still determine the "actual" percentage.
|
2019-08-02 09:32:31 +02:00
|
|
|
if lang_name in incomplete_languages:
|
2020-01-27 16:35:56 +01:00
|
|
|
stats = -stats
|
2018-12-13 14:50:33 +01:00
|
|
|
all_langs.append((stats, lang_name))
|
|
|
|
|
2020-01-27 16:35:56 +01:00
|
|
|
if verbose:
|
|
|
|
for s, l in sorted(all_langs, reverse=True):
|
2020-07-31 12:22:40 +02:00
|
|
|
outputter.print("# %16s\t%6.2f" % (l, s * 100.0))
|
|
|
|
output_langs(all_langs, outputter, "complete", lambda s : s == 1.0)
|
|
|
|
output_langs(all_langs, outputter, "good", lambda s : 1.0 > s >= 0.75)
|
|
|
|
output_langs(all_langs, outputter, "ok", lambda s : 0.75 > s >= 0.05)
|
|
|
|
output_langs(all_langs, outputter, "incomplete", lambda s : 0.05 > s)
|
2018-12-13 14:50:33 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2020-07-31 12:22:40 +02:00
|
|
|
def get_outputter():
|
|
|
|
return PrintOutputter()
|
|
|
|
|
2018-12-13 14:50:33 +01:00
|
|
|
def main():
|
2020-07-31 10:46:54 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Update Transifex Statistics")
|
|
|
|
parser.add_argument("--verbose", "-v", help="Show statistics", action="store_true")
|
2020-07-31 12:17:07 +02:00
|
|
|
parser.add_argument("--bogus", "-n", help="Use bogus data (do not query Transifex)", action="store_true")
|
2020-07-31 10:46:54 +02:00
|
|
|
args = parser.parse_args()
|
2020-07-31 12:07:01 +02:00
|
|
|
try:
|
2020-07-31 12:17:07 +02:00
|
|
|
if args.bogus:
|
|
|
|
getter = BogusGetter()
|
|
|
|
else:
|
|
|
|
getter = TransifexGetter()
|
2020-07-31 12:22:40 +02:00
|
|
|
with get_outputter() as outputter:
|
|
|
|
return get_tx_stats(getter.languages, outputter, args.verbose)
|
2020-07-31 12:07:01 +02:00
|
|
|
except TXError as e:
|
|
|
|
print("! " + str(e))
|
|
|
|
return 1;
|
2018-12-13 14:50:33 +01:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|