Skip to content

Commit

Permalink
Merge pull request #17 from ubclaunchpad/error_handling
Browse files Browse the repository at this point in the history
Graceful Error Handling.
  • Loading branch information
kevinrczhang authored Dec 6, 2024
2 parents b8a7a86 + 4284acd commit 64b4472
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 5 deletions.
9 changes: 9 additions & 0 deletions i18nilize/src/internationalize/diffing_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)

"""
Expand Down
92 changes: 92 additions & 0 deletions i18nilize/src/internationalize/error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import json
import os

"""
Error Handler Class
"""
class ErrorHandler():
"""
Sets up the directory where the languages are located
"""
def __init__(self, translations_dir):
self.translations_dir = translations_dir

"""
Verify each language file is valid
"""
def verify_languages(self):
"""
key: language containing an error
value: error message
"""
errors = {}

all_language_files = os.listdir(self.translations_dir)

for language_file in all_language_files:
error = self.handle_error(language_file)
if error != "":
errors[language_file] = 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_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)
if invalid_file_result != "":
return invalid_file_result
# Verify if any keys are invalid
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
Output:
- An empty string if there isn't any errors
- A descriptive message about the error
"""
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 ""

"""
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_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:
stripped_key = key.strip()
if stripped_key == "":
return "Key is empty or contains only whitespace."
if not isinstance(language[key], str):
return f"Value is not a string."
except Exception as e:
print(f"Unexpected Error: {e}")
raise e
return ""
47 changes: 42 additions & 5 deletions i18nilize/src/internationalize/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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}'.")
Expand Down
1 change: 1 addition & 0 deletions i18nilize/src/internationalize/languages/turkish.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions i18nilize/src/internationalize/languages/urdu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
25 changes: 25 additions & 0 deletions i18nilize/src/localization.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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.
19 changes: 19 additions & 0 deletions i18nilize/src/localization.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions i18nilize/src/localization.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions i18nilize/src/localization.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
internationalize
6 changes: 6 additions & 0 deletions i18nilize/tests/resources/error_handling/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"invalid_file.json": "Invalid Language File, try fixing the json format.",
"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."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Hi": 123,
" ": "merci",
"hello-world": "salut"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"": "as",
" ": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour"
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Hi": 123,
"Thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
57 changes: 57 additions & 0 deletions i18nilize/tests/test_error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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):
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):
language = "empty_keys.json"
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_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.")

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), "")

# ================== Test Folder ================================
def test_invalid_folder(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()
4 changes: 4 additions & 0 deletions src/internationalize/languages/croatian.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour"
"thanks": "merci"
}

0 comments on commit 64b4472

Please sign in to comment.