Skip to content

Commit

Permalink
Changed formatting of diff results
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLuo602 committed Nov 15, 2024
1 parent 0ae27c8 commit 40f8f39
Showing 6 changed files with 93 additions and 20 deletions.
61 changes: 46 additions & 15 deletions i18nilize/src/internationalize/diffing_processor.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

JSON_EXTENSION = ".json"

TYPE = "type"
CREATED = "created"
MODIFIED = "modified"
DELETED = "deleted"
@@ -75,17 +76,25 @@ def get_changed_files(self):
with open(self.metadata_file_dir, "r") as file:
original_hashes = json.load(file)

changed_files = []
changed_files = {
CREATED: [],
MODIFIED: [],
DELETED: []
}

# Find any languages that were either modified or added the current PIP package
for language, current_hash in current_hashes.items():
if language not in original_hashes or original_hashes[language] != current_hash:
changed_files.append(language + JSON_EXTENSION)
file_name = language + JSON_EXTENSION
if language not in original_hashes:
changed_files[CREATED].append(file_name)
elif original_hashes[language] != current_hash:
changed_files[MODIFIED].append(file_name)

# Find files that were removed from PIP package
for language in original_hashes:
file_name = language + JSON_EXTENSION
if language not in current_hashes:
changed_files.append(language + JSON_EXTENSION)
changed_files[DELETED].append(file_name)

return changed_files

@@ -96,11 +105,17 @@ def get_changed_translations(self):
changed_files = self.get_changed_files()
changed_translations = {}

# Perform diffing on files that changed and got added, modified, deleted
for file_name in changed_files:
language = file_name.split(".")[0]
changed_translations[language] = self.compare_language(file_name)
for type, file_names in changed_files.items():
for file_name in file_names:
language = file_name.split(".")[0]
changed_translations[language] = self.__initialize_changed_template(type)

# fetch modified translations
if type == MODIFIED:
changed_translations[language] = self.compare_language(file_name, changed_translations[language])

if type == CREATED:
changed_translations[language] = self.add_language(file_name, changed_translations[language])

"""
commented out updating metadata in this section for now
@@ -113,18 +128,13 @@ def get_changed_translations(self):
"""
Gets differences between old and new translations for one language
"""
def compare_language(self, file_name):
def compare_language(self, file_name, changed_translations):
original_language_location = self.diff_state_files_dir + "\\" + file_name
current_language_location = self.curr_translation_files_dir + "\\" + file_name

original_language = read_language(original_language_location)
current_language = read_language(current_language_location)

changed_translations = {}
changed_translations[CREATED] = {}
changed_translations[MODIFIED] = {}
changed_translations[DELETED] = {}

# find modified and newly added translations
for word, translation in current_language.items():
if word not in original_language:
@@ -138,7 +148,26 @@ def compare_language(self, file_name):
changed_translations[DELETED][word] = translation

return changed_translations

def add_language(self, file_name, changed_translations):
current_language_location = self.curr_translation_files_dir + "\\" + file_name
current_language = read_language(current_language_location)

for word, translation in current_language.items():
changed_translations[CREATED][word] = translation

return changed_translations

"""
Create empty JSON template to show modifications from a language
"""
def __initialize_changed_template(self, type):
changed_translations = {}
changed_translations[TYPE] = type
changed_translations[CREATED] = {}
changed_translations[MODIFIED] = {}
changed_translations[DELETED] = {}
return changed_translations


"""
@@ -177,4 +206,6 @@ def read_language(directory):
raise
except IOError:
print(f"An error occurred while trying to read the file: {directory}")
raise
raise
except Exception as e:
print(f"An exception occured: {e}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "ni hao",
"welcome": "huan yin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hello": "ola",
"thanks": "obrigado",
"welcome": "Bem-vindo"
}
33 changes: 28 additions & 5 deletions i18nilize/tests/test_diffing.py
Original file line number Diff line number Diff line change
@@ -46,16 +46,23 @@ def test_initialization(self):


def test_find_changed_files_basic(self):
self.util.bulk_modify_test_data(self.basic_modified_data_location)
expected_changed_files = ["italian.json", "spanish.json"]
self.util.initialize_test_data(self.basic_modified_data_location)
expected_changed_files = {
"modified": ["italian.json", "spanish.json"],
"created": ["mandarin.json"],
"deleted": ["french.json"]
}
changed_files = self.dp.get_changed_files()
self.assertListEqual(changed_files, expected_changed_files)

for type, languages in changed_files.items():
self.assertListEqual(languages, expected_changed_files[type])


def test_find_changed_translations_basic(self):
self.util.bulk_modify_test_data(self.basic_modified_data_location)
expected_changed_translations = {
self.util.initialize_test_data(self.basic_modified_data_location)
expected_changed_translations = expected_changed_translations = {
"italian": {
"type": "modified",
"created": {},
"modified": {
"thanks": "La ringrazio"
@@ -65,13 +72,29 @@ def test_find_changed_translations_basic(self):
}
},
"spanish": {
"type": "modified",
"created": {
"welcome": "bienvenido"
},
"modified": {
"hello": "holi"
},
"deleted": {}
},
"french": {
"type": "deleted",
"created": {},
"modified": {},
"deleted": {}
},
"mandarin": {
"type": "created",
"created": {
"hello": "ni hao",
"welcome": "huan yin"
},
"modified": {},
"deleted": {}
}
}

0 comments on commit 40f8f39

Please sign in to comment.