TestShortcutKeys.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import re
  3. import pytest
  4. MSGCTXT = "msgctxt" # Scope of the text . Like : msgctxt "@action:inmenu menubar:help"
  5. MSGID = "msgid" # The id tag, also English text version
  6. MSGTR = "msgstr" # The translation tag
  7. COLOR_WARNING = '\033[93m'
  8. COLOR_ENDC = '\033[0m'
  9. regex_patter = '(&[\w])' #"&[a-zA-Z0-9]" - Search char '&' and at least one character after it
  10. # Directory where this python file resides
  11. SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
  12. # Check default language shortcut keys, by default it is English
  13. def test_default_shortcut_keys():
  14. language_folder = "de_DE" # choose German language folder, because we only need to test English shortcut keys, the message id is English translation
  15. translation_file_name = "cura.po"
  16. cura_path = os.path.abspath(os.path.join(SCRIPT_DIR, ".."))
  17. language_file_path = os.path.join(cura_path, "resources", "i18n", language_folder, translation_file_name)
  18. not_valid_shortcut_keys = getDuplicatedShortcutKeys(language_file_path, False)
  19. if len(not_valid_shortcut_keys) != 0:
  20. temp = '%s' % ', '.join(map(str, not_valid_shortcut_keys))
  21. print(COLOR_WARNING + "NOT VALID SHORTCUT KEYS: " + temp + COLOR_ENDC)
  22. assert len(not_valid_shortcut_keys) == 0
  23. # If the 'findShortcutKeysInTranslation' is False the function will search shortcut keys in message id
  24. def getDuplicatedShortcutKeys(language_file_path: str, find_shortcut_keys_in_translation: bool):
  25. last_translation_scope = ""
  26. # {shortcut_key, {scope, [translation_text]}}
  27. shortcut_keys = dict()
  28. with open(language_file_path, 'r') as f:
  29. for text in f:
  30. if text.startswith(MSGCTXT):
  31. last_translation_scope = text
  32. elif text.startswith(MSGID) and find_shortcut_keys_in_translation or text.startswith(MSGTR) and not find_shortcut_keys_in_translation:
  33. # if text has '&'symbol and at least one character (char or digit) after it
  34. # ex '&acr mytest' -> this should return '&a'
  35. # ex '& acr mytest' -> returns None
  36. the_shortcut_key_word = re.search(regex_patter, text)
  37. if the_shortcut_key_word is not None:
  38. # take only char after '&' symbol
  39. the_shortcut_key = the_shortcut_key_word.group(0)[1]
  40. the_shortcut_key = the_shortcut_key.upper() # make all shortcut keys capital
  41. # The shortcut key is not yet added
  42. if the_shortcut_key not in shortcut_keys:
  43. scope_translation = dict()
  44. scope_translation[last_translation_scope] = []
  45. scope_translation[last_translation_scope].append(text)
  46. shortcut_keys[the_shortcut_key] = scope_translation
  47. else:
  48. # check if the shortcut key scope is already added or not
  49. if last_translation_scope not in shortcut_keys[the_shortcut_key]:
  50. scope_translation = dict()
  51. scope_translation[last_translation_scope] = []
  52. scope_translation[last_translation_scope].append(text)
  53. shortcut_keys[the_shortcut_key].update(scope_translation)
  54. # if the scope already exist then add the key
  55. elif last_translation_scope in shortcut_keys[the_shortcut_key]:
  56. shortcut_keys[the_shortcut_key][last_translation_scope].append(text)
  57. last_translation_scope = ""
  58. not_valid_shortcut_keys = []
  59. # Validate all shortcut keys
  60. for shortcut_key, scopes in shortcut_keys.items():
  61. # check, whether the key exist in the same scope multiple times or not
  62. for key, items in scopes.items():
  63. # The shortcut keys should not be more than one time in the same scope
  64. if len(items) > 1:
  65. not_valid_shortcut_keys += items
  66. return not_valid_shortcut_keys
  67. @pytest.mark.parametrize("language_type", [("de_DE"),("es_ES"),("fi_FI"),("fr_FR"),("it_IT"),("ja_JP"),("ko_KR"),
  68. ("nl_NL"),("pl_PL"),("pt_BR"),("ru_RU"),("tr_TR")])
  69. def test_shortcut_keys(language_type):
  70. language_folder = language_type
  71. translation_file_name = "cura.po"
  72. plugin_file_path = os.path.dirname(os.path.abspath(__file__))
  73. path_records = os.path.split(plugin_file_path)
  74. global_path = path_records[:-1]
  75. cura_path = os.path.join(*global_path)
  76. language_file_path = os.path.join(cura_path, "resources", "i18n", language_folder, translation_file_name)
  77. not_valid_shortcut_keys = getDuplicatedShortcutKeys(language_file_path, False)
  78. if len(not_valid_shortcut_keys) != 0:
  79. temp = "%s" % ", ".join(map(str, not_valid_shortcut_keys))
  80. print(COLOR_WARNING + "NOT VALID SHORTCUT KEYS: " + temp + COLOR_ENDC)
  81. assert len(not_valid_shortcut_keys) == 0