diff --git a/i18nilize/src/internationalize/diffing_processor.py b/i18nilize/src/internationalize/diffing_processor.py index be7ce83..449e67b 100644 --- a/i18nilize/src/internationalize/diffing_processor.py +++ b/i18nilize/src/internationalize/diffing_processor.py @@ -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 \ No newline at end of file + raise + except Exception as e: + print(f"An exception occured: {e}") \ No newline at end of file diff --git a/i18nilize/tests/resources/diffing_algorithm/basic_initial_translations/portugese.json b/i18nilize/tests/resources/diffing_algorithm/basic_initial_translations/portugese.json new file mode 100644 index 0000000..ef5f8db --- /dev/null +++ b/i18nilize/tests/resources/diffing_algorithm/basic_initial_translations/portugese.json @@ -0,0 +1,5 @@ +{ + "hello": "ola", + "thanks": "obrigado", + "welcome": "Bem-vindo" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/mandarin.json b/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/mandarin.json new file mode 100644 index 0000000..9d5fa6d --- /dev/null +++ b/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/mandarin.json @@ -0,0 +1,4 @@ +{ + "hello": "ni hao", + "welcome": "huan yin" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/portugese.json b/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/portugese.json new file mode 100644 index 0000000..ef5f8db --- /dev/null +++ b/i18nilize/tests/resources/diffing_algorithm/basic_modified_translations/portugese.json @@ -0,0 +1,5 @@ +{ + "hello": "ola", + "thanks": "obrigado", + "welcome": "Bem-vindo" +} \ No newline at end of file diff --git a/i18nilize/tests/resources/diffing_algorithm/test_translations/portugese.json b/i18nilize/tests/resources/diffing_algorithm/test_translations/portugese.json new file mode 100644 index 0000000..5a903ee --- /dev/null +++ b/i18nilize/tests/resources/diffing_algorithm/test_translations/portugese.json @@ -0,0 +1,5 @@ +{ + "hello": "ola", + "thanks": "obrigado", + "welcome": "Bem-vindo" +} \ No newline at end of file diff --git a/i18nilize/tests/test_diffing.py b/i18nilize/tests/test_diffing.py index 9afc9e3..d539b46 100644 --- a/i18nilize/tests/test_diffing.py +++ b/i18nilize/tests/test_diffing.py @@ -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,6 +72,7 @@ def test_find_changed_translations_basic(self): } }, "spanish": { + "type": "modified", "created": { "welcome": "bienvenido" }, @@ -72,6 +80,21 @@ def test_find_changed_translations_basic(self): "hello": "holi" }, "deleted": {} + }, + "french": { + "type": "deleted", + "created": {}, + "modified": {}, + "deleted": {} + }, + "mandarin": { + "type": "created", + "created": { + "hello": "ni hao", + "welcome": "huan yin" + }, + "modified": {}, + "deleted": {} } }