TestShortcutKeys.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import re
  3. import unittest
  4. import pytest
  5. MSGCTXT = "msgctxt" # Scope of the text
  6. MSGID = "msgid" # The id of the text, also English version
  7. MSGTR = "msgstr" # The translation
  8. COLOR_WARNING = '\033[93m'
  9. COLOR_ENDC = '\033[0m'
  10. regex_patter = '(&[\w])' #"&[a-zA-Z0-9]" - Search char '&' and at least one character after it, the '+' returns one char after '&'
  11. @pytest.mark.parametrize("language_type", [("de_DE"),("es_ES"),("fi_FI"),("fr_FR"),("hu_HU"),("it_IT"),("ja_JP"),("ko_KR"),("nl_NL"),("pl_PL"),("pt_BR"),("ru_RU"),("tr_TR")])
  12. def test_shortcut_keys(language_type):
  13. language_folder = language_type
  14. translation_file_name = "cura.po"
  15. plugin_file_path = os.path.dirname(os.path.abspath(__file__))
  16. path_records = os.path.split(plugin_file_path)
  17. global_path = path_records[:-1]
  18. cura_path = os.path.join(*global_path)
  19. language_file_path = os.path.join(cura_path,"resources","i18n",language_folder, translation_file_name)
  20. last_translation_scope = ""
  21. last_translation_id = ""
  22. last_translation_text = ""
  23. #{shortcut_key, {scope, [translation_text]}}
  24. shortcut_keys = dict()
  25. with open(language_file_path,'r') as f:
  26. for text in f:
  27. if text.startswith(MSGCTXT):
  28. last_translation_scope = text
  29. elif text.startswith(MSGID):
  30. last_translation_id = text
  31. elif text.startswith(MSGTR):
  32. last_translation_text = text
  33. #if text has '&'symbol and at least one character (char or digit) should be after it
  34. # ex '&acr mytest' -> this should return '&a'
  35. the_shortcut_key_word = re.search(regex_patter, text)
  36. if the_shortcut_key_word is not None:
  37. # take only char after '&' symbol
  38. the_shortcut_key = the_shortcut_key_word.group(0)[1]
  39. the_shortcut_key = the_shortcut_key.upper() #make all shortcut keys capital
  40. #The shortcut key is not yet added
  41. if the_shortcut_key not in shortcut_keys:
  42. scope_translation = dict()
  43. scope_translation[last_translation_scope] = []
  44. scope_translation[last_translation_scope].append(text)
  45. shortcut_keys[the_shortcut_key] = scope_translation
  46. else:
  47. #check if the shortcut key scope is already added
  48. if last_translation_scope not in shortcut_keys[the_shortcut_key]:
  49. scope_translation = dict()
  50. scope_translation[last_translation_scope] = []
  51. scope_translation[last_translation_scope].append(text)
  52. shortcut_keys[the_shortcut_key].update(scope_translation)
  53. # if the scope already exist then add the key
  54. elif last_translation_scope in shortcut_keys[the_shortcut_key]:
  55. shortcut_keys[the_shortcut_key][last_translation_scope].append(text)
  56. last_translation_scope = ""
  57. last_translation_id = ""
  58. not_valid_shortcut_keys = []
  59. #Validate all shortcut keys
  60. for shortcut_key, scopes in shortcut_keys.items():
  61. #check, whether the exist in one scope multiple times or not
  62. for key, items in scopes.items():
  63. if len (items) > 1:
  64. not_valid_shortcut_keys += items
  65. if len(not_valid_shortcut_keys) != 0:
  66. temp='%s' % ', '.join(map(str, not_valid_shortcut_keys))
  67. print(COLOR_WARNING + "NOT VALID KEYS: " + temp + COLOR_ENDC)
  68. assert len(not_valid_shortcut_keys) == 0
  69. if __name__ == "__main__":
  70. unittest.main()