From abe37e4bcc070847052c784ca7e3c4fc05e607c2 Mon Sep 17 00:00:00 2001 From: Alex Luo <60013046+AlexLuo602@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:09:08 -0800 Subject: [PATCH 1/6] Add blueprint for error handling --- .../src/internationalize/error_handler.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 i18nilize/src/internationalize/error_handler.py diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py new file mode 100644 index 0000000..700afcc --- /dev/null +++ b/i18nilize/src/internationalize/error_handler.py @@ -0,0 +1,70 @@ +import os + +""" +Error Handler Class +""" +class ErrorHandler(): + """ + Sets up the directory where the languages are located + """ + def __init__(self, translations_dir): + self.translations_dif = translations_dir + + """ + Verify each language file is valid + """ + def verify_languages(self): + """ + key: language containing an error + value: error message + """ + errors = {} + + all_languages = os.listdir(self.translations_dif) + + for language in all_languages: + error = self.handle_error(language, False) + if error != "": + errors[language] = error + + return errors + + + """ + Used to handle an error from a language + Input: string: language_file (source of error), boolean: error_expected (is the error expected?)\ + Output: descriptive string about the error from the language file + """ + def handle_error(self, language, error_expected): + result = "" + + # Verify if file is invalid + result = self.handle_invalid_file(language) + if result != "": + return result + + # Verify if any keys are invalid + result = self.handle_invalid_keys(language) + + if result == "" and error_expected: + raise Exception(f"expected error in {language} but no error was found") + + return result + + """ + Checks if given language is in an invalid file + Output: + - An empty string if there isn't any errors + - A descriptive message about the error + """ + def handle_invalid_file(self, language): + return "" + + """ + Checks if given language contains any invalid keys + Output: + - An empty string if there aren't any errors + - A descriptive message about the invalid key(s) + """ + def handle_invalid_keys(self, language): + return "" \ No newline at end of file From d9a39dac4a25a5511755825a432befb9bd370217 Mon Sep 17 00:00:00 2001 From: Brian Park Date: Thu, 28 Nov 2024 14:54:20 -0800 Subject: [PATCH 2/6] added key validation check + tests --- .../src/internationalize/error_handler.py | 5 +++ .../internationalize/languages/turkish.json | 1 + .../src/internationalize/languages/urdu.json | 1 + i18nilize/src/localization.egg-info/PKG-INFO | 25 ++++++++++++ .../src/localization.egg-info/SOURCES.txt | 19 +++++++++ .../dependency_links.txt | 1 + .../src/localization.egg-info/top_level.txt | 1 + i18nilize/tests/test_error_handler.py | 39 +++++++++++++++++++ 8 files changed, 92 insertions(+) create mode 100644 i18nilize/src/internationalize/languages/turkish.json create mode 100644 i18nilize/src/internationalize/languages/urdu.json create mode 100644 i18nilize/src/localization.egg-info/PKG-INFO create mode 100644 i18nilize/src/localization.egg-info/SOURCES.txt create mode 100644 i18nilize/src/localization.egg-info/dependency_links.txt create mode 100644 i18nilize/src/localization.egg-info/top_level.txt create mode 100644 i18nilize/tests/test_error_handler.py diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py index 700afcc..d5eb80c 100644 --- a/i18nilize/src/internationalize/error_handler.py +++ b/i18nilize/src/internationalize/error_handler.py @@ -67,4 +67,9 @@ def handle_invalid_file(self, language): - A descriptive message about the invalid key(s) """ def handle_invalid_keys(self, language): + for key in language.keys(): + if not isinstance(key,str): + return "Key is not a string." + if not key.strip(): + return "Key is empty." return "" \ No newline at end of file diff --git a/i18nilize/src/internationalize/languages/turkish.json b/i18nilize/src/internationalize/languages/turkish.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/i18nilize/src/internationalize/languages/turkish.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/i18nilize/src/internationalize/languages/urdu.json b/i18nilize/src/internationalize/languages/urdu.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/i18nilize/src/internationalize/languages/urdu.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/i18nilize/src/localization.egg-info/PKG-INFO b/i18nilize/src/localization.egg-info/PKG-INFO new file mode 100644 index 0000000..dd59d81 --- /dev/null +++ b/i18nilize/src/localization.egg-info/PKG-INFO @@ -0,0 +1,25 @@ +Metadata-Version: 2.1 +Name: localization +Version: 1.0.0 +Summary: A localization package for microservices +Author: UBC Launchpad +Author-email: strategy@ubclaunchpad.com +Classifier: Programming Language :: Python :: 3 +Classifier: License :: OSI Approved :: MIT License +Classifier: Operating System :: OS Independent +Requires-Python: >=3.6 +Description-Content-Type: text/markdown +License-File: LICENSE.txt + +### L10n +A simple, lightweight interface for Python-based applications handle anything l10n/i18n +MIT License + +Copyright (c) [2024] [UBC Launchpad] + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/i18nilize/src/localization.egg-info/SOURCES.txt b/i18nilize/src/localization.egg-info/SOURCES.txt new file mode 100644 index 0000000..5cb9de1 --- /dev/null +++ b/i18nilize/src/localization.egg-info/SOURCES.txt @@ -0,0 +1,19 @@ +LICENSE.txt +README.md +pyproject.toml +setup.cfg +src/internationalize/__init__.py +src/internationalize/command_line.py +src/internationalize/globals.py +src/internationalize/helpers.py +src/internationalize/internationalize.py +src/internationalize/localize.py +src/localization.egg-info/PKG-INFO +src/localization.egg-info/SOURCES.txt +src/localization.egg-info/dependency_links.txt +src/localization.egg-info/top_level.txt +tests/test.py +tests/test_cli.py +tests/test_generate_file.py +tests/test_parse_json.py +tests/test_read_file.py \ No newline at end of file diff --git a/i18nilize/src/localization.egg-info/dependency_links.txt b/i18nilize/src/localization.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/i18nilize/src/localization.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/i18nilize/src/localization.egg-info/top_level.txt b/i18nilize/src/localization.egg-info/top_level.txt new file mode 100644 index 0000000..1d2fe09 --- /dev/null +++ b/i18nilize/src/localization.egg-info/top_level.txt @@ -0,0 +1 @@ +internationalize diff --git a/i18nilize/tests/test_error_handler.py b/i18nilize/tests/test_error_handler.py new file mode 100644 index 0000000..59d5ecf --- /dev/null +++ b/i18nilize/tests/test_error_handler.py @@ -0,0 +1,39 @@ +import unittest +from src.internationalize.error_handler import ErrorHandler + +class TestErrorHandler(unittest.TestCase): + def test_valid_keys(self): + handler = ErrorHandler({}) + language = { + "hello": "bonjour", + "thanks": "merci" + } + self.assertEqual(handler.handle_invalid_keys(language), "") + + def test_non_string_keys(self): + handler = ErrorHandler({}) + language = { + 123: "bonjour", + None: "merci" + } + self.assertEqual(handler.handle_invalid_keys(language), "Key '123' is not a string.") + + def test_empty_or_whitespace_keys(self): + handler = ErrorHandler({}) + language = { + "": "bonjour", + " ": "merci" + } + self.assertEqual(handler.handle_invalid_keys(language), "Key is empty or only contains whitespace.") + + def test_combined_issues(self): + handler = TranslationHandler({}) + language = { + 123: "bonjour", + " ": "merci", + "hello-world": "salut" + } + self.assertEqual(handler.handle_invalid_keys(language), "Key '123' is not a string.") + +if __name__ == "__main__": + unittest.main() From 961d2f673b2b4e58813c0fa1e949f943edfc9ef0 Mon Sep 17 00:00:00 2001 From: Brian Park Date: Thu, 28 Nov 2024 15:11:12 -0800 Subject: [PATCH 3/6] fixed tests --- i18nilize/tests/test_error_handler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/i18nilize/tests/test_error_handler.py b/i18nilize/tests/test_error_handler.py index 59d5ecf..c5be551 100644 --- a/i18nilize/tests/test_error_handler.py +++ b/i18nilize/tests/test_error_handler.py @@ -16,7 +16,7 @@ def test_non_string_keys(self): 123: "bonjour", None: "merci" } - self.assertEqual(handler.handle_invalid_keys(language), "Key '123' is not a string.") + self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.") def test_empty_or_whitespace_keys(self): handler = ErrorHandler({}) @@ -24,16 +24,16 @@ def test_empty_or_whitespace_keys(self): "": "bonjour", " ": "merci" } - self.assertEqual(handler.handle_invalid_keys(language), "Key is empty or only contains whitespace.") + self.assertEqual(handler.handle_invalid_keys(language), "Key is empty.") def test_combined_issues(self): - handler = TranslationHandler({}) + handler = ErrorHandler({}) language = { 123: "bonjour", " ": "merci", "hello-world": "salut" } - self.assertEqual(handler.handle_invalid_keys(language), "Key '123' is not a string.") + self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.") if __name__ == "__main__": unittest.main() From 65e513f84e58d17ed9ce8f29e851e94d271a13a1 Mon Sep 17 00:00:00 2001 From: Alex Luo <60013046+AlexLuo602@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:43:10 -0800 Subject: [PATCH 4/6] Finished Error handling class and tests --- .../src/internationalize/error_handler.py | 51 ++++++++---- .../resources/error_handling/expected.json | 6 ++ .../translations/combined_issues.json | 5 ++ .../translations/empty_keys.json | 4 + .../translations/invalid_file.json | 4 + .../translations/non_string_values.json | 4 + .../translations/valid_keys.json | 4 + i18nilize/tests/test_error_handler.py | 81 ++++++++++++------- 8 files changed, 116 insertions(+), 43 deletions(-) create mode 100644 i18nilize/tests/resources/error_handling/expected.json create mode 100644 i18nilize/tests/resources/error_handling/translations/combined_issues.json create mode 100644 i18nilize/tests/resources/error_handling/translations/empty_keys.json create mode 100644 i18nilize/tests/resources/error_handling/translations/invalid_file.json create mode 100644 i18nilize/tests/resources/error_handling/translations/non_string_values.json create mode 100644 i18nilize/tests/resources/error_handling/translations/valid_keys.json diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py index d5eb80c..cd4afd9 100644 --- a/i18nilize/src/internationalize/error_handler.py +++ b/i18nilize/src/internationalize/error_handler.py @@ -1,3 +1,4 @@ +import json import os """ @@ -8,7 +9,7 @@ class ErrorHandler(): Sets up the directory where the languages are located """ def __init__(self, translations_dir): - self.translations_dif = translations_dir + self.translations_dir = translations_dir """ Verify each language file is valid @@ -20,12 +21,12 @@ def verify_languages(self): """ errors = {} - all_languages = os.listdir(self.translations_dif) + all_language_files = os.listdir(self.translations_dir) - for language in all_languages: - error = self.handle_error(language, False) + for language_file in all_language_files: + error = self.handle_error(language_file) if error != "": - errors[language] = error + errors[language_file] = error return errors @@ -35,19 +36,19 @@ def verify_languages(self): Input: string: language_file (source of error), boolean: error_expected (is the error expected?)\ Output: descriptive string about the error from the language file """ - def handle_error(self, language, error_expected): + def handle_error(self, language_file, error_expected=False): result = "" # Verify if file is invalid - result = self.handle_invalid_file(language) + result = self.handle_invalid_file(language_file) if result != "": return result # Verify if any keys are invalid - result = self.handle_invalid_keys(language) + result = self.handle_invalid_keys(language_file) if result == "" and error_expected: - raise Exception(f"expected error in {language} but no error was found") + raise Exception(f"expected error in {language_file} but no error was found") return result @@ -57,7 +58,17 @@ def handle_error(self, language, error_expected): - An empty string if there isn't any errors - A descriptive message about the error """ - def handle_invalid_file(self, language): + def handle_invalid_file(self, language_file): + language_location = os.path.join(self.translations_dir, language_file) + try: + with open(language_location, "r") as file: + json.load(file) + except json.JSONDecodeError as e: + return "Invalid Language File, try fixing the json format." + except Exception as e: + print(f"Unexpected Error: {e}") + raise e + # return empty string if no error found return "" """ @@ -66,10 +77,18 @@ def handle_invalid_file(self, language): - An empty string if there aren't any errors - A descriptive message about the invalid key(s) """ - def handle_invalid_keys(self, language): - for key in language.keys(): - if not isinstance(key,str): - return "Key is not a string." - if not key.strip(): - return "Key is empty." + def handle_invalid_keys(self, language_file): + language_location = os.path.join(self.translations_dir, language_file) + language = {} + try: + with open(language_location, "r") as file: + language = json.load(file) + for key in language: + if not isinstance(language[key], str): + return "Value is not a string." + if not key.strip(): + return "Key is empty." + except Exception as e: + print(f"Unexpected Error: {e}") + raise e return "" \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/expected.json b/i18nilize/tests/resources/error_handling/expected.json new file mode 100644 index 0000000..62284af --- /dev/null +++ b/i18nilize/tests/resources/error_handling/expected.json @@ -0,0 +1,6 @@ +{ + "combined_issues.json": "Value is not a string.", + "empty_keys.json": "Key is empty.", + "invalid_file.json": "Invalid Language File, try fixing the json format.", + "non_string_values.json": "Value is not a string." +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/combined_issues.json b/i18nilize/tests/resources/error_handling/translations/combined_issues.json new file mode 100644 index 0000000..e5cdc9b --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/combined_issues.json @@ -0,0 +1,5 @@ +{ + "Hi": 123, + " ": "merci", + "hello-world": "salut" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/empty_keys.json b/i18nilize/tests/resources/error_handling/translations/empty_keys.json new file mode 100644 index 0000000..800bf3a --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/empty_keys.json @@ -0,0 +1,4 @@ +{ + "": "bonjour", + " ": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/invalid_file.json b/i18nilize/tests/resources/error_handling/translations/invalid_file.json new file mode 100644 index 0000000..fd230e1 --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/invalid_file.json @@ -0,0 +1,4 @@ +{ + "hello": "bonjour" + "thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/non_string_values.json b/i18nilize/tests/resources/error_handling/translations/non_string_values.json new file mode 100644 index 0000000..962915c --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/non_string_values.json @@ -0,0 +1,4 @@ +{ + "Hi": 123, + "Thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/valid_keys.json b/i18nilize/tests/resources/error_handling/translations/valid_keys.json new file mode 100644 index 0000000..6d51899 --- /dev/null +++ b/i18nilize/tests/resources/error_handling/translations/valid_keys.json @@ -0,0 +1,4 @@ +{ + "hello": "bonjour", + "thanks": "merci" +} \ No newline at end of file diff --git a/i18nilize/tests/test_error_handler.py b/i18nilize/tests/test_error_handler.py index c5be551..919ac2f 100644 --- a/i18nilize/tests/test_error_handler.py +++ b/i18nilize/tests/test_error_handler.py @@ -1,39 +1,66 @@ +import json +import os import unittest from src.internationalize.error_handler import ErrorHandler class TestErrorHandler(unittest.TestCase): + def setUp(self): + self.test_folder = os.path.join("tests", "resources", "error_handling") + self.translations_folder = os.path.join(self.test_folder, "translations") + self.handler = ErrorHandler(self.translations_folder) + + # ================== Test Keys ================================ def test_valid_keys(self): - handler = ErrorHandler({}) - language = { - "hello": "bonjour", - "thanks": "merci" - } - self.assertEqual(handler.handle_invalid_keys(language), "") - - def test_non_string_keys(self): - handler = ErrorHandler({}) - language = { - 123: "bonjour", - None: "merci" - } - self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.") + language = "valid_keys.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "") + + def test_non_string_values(self): + language = "non_string_values.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") def test_empty_or_whitespace_keys(self): - handler = ErrorHandler({}) - language = { - "": "bonjour", - " ": "merci" - } - self.assertEqual(handler.handle_invalid_keys(language), "Key is empty.") + language = "empty_keys.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty.") def test_combined_issues(self): - handler = ErrorHandler({}) - language = { - 123: "bonjour", - " ": "merci", - "hello-world": "salut" - } - self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.") + language = "combined_issues.json" + self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") + + # ================== Test Invalid Files ================================ + def test_invalid_file(self): + language = "invalid_file.json" + self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") + + def test_invalid_file(self): + language = "invalid_file.json" + self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") + + # ================== Test Error Handler ================================ + def test_invalid_file(self): + language = "invalid_file.json" + self.assertEqual(self.handler.handle_error(language), "Invalid Language File, try fixing the json format.") + + def test_non_string_values(self): + language = "non_string_values.json" + self.assertEqual(self.handler.handle_error(language), "Value is not a string.") + + def test_valid_expected(self): + language = "valid_keys.json" + self.assertEqual(self.handler.handle_error(language), "") + + def test_valid_unexpected(self): + language = "valid_keys.json" + with self.assertRaises(Exception) as context: + self.handler.handle_error(language, True) + + self.assertEqual(str(context.exception), "expected error in valid_keys.json but no error was found") + + # ================== Test Folder ================================ + def test_invalid_file(self): + with open(os.path.join(self.test_folder, "expected.json"), "r") as file: + expected = json.load(file) + + self.assertEqual(self.handler.verify_languages(), expected) if __name__ == "__main__": unittest.main() From b44fb4282017ddd23f2eebe090d3b1c0ef4faa53 Mon Sep 17 00:00:00 2001 From: Brian Park Date: Wed, 4 Dec 2024 03:13:28 -0800 Subject: [PATCH 5/6] removed the error expected set, incorporated error handling with cli --- .../src/internationalize/diffing_processor.py | 9 ++++ .../src/internationalize/error_handler.py | 32 ++++++------- i18nilize/src/internationalize/helpers.py | 47 +++++++++++++++++-- .../translations/empty_keys.json | 2 +- src/internationalize/languages/croatian.json | 4 ++ 5 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/internationalize/languages/croatian.json diff --git a/i18nilize/src/internationalize/diffing_processor.py b/i18nilize/src/internationalize/diffing_processor.py index f024ff7..73cd3c6 100644 --- a/i18nilize/src/internationalize/diffing_processor.py +++ b/i18nilize/src/internationalize/diffing_processor.py @@ -2,7 +2,9 @@ import hashlib import json from dirsync import sync +from . import globals from src.internationalize.helpers import compute_hash, compute_hashes, read_json_file +from src.internationalize.error_handler import ErrorHandler JSON_EXTENSION = ".json" @@ -57,6 +59,13 @@ def update_metadata(self, hash_dict): json.dump(hash_dict, outfile) def sync_translations(self): + handler = ErrorHandler(globals.LANGUAGES_DIR) + errors = handler.verify_languages() + if errors: + print("Errors detected in language files:") + for file, error in errors.items(): + print(f"{file}: {error}") + return sync(self.curr_translation_files_dir, self.diff_state_files_dir, "sync", purge=True) """ diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py index cd4afd9..6170a1e 100644 --- a/i18nilize/src/internationalize/error_handler.py +++ b/i18nilize/src/internationalize/error_handler.py @@ -37,20 +37,17 @@ def verify_languages(self): Output: descriptive string about the error from the language file """ def handle_error(self, language_file, error_expected=False): - result = "" - + invalid_file_result = "" # Verify if file is invalid - result = self.handle_invalid_file(language_file) - if result != "": - return result - + invalid_file_result = self.handle_invalid_file(language_file) + print(invalid_file_result) + if invalid_file_result != "": + return invalid_file_result # Verify if any keys are invalid - result = self.handle_invalid_keys(language_file) - - if result == "" and error_expected: - raise Exception(f"expected error in {language_file} but no error was found") - - return result + invalid_keys_result = self.handle_invalid_keys(language_file) + if invalid_keys_result != "": + return invalid_keys_result + return "" """ Checks if given language is in an invalid file @@ -82,13 +79,14 @@ def handle_invalid_keys(self, language_file): language = {} try: with open(language_location, "r") as file: - language = json.load(file) + language = json.load(file) for key in language: + stripped_key = key.strip() + if stripped_key == "": + return "Key is empty or contains only whitespace." if not isinstance(language[key], str): - return "Value is not a string." - if not key.strip(): - return "Key is empty." + return f"Value for key '{key}' is not a string." except Exception as e: print(f"Unexpected Error: {e}") raise e - return "" \ No newline at end of file + return "" diff --git a/i18nilize/src/internationalize/helpers.py b/i18nilize/src/internationalize/helpers.py index e95cabb..a5c399f 100644 --- a/i18nilize/src/internationalize/helpers.py +++ b/i18nilize/src/internationalize/helpers.py @@ -4,6 +4,7 @@ import hashlib import requests from . import globals +from internationalize.error_handler import ErrorHandler # Function to parse json file, given its path def get_json(file_path): @@ -38,13 +39,30 @@ def add_language(language): # Adds/updates a translated word under the given language in the default JSON file def add_update_translated_word(language, original_word, translated_word): file_path = os.path.join(globals.LANGUAGES_DIR, f"{language.lower()}.json") - + handler = ErrorHandler(globals.LANGUAGES_DIR) + if not os.path.exists(file_path): print(f"Error: Language '{language}' does not exist. Add the language before adding a translation.") sys.exit(1) + if not original_word.strip(): + print("Error: Original word cannot be empty or contain only whitespace.") + sys.exit(1) + if not translated_word.strip(): + print("Error: Translated word cannot be empty or contain only whitespace.") + sys.exit(1) + try: + data = get_json(file_path) + except json.JSONDecodeError as e: + result = handler.handle_error(f"{language.lower()}.json", True) + sys.exit(1) - data = get_json(file_path) - + result = handler.handle_error(f"{language.lower()}.json", True) + if (result == "Key is empty or contains only whitespace."): + print(result) + sys.exit(1) + elif (result != ""): + print(result) + sys.exit(1) data[original_word] = translated_word with open(file_path, 'w') as file: json.dump(data, file, indent=4) @@ -53,12 +71,31 @@ def add_update_translated_word(language, original_word, translated_word): # Deletes a translated word for the given language def delete_translation(language, original_word, translated_word): file_path = os.path.join(globals.LANGUAGES_DIR, f"{language.lower()}.json") - + handler = ErrorHandler(globals.LANGUAGES_DIR) if not os.path.exists(file_path): print(f"Error: Language '{language}' does not exist.") sys.exit(1) + try: + data = get_json(file_path) + except json.JSONDecodeError as e: + result = handler.handle_error(f"{language.lower()}.json", True) + sys.exit(1) + + result = handler.handle_error(f"{language.lower()}.json", True) + if (result == "Key is empty or contains only whitespace."): + print(result) + sys.exit(1) + elif (result != ""): + print(result) + sys.exit(1) - data = get_json(file_path) + if not original_word.strip(): + print("Error: Original word cannot be empty or contain only whitespace.") + sys.exit(1) + + if not translated_word.strip(): + print("Error: Translated word cannot be empty or contain only whitespace.") + sys.exit(1) if original_word not in data: print(f"Error: Original word '{original_word}' does not exist in language '{language}'.") diff --git a/i18nilize/tests/resources/error_handling/translations/empty_keys.json b/i18nilize/tests/resources/error_handling/translations/empty_keys.json index 800bf3a..cf75fdd 100644 --- a/i18nilize/tests/resources/error_handling/translations/empty_keys.json +++ b/i18nilize/tests/resources/error_handling/translations/empty_keys.json @@ -1,4 +1,4 @@ { - "": "bonjour", + "": 123", " ": "merci" } \ No newline at end of file diff --git a/src/internationalize/languages/croatian.json b/src/internationalize/languages/croatian.json new file mode 100644 index 0000000..a67aec3 --- /dev/null +++ b/src/internationalize/languages/croatian.json @@ -0,0 +1,4 @@ +{ + "as": "hello", + "asdd": "as" +} \ No newline at end of file From 4284acdc097102a27bc15f6d95b57cf66a4057b4 Mon Sep 17 00:00:00 2001 From: Brian Park Date: Wed, 4 Dec 2024 15:42:01 -0800 Subject: [PATCH 6/6] fixed tests --- i18nilize/src/internationalize/error_handler.py | 4 ++-- .../resources/error_handling/expected.json | 6 +++--- .../error_handling/translations/empty_keys.json | 2 +- i18nilize/tests/test_error_handler.py | 17 ++++------------- src/internationalize/languages/croatian.json | 4 ++-- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/i18nilize/src/internationalize/error_handler.py b/i18nilize/src/internationalize/error_handler.py index 6170a1e..2abbbb9 100644 --- a/i18nilize/src/internationalize/error_handler.py +++ b/i18nilize/src/internationalize/error_handler.py @@ -40,7 +40,7 @@ def handle_error(self, language_file, error_expected=False): invalid_file_result = "" # Verify if file is invalid invalid_file_result = self.handle_invalid_file(language_file) - print(invalid_file_result) + # print(invalid_file_result) if invalid_file_result != "": return invalid_file_result # Verify if any keys are invalid @@ -85,7 +85,7 @@ def handle_invalid_keys(self, language_file): if stripped_key == "": return "Key is empty or contains only whitespace." if not isinstance(language[key], str): - return f"Value for key '{key}' is not a string." + return f"Value is not a string." except Exception as e: print(f"Unexpected Error: {e}") raise e diff --git a/i18nilize/tests/resources/error_handling/expected.json b/i18nilize/tests/resources/error_handling/expected.json index 62284af..1d2fe3f 100644 --- a/i18nilize/tests/resources/error_handling/expected.json +++ b/i18nilize/tests/resources/error_handling/expected.json @@ -1,6 +1,6 @@ { - "combined_issues.json": "Value is not a string.", - "empty_keys.json": "Key is empty.", "invalid_file.json": "Invalid Language File, try fixing the json format.", - "non_string_values.json": "Value is not a string." + "empty_keys.json": "Key is empty or contains only whitespace.", + "non_string_values.json": "Value is not a string.", + "combined_issues.json": "Value is not a string." } \ No newline at end of file diff --git a/i18nilize/tests/resources/error_handling/translations/empty_keys.json b/i18nilize/tests/resources/error_handling/translations/empty_keys.json index cf75fdd..bdcca93 100644 --- a/i18nilize/tests/resources/error_handling/translations/empty_keys.json +++ b/i18nilize/tests/resources/error_handling/translations/empty_keys.json @@ -1,4 +1,4 @@ { - "": 123", + "": "as", " ": "merci" } \ No newline at end of file diff --git a/i18nilize/tests/test_error_handler.py b/i18nilize/tests/test_error_handler.py index 919ac2f..1c11cc6 100644 --- a/i18nilize/tests/test_error_handler.py +++ b/i18nilize/tests/test_error_handler.py @@ -20,23 +20,21 @@ def test_non_string_values(self): def test_empty_or_whitespace_keys(self): language = "empty_keys.json" - self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty.") + self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty or contains only whitespace.") def test_combined_issues(self): language = "combined_issues.json" self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") # ================== Test Invalid Files ================================ - def test_invalid_file(self): - language = "invalid_file.json" - self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") - def test_invalid_file(self): + def test_invalid_file_individual(self): language = "invalid_file.json" self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") # ================== Test Error Handler ================================ def test_invalid_file(self): + print("invalid File Test: ") language = "invalid_file.json" self.assertEqual(self.handler.handle_error(language), "Invalid Language File, try fixing the json format.") @@ -48,15 +46,8 @@ def test_valid_expected(self): language = "valid_keys.json" self.assertEqual(self.handler.handle_error(language), "") - def test_valid_unexpected(self): - language = "valid_keys.json" - with self.assertRaises(Exception) as context: - self.handler.handle_error(language, True) - - self.assertEqual(str(context.exception), "expected error in valid_keys.json but no error was found") - # ================== Test Folder ================================ - def test_invalid_file(self): + def test_invalid_folder(self): with open(os.path.join(self.test_folder, "expected.json"), "r") as file: expected = json.load(file) diff --git a/src/internationalize/languages/croatian.json b/src/internationalize/languages/croatian.json index a67aec3..fd230e1 100644 --- a/src/internationalize/languages/croatian.json +++ b/src/internationalize/languages/croatian.json @@ -1,4 +1,4 @@ { - "as": "hello", - "asdd": "as" + "hello": "bonjour" + "thanks": "merci" } \ No newline at end of file