Skip to content

Commit

Permalink
removed the error expected set, incorporated error handling with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Park committed Dec 4, 2024
1 parent 65e513f commit b44fb42
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 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
32 changes: 15 additions & 17 deletions i18nilize/src/internationalize/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"": "bonjour",
"": 123",
" ": "merci"
}
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 @@
{
"as": "hello",
"asdd": "as"
}

0 comments on commit b44fb42

Please sign in to comment.