i18n: refactoring txstats

This commit is contained in:
Adriaan de Groot 2020-07-31 12:07:01 +02:00
parent fad364993c
commit 3762d4df05

View File

@ -10,7 +10,33 @@
import sys import sys
import argparse import argparse
def get_tx_credentials(): class TXError(Exception):
pass
class TransifexGetter(object):
"""
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.
"""
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 Gets the API token out of the user's .transifexrc (this is supposed
to be secure). to be secure).
@ -27,6 +53,9 @@ def get_tx_credentials():
except IOError as e: except IOError as e:
return None return None
def output_langs(all_langs, label, filterfunc): def output_langs(all_langs, label, filterfunc):
""" """
Output (via print) all of the languages in @p all_langs Output (via print) all of the languages in @p all_langs
@ -48,7 +77,8 @@ def output_langs(all_langs, label, filterfunc):
prefix = " " prefix = " "
print("%s%s" % (prefix, out)) print("%s%s" % (prefix, out))
def get_tx_stats(token, verbose):
def get_tx_stats(languages, verbose):
""" """
Does an API request to Transifex with the given API @p token, getting Does an API request to Transifex with the given API @p token, getting
the translation statistics for the main body of texts. Then prints the translation statistics for the main body of texts. Then prints
@ -57,12 +87,6 @@ def get_tx_stats(token, verbose):
If @p verbose is True, prints out language stats as well. If @p verbose is True, prints out language stats as well.
""" """
import requests
r = requests.get("https://api.transifex.com/organizations/calamares/projects/calamares/resources/calamares/", auth=("api", token))
if r.status_code != 200:
return 1
suppressed_languages = ( "es_ES", ) # In Transifex, but not used suppressed_languages = ( "es_ES", ) # In Transifex, but not used
# Some languages go into the "incomplete" list by definition, # Some languages go into the "incomplete" list by definition,
# regardless of their completion status: this can have various reasons. # regardless of their completion status: this can have various reasons.
@ -75,9 +99,6 @@ def get_tx_stats(token, verbose):
) )
all_langs = [] all_langs = []
j = r.json()
languages = j["stats"]
print("# Total %d languages" % len(languages)) print("# Total %d languages" % len(languages))
for lang_name in languages: for lang_name in languages:
if lang_name in suppressed_languages: if lang_name in suppressed_languages:
@ -105,12 +126,12 @@ def main():
parser = argparse.ArgumentParser(description="Update Transifex Statistics") parser = argparse.ArgumentParser(description="Update Transifex Statistics")
parser.add_argument("--verbose", "-v", help="Show statistics", action="store_true") parser.add_argument("--verbose", "-v", help="Show statistics", action="store_true")
args = parser.parse_args() args = parser.parse_args()
cred = get_tx_credentials() try:
if cred: getter = TransifexGetter()
return get_tx_stats(cred, args.verbose) return get_tx_stats(getter.languages, args.verbose)
else: except TXError as e:
print("! Could not find API token in ~/.transifexrc") print("! " + str(e))
return 1 return 1;
return 0 return 0
if __name__ == "__main__": if __name__ == "__main__":