Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLipu Fei <lipu.fei815@gmail.com>2018-09-05 17:17:02 +0300
committerJaime van Kessel <nallath@gmail.com>2018-09-06 17:06:45 +0300
commit784f7cca7601e5e52c9e8e0046a1fe4370e5436b (patch)
treecd1f6f8c5f57e86b7206c364e6ed0f01319d53cf /scripts
parenta8a88e5ccb36071259e33006fa8bfb8528a1cd8b (diff)
Move shortcut key check into a separate script
Diffstat (limited to 'scripts')
-rw-r--r--scripts/check_shortcut_keys.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/scripts/check_shortcut_keys.py b/scripts/check_shortcut_keys.py
new file mode 100644
index 0000000000..2796002c3f
--- /dev/null
+++ b/scripts/check_shortcut_keys.py
@@ -0,0 +1,135 @@
+#!/usr/bin/env python3
+#
+# This script checks for duplicate shortcut keys in all translation files.
+#
+import collections
+import os
+import sys
+from typing import Optional
+
+COLOR_WARNING = '\033[93m'
+COLOR_ENDC = '\033[0m'
+
+regex_patter = '(&[\w])' #"&[a-zA-Z0-9]" - Search char '&' and at least one character after it
+
+# Directory where this python file resides
+SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
+
+
+class ShortcutKeysChecker:
+
+ MSGCTXT = "msgctxt" # Scope of the text . Like : msgctxt "@action:inmenu menubar:help"
+ MSGID = "msgid" # The id tag, also English text version
+ MSGSTR = "msgstr" # The translation tag
+
+ def has_duplicates(self, filename: str) -> bool:
+ """
+ Checks if the given file has duplicate shortcut keys.
+ """
+ with open(filename, "r", encoding = "utf-8") as f:
+ all_lines = f.readlines()
+
+ all_lines = [l.strip() for l in all_lines]
+ shortcut_dict = collections.defaultdict(dict)
+ found_ctxt = False
+ current_data = dict()
+ current_field = None
+ start_line = None
+
+ for idx, line in enumerate(all_lines):
+ if line.startswith(self.MSGCTXT):
+ found_ctxt = True
+ current_data.clear()
+ current_field = self.MSGCTXT
+ current_data[current_field] = self._fetch_data(line)
+ start_line = idx
+ continue
+
+ elif found_ctxt and line.startswith(self.MSGID):
+ current_field = self.MSGID
+ current_data[current_field] = self._fetch_data(line)
+ continue
+
+ elif found_ctxt and line.startswith(self.MSGSTR):
+ current_field = self.MSGSTR
+ current_data[current_field] = self._fetch_data(line)
+ continue
+
+ elif found_ctxt and line.startswith('"'):
+ data = line[1:-1] # strip the beginning and ending double-quotes
+ current_data[current_field] += data
+ continue
+
+ if current_data:
+ self._process_translation(shortcut_dict, current_data, start_line)
+
+ current_data.clear()
+ current_field = None
+ found_ctxt = False
+ start_line = None
+
+ return self._show_all_duplicates(shortcut_dict, filename)
+
+ def _fetch_data(self, line: str) -> str:
+ return (line.split(" ", 1)[-1])[1:-1]
+
+ def _process_translation(self, shortcut_dict: dict, data_dict: dict, start_line: int) -> None:
+ # Only check the ones with shortcuts
+ msg = data_dict[self.MSGID]
+ if data_dict[self.MSGSTR]:
+ msg = data_dict[self.MSGSTR]
+ shortcut_key = self._get_shortcut_key(msg)
+ if shortcut_key is None:
+ return
+
+ msg_section = data_dict[self.MSGCTXT]
+ keys_dict = shortcut_dict[msg_section]
+ if shortcut_key not in keys_dict:
+ keys_dict[shortcut_key] = dict()
+ existing_data_dict = keys_dict[shortcut_key]
+ existing_data_dict[start_line] = {"message": msg,
+ "shortcut_key": shortcut_key,
+ }
+
+ def _get_shortcut_key(self, text: str) -> Optional[str]:
+ key = None
+ if text.count("&") == 1:
+ idx = text.find("&") + 1
+ if idx < len(text):
+ character = text[idx]
+ if not character.isspace():
+ key = character.lower()
+ return key
+
+ def _show_all_duplicates(self, shortcut_dict: dict, filename: str) -> bool:
+ has_duplicates = False
+ for keys_dict in shortcut_dict.values():
+ for shortcut_key, data_dict in keys_dict.items():
+ if len(data_dict) == 1:
+ continue
+
+ has_duplicates = True
+
+ print("")
+ print("The following messages have the same shortcut key '%s':" % shortcut_key)
+ for line, msg in data_dict.items():
+ relative_filename = (filename.rsplit("..", 1)[-1])[1:]
+ print(" - [%s] L%7d : [%s]" % (relative_filename, line, msg))
+
+ return has_duplicates
+
+
+if __name__ == "__main__":
+ checker = ShortcutKeysChecker()
+ all_dirnames = [""]
+ for _, dirnames, _ in os.walk(os.path.join(SCRIPT_DIR, "..", "resources", "i18n")):
+ all_dirnames += [dn for dn in dirnames]
+ break
+
+ found_duplicates = False
+ for dirname in all_dirnames:
+ file_name = "cura.pot" if not dirname else "cura.po"
+ file_path = os.path.join(SCRIPT_DIR, "..", "resources", "i18n", dirname, file_name)
+ found_duplicates = found_duplicates or checker.has_duplicates(file_path)
+
+ sys.exit(0 if not found_duplicates else 1)