generate_metadata.py 4.1 KB

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