generate_metadata.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python3
  2. # Author: Torsten Grote
  3. # License: GPLv3 or later
  4. # copied on 2017/11/06 from https://github.com/grote/Transportr/blob/master/fastlane/generate_metadata.py
  5. # adapted by Tobias Kaminsky
  6. # SPDX-FileCopyrightText: 2017 Torsten Grote
  7. # SPDX-FileCopyrightText: 2017-2018 Tobias Kaminsky <tobias@kaminsky.me>
  8. # SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
  9. import codecs
  10. import os
  11. import shutil
  12. from xml.etree import ElementTree
  13. XML_PATH = '../../app/src/main/res'
  14. METADATA_PATH = '../../src/generic/fastlane/metadata/android/'
  15. METADATA_DEV_PATH = '../../src/versionDev/fastlane/metadata/android/'
  16. DEFAULT_LANG = 'en-US'
  17. LANG_MAP = {
  18. 'values': 'en-US',
  19. 'values-en-rGB': 'en-GB',
  20. 'values-ca': 'ca',
  21. 'values-cs': 'cs-CZ',
  22. 'values-de': 'de-DE',
  23. 'values-es': 'es-ES',
  24. 'values-fr': 'fr-FR',
  25. 'values-hu': 'hu-HU',
  26. 'values-it': 'it-IT',
  27. 'values-pt-rBR': 'pt-BR',
  28. 'values-pt-rPT': 'pt-PT',
  29. 'values-ta': 'ta-IN',
  30. 'values-sv': 'sv-SE',
  31. 'values-sq-rAL': 'sq-AL',
  32. 'values-sq-rMK': 'sq-MK',
  33. 'values-iw-rIL': 'iw-IL',
  34. 'values-ar': 'ar-AR',
  35. 'values-bg-rBG': 'bg-BG',
  36. 'values-da': 'da-DK',
  37. 'values-fi-rFI': 'fi-FI',
  38. 'values-gl-rES': 'gl-ES',
  39. 'values-tr': 'tr-TR',
  40. 'values-uk': 'uk-UK',
  41. 'values-vi': 'vi-VI',
  42. 'values-ro': 'ro-RO',
  43. 'values-ou': 'ru-RU',
  44. 'values-sr': 'sr-SR',
  45. 'values-pl': 'pl-PL',
  46. 'values-el': 'el-GR',
  47. 'values-ko': 'ko-KR',
  48. 'values-nl': 'nl-NL',
  49. 'values-ja': 'ja-JP',
  50. 'values-no-rNO': 'no-NO',
  51. 'values-eu': 'eu-ES',
  52. 'values-lt-rLT': 'lt-LT',
  53. 'values-zh-rKN': 'zh-HK',
  54. 'values-zk': 'zk-CN',
  55. 'values-is': 'is-IS',
  56. 'values-id': 'id-ID',
  57. 'values-cs-rCZ': 'cs-CZ',
  58. 'values-sl': 'sl-SL',
  59. 'values-fa': 'fa-FA'
  60. }
  61. PATH = os.path.dirname(os.path.realpath(__file__))
  62. def main():
  63. path = os.path.join(PATH, XML_PATH)
  64. for entry in os.listdir(path):
  65. directory = os.path.join(path, entry)
  66. if not os.path.isdir(directory) or entry not in LANG_MAP.keys():
  67. continue
  68. strings_file = os.path.join(directory, 'strings.xml')
  69. if not os.path.isfile(strings_file):
  70. print("Error: %s does not exist" % strings_file)
  71. continue
  72. print()
  73. print(LANG_MAP[entry])
  74. print("Parsing %s..." % strings_file)
  75. e = ElementTree.parse(strings_file).getroot()
  76. short_desc = e.find('.//string[@name="store_short_desc"]')
  77. full_desc = e.find('.//string[@name="store_full_desc"]')
  78. short_dev_desc = e.find('.//string[@name="store_short_dev_desc"]')
  79. full_dev_desc = e.find('.//string[@name="store_full_dev_desc"]')
  80. if short_desc is not None:
  81. save_file(short_desc.text, LANG_MAP[entry], 'short_description.txt', False)
  82. if short_dev_desc is not None:
  83. save_file(short_dev_desc.text, LANG_MAP[entry], 'short_description.txt', True)
  84. if full_desc is not None:
  85. save_file(full_desc.text, LANG_MAP[entry], 'full_description.txt', False)
  86. if full_dev_desc is not None:
  87. save_file(full_dev_desc.text, LANG_MAP[entry], 'full_description.txt', True)
  88. def save_file(text, directory, filename, dev):
  89. if dev:
  90. directory_path = os.path.join(PATH, METADATA_DEV_PATH, directory)
  91. else:
  92. directory_path = os.path.join(PATH, METADATA_PATH, directory)
  93. if not os.path.exists(directory_path):
  94. os.makedirs(directory_path)
  95. if filename == 'short_description.txt':
  96. limit = 80
  97. else:
  98. limit = 0
  99. text = clean_text(text, limit)
  100. check_title(directory_path)
  101. file_path = os.path.join(directory_path, filename)
  102. print("Writing %s..." % file_path)
  103. with codecs.open(file_path, 'w', 'utf-8') as f:
  104. f.write(text)
  105. def clean_text(text, limit=0):
  106. text = text.replace('\\\'', '\'').replace('\\n', '\n')
  107. if limit != 0 and len(text) > limit:
  108. print("Warning: Short Description longer than 80 characters, truncating...")
  109. text = text[:limit]
  110. return text
  111. def check_title(directory):
  112. title_path = os.path.join(directory, 'title.txt')
  113. if os.path.exists(title_path):
  114. return
  115. default_title_path = os.path.join(directory, '..', DEFAULT_LANG, 'title.txt')
  116. shutil.copy(default_title_path, title_path)
  117. if __name__ == "__main__":
  118. main()