TLG_JoinCaptchaBot/sources/langcheck.py

105 wiersze
3.2 KiB
Python
Czysty Zwykły widok Historia

2021-02-24 18:55:11 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
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
# JSON Library
2020-10-02 17:11:27 +00:00
import json
# Operating System Library
2020-10-02 17:11:27 +00:00
import os
# 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
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.
'''
l_missing_keys = []
l_brackets_mismatch_keys = []
for key in reference_lang:
if key not in lang_to_check:
l_missing_keys.append(key)
num_expected_brackets = reference_lang[key].count("{}")
num_brackets = lang_to_check[key].count("{}")
if num_brackets != num_expected_brackets:
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
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():
'''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"]):
if not lang.endswith(".json"):
2020-10-02 17:11:27 +00:00
continue
if lang == "en.json":
2020-10-02 17:11:27 +00:00
continue
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:
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:
return 1
return 0
2020-10-02 17:11:27 +00:00
###############################################################################
2023-12-27 16:31:50 +00:00
# Runnable Main Script Detection
###############################################################################
2020-10-02 17:11:27 +00:00
if __name__ == "__main__":
sys_exit(main())