2021-02-24 18:55:11 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
'''
|
|
|
|
|
Script:
|
|
|
|
|
langcheck.py
|
|
|
|
|
Description:
|
|
|
|
|
Join Captcha Bot language texts JSON files validator to verify of the JSON
|
|
|
|
|
of all language files are valid and all languages has the same keys as the
|
|
|
|
|
english language file (reference file).
|
|
|
|
|
Author:
|
|
|
|
|
Jose Miguel Rios Rubio
|
|
|
|
|
Creation date:
|
|
|
|
|
02/10/2020
|
|
|
|
|
Last modified date:
|
|
|
|
|
28/12/2022
|
|
|
|
|
Version:
|
|
|
|
|
1.1.0
|
|
|
|
|
'''
|
|
|
|
|
|
2021-02-24 18:55:11 +00:00
|
|
|
###############################################################################
|
2023-12-27 16:31:50 +00:00
|
|
|
# Imported modules
|
|
|
|
|
###############################################################################
|
2021-02-24 18:55:11 +00:00
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
# JSON Library
|
2020-10-02 17:11:27 +00:00
|
|
|
import json
|
2022-12-28 19:35:43 +00:00
|
|
|
|
|
|
|
|
# Operating System Library
|
2020-10-02 17:11:27 +00:00
|
|
|
import os
|
|
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
# System Library
|
|
|
|
|
from sys import exit as sys_exit
|
|
|
|
|
|
|
|
|
|
# Local Constants Library
|
2020-10-02 17:11:27 +00:00
|
|
|
from constants import CONST
|
|
|
|
|
|
2023-12-27 16:31:50 +00:00
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
# Auxiliary Functions
|
2021-02-24 18:55:11 +00:00
|
|
|
###############################################################################
|
2020-10-02 17:11:27 +00:00
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
def is_valid(lang_name, lang_to_check, reference_lang):
|
|
|
|
|
'''
|
|
|
|
|
Validate JSON language texts with a reference language (i.e. english).
|
|
|
|
|
It checks for mismatch keys or number of brackets in key values.
|
|
|
|
|
'''
|
2022-02-07 12:27:33 +00:00
|
|
|
l_missing_keys = []
|
2022-12-28 19:35:43 +00:00
|
|
|
l_brackets_mismatch_keys = []
|
|
|
|
|
for key in reference_lang:
|
|
|
|
|
if key not in lang_to_check:
|
2022-02-07 12:27:33 +00:00
|
|
|
l_missing_keys.append(key)
|
2022-12-28 19:35:43 +00:00
|
|
|
num_expected_brackets = reference_lang[key].count("{}")
|
|
|
|
|
num_brackets = lang_to_check[key].count("{}")
|
2022-02-07 12:27:33 +00:00
|
|
|
if num_brackets != num_expected_brackets:
|
2022-12-28 19:35:43 +00:00
|
|
|
l_brackets_mismatch_keys.append(key)
|
|
|
|
|
if len(lang_name) == 2:
|
|
|
|
|
lang_name = f"{lang_name} "
|
|
|
|
|
if len(l_missing_keys) != 0:
|
|
|
|
|
print(f"{lang_name} - FAIL - Missing Keys: {l_missing_keys}")
|
2020-10-02 17:11:27 +00:00
|
|
|
return False
|
2022-12-28 19:35:43 +00:00
|
|
|
if len(l_brackets_mismatch_keys) != 0:
|
|
|
|
|
miss_key = l_brackets_mismatch_keys
|
|
|
|
|
print(f"{lang_name} - FAIL - Brackets Mismatch in Keys: {miss_key}")
|
|
|
|
|
return False
|
|
|
|
|
print(f"{lang_name} - OK")
|
|
|
|
|
return True
|
2020-10-02 17:11:27 +00:00
|
|
|
|
2023-12-27 16:31:50 +00:00
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
# Main Function
|
2021-02-24 18:55:11 +00:00
|
|
|
###############################################################################
|
2020-10-02 17:11:27 +00:00
|
|
|
|
|
|
|
|
def main():
|
2022-12-28 19:35:43 +00:00
|
|
|
'''Main Function.'''
|
|
|
|
|
file_path_en = os.path.join(CONST["LANG_DIR"], "en.json")
|
|
|
|
|
with open(file_path_en, encoding="utf8") as file_en:
|
|
|
|
|
json_en = json.load(file_en)
|
2020-10-02 17:11:27 +00:00
|
|
|
errs = False
|
|
|
|
|
for lang in os.listdir(CONST["LANG_DIR"]):
|
2022-12-28 19:35:43 +00:00
|
|
|
if not lang.endswith(".json"):
|
2020-10-02 17:11:27 +00:00
|
|
|
continue
|
2022-12-28 19:35:43 +00:00
|
|
|
if lang == "en.json":
|
2020-10-02 17:11:27 +00:00
|
|
|
continue
|
2022-12-28 19:35:43 +00:00
|
|
|
file_path_lang = os.path.join(CONST["LANG_DIR"], lang)
|
|
|
|
|
with open(file_path_lang, encoding="utf8") as file_lang:
|
2020-10-02 17:11:27 +00:00
|
|
|
try:
|
2022-12-28 19:35:43 +00:00
|
|
|
lang_name = lang.split(".")[0]
|
|
|
|
|
json_lang = json.load(file_lang)
|
|
|
|
|
if not is_valid(lang_name, json_lang, json_en):
|
2020-10-02 17:11:27 +00:00
|
|
|
errs = True
|
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
|
errs = True
|
2022-12-28 18:58:54 +00:00
|
|
|
print(f"{lang} is not valid json")
|
2020-10-02 17:11:27 +00:00
|
|
|
if errs:
|
2022-12-28 19:35:43 +00:00
|
|
|
return 1
|
|
|
|
|
return 0
|
|
|
|
|
|
2020-10-02 17:11:27 +00:00
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
###############################################################################
|
2023-12-27 16:31:50 +00:00
|
|
|
# Runnable Main Script Detection
|
|
|
|
|
###############################################################################
|
2020-10-02 17:11:27 +00:00
|
|
|
|
2022-12-28 19:35:43 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
sys_exit(main())
|