txstats.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #! /usr/bin/env python
  2. #
  3. # Uses the Transifex API to get a list of enabled languages,
  4. # and outputs CMake settings for inclusion into CMakeLists.txt.
  5. import sys
  6. def get_tx_credentials():
  7. """
  8. Gets the API token out of the user's .transifexrc (this is supposed
  9. to be secure).
  10. """
  11. import configparser
  12. import os
  13. txconfig_name = os.path.expanduser("~/.transifexrc")
  14. try:
  15. with open(txconfig_name, "r") as f:
  16. parser = configparser.ConfigParser()
  17. parser.read_file(f)
  18. return parser.get("https://www.transifex.com", "password")
  19. except IOError as e:
  20. return None
  21. def output_langs(all_langs, label, filterfunc):
  22. """
  23. Output (via print) all of the languages in @p all_langs
  24. that satisfy the translation-percentage filter @p filterfunc.
  25. Prints a CMake set() command with the @p label as part
  26. of the variable name.
  27. Performs line-wrapping.
  28. """
  29. these_langs = [l for s, l in all_langs if filterfunc(s)]
  30. out = " ".join(["set( _tx_%s" % label, " ".join(sorted(these_langs)), ")"])
  31. width = 68
  32. prefix = ""
  33. while len(out) > width - len(prefix):
  34. chunk = out[:out[:width].rfind(" ")]
  35. print("%s%s" % (prefix, chunk))
  36. out = out[len(chunk)+1:]
  37. prefix = " "
  38. print("%s%s" % (prefix, out))
  39. def get_tx_stats(token):
  40. """
  41. Does an API request to Transifex with the given API @p token, getting
  42. the translation statistics for the main body of texts. Then prints
  43. out CMake settings to replace the _tx_* variables in CMakeLists.txt
  44. according to standard criteria.
  45. """
  46. import requests
  47. r = requests.get("https://api.transifex.com/organizations/calamares/projects/calamares/resources/calamares-master/", auth=("api", token))
  48. if r.status_code != 200:
  49. return 1
  50. suppressed_languages = ( "es_ES", ) # In Transifex, but not used
  51. all_langs = []
  52. j = r.json()
  53. languages = j["stats"]
  54. print("# Total %d languages" % len(languages))
  55. for lang_name in languages:
  56. if lang_name in suppressed_languages:
  57. continue
  58. stats = languages[lang_name]["translated"]["percentage"]
  59. all_langs.append((stats, lang_name))
  60. output_langs(all_langs, "complete", lambda s : s == 1.0)
  61. output_langs(all_langs, "good", lambda s : 1.0 > s >= 0.75)
  62. output_langs(all_langs, "ok", lambda s : 0.75 > s >= 0.05)
  63. output_langs(all_langs, "incomplete", lambda s : 0.05 > s)
  64. return 0
  65. def main():
  66. cred = get_tx_credentials()
  67. if cred:
  68. return get_tx_stats(cred)
  69. else:
  70. print("! Could not find API token in ~/.transifexrc")
  71. return 1
  72. return 0
  73. if __name__ == "__main__":
  74. sys.exit(main())