From 462e4ca5c6d15e50c781d5c29a478df276543440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 3 Mar 2017 10:36:17 +0100 Subject: [PATCH] Add verify locale keys usage script --- scripts/verify-locale-keys-usage.py | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 scripts/verify-locale-keys-usage.py diff --git a/scripts/verify-locale-keys-usage.py b/scripts/verify-locale-keys-usage.py new file mode 100755 index 00000000..b461e27c --- /dev/null +++ b/scripts/verify-locale-keys-usage.py @@ -0,0 +1,54 @@ +import json +import os + +ROOT_PATH = os.path.dirname(os.path.dirname(__file__)) +DEFAULT_LOCALE_PATH = os.path.join(ROOT_PATH, "app/locales/taiga/locale-en.json") + + +def keywords(key, value): + if key is not None and not isinstance(value, dict): + return [".".join(key)] + + if key is not None and isinstance(value, dict): + kws = [] + for item_key in value.keys(): + kws += keywords(key+[item_key], value[item_key]) + return kws + + if key is None and isinstance(value, dict): + kws = [] + for item_key in value.keys(): + kws += keywords([item_key], value[item_key]) + return kws + + +def read_file(path): + with open(path) as fd: + return fd.read() + + +def check_keyword(keyword, files_text): + for text in files_text: + if text.find(keyword) != -1: + return True + return False + + +def verify_keywords_usage(): + locales = json.load(open(DEFAULT_LOCALE_PATH)) + + all_files = [] + for root, dirs, files in os.walk(os.path.join(ROOT_PATH, 'app')): + json_and_jade_files = list(filter(lambda x: x.endswith('.coffee') or x.endswith('.jade'), files)) + json_and_jade_files = map(lambda x: os.path.join(root, x), json_and_jade_files) + all_files += json_and_jade_files + + all_files_text = list(map(read_file, all_files)) + + for keyword in keywords(None, locales): + if not check_keyword(keyword, all_files_text): + print("Keyword unused: {}".format(keyword)) + + +if __name__ == "__main__": + verify_keywords_usage()