-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ubclaunchpad/diffing_algorithm
Diffing Algorithm
- Loading branch information
Showing
61 changed files
with
634 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dirsync==2.2.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file removed
BIN
-173 Bytes
i18nilize/src/internationalize/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file removed
BIN
-214 Bytes
i18nilize/src/internationalize/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file removed
BIN
-180 Bytes
i18nilize/src/internationalize/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file removed
BIN
-1.04 KB
i18nilize/src/internationalize/__pycache__/generateFile.cpython-311.pyc
Binary file not shown.
Binary file removed
BIN
-850 Bytes
i18nilize/src/internationalize/__pycache__/generate_file.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-181 Bytes
i18nilize/src/internationalize/__pycache__/internationalize.cpython-310.pyc
Binary file not shown.
Binary file removed
BIN
-710 Bytes
i18nilize/src/internationalize/__pycache__/localize.cpython-312.pyc
Binary file not shown.
Binary file removed
BIN
-393 Bytes
i18nilize/src/internationalize/__pycache__/parser.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-1.77 KB
i18nilize/src/internationalize/__pycache__/test_parse_json.cpython-310.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import os | ||
import hashlib | ||
import json | ||
from dirsync import sync | ||
from src.internationalize.helpers import compute_hash, compute_hashes, read_json_file | ||
|
||
JSON_EXTENSION = ".json" | ||
|
||
TYPE = "type" | ||
CREATED = "created" | ||
MODIFIED = "modified" | ||
DELETED = "deleted" | ||
|
||
""" | ||
Diffing Processor Class | ||
""" | ||
class DiffingProcessor(): | ||
def __init__(self, curr_translations_dir): | ||
self.diff_state_root_dir = "diff_state" | ||
self.diff_state_files_dir = os.path.join(self.diff_state_root_dir, "translations") | ||
self.metadata_file_dir = os.path.join(self.diff_state_root_dir, "metadata.json") | ||
self.curr_translation_files_dir = curr_translations_dir | ||
|
||
""" | ||
Initializes the old state of translations when package is first installed. | ||
""" | ||
def setup(self): | ||
try: | ||
os.mkdir(self.diff_state_root_dir) | ||
os.mkdir(self.diff_state_files_dir) | ||
with open(self.metadata_file_dir, "w") as outfile: | ||
json.dump({}, outfile) | ||
|
||
# sync folders | ||
self.sync_translations() | ||
|
||
# Compute all file hashes and store hashes in metadata | ||
all_files = os.listdir(self.diff_state_files_dir) | ||
all_file_hashes = compute_hashes(self.diff_state_files_dir) | ||
self.update_metadata(all_file_hashes) | ||
except FileExistsError: | ||
print(f"Old translations directory has already been created.") | ||
except PermissionError: | ||
print(f"Permission denied: unable to setup old translation state.") | ||
except Exception as e: | ||
print(f"An exception occured: {e}") | ||
|
||
""" | ||
Updates translation files with new changes and updates hashes in metadata. | ||
""" | ||
def update_to_current_state(self, hash_dict): | ||
self.update_metadata(hash_dict) | ||
self.sync_translations() | ||
|
||
def update_metadata(self, hash_dict): | ||
with open(self.metadata_file_dir, "w") as outfile: | ||
json.dump(hash_dict, outfile) | ||
|
||
def sync_translations(self): | ||
sync(self.curr_translation_files_dir, self.diff_state_files_dir, "sync", purge=True) | ||
|
||
""" | ||
Returns a list of all the files that have been modified | ||
""" | ||
def get_changed_files(self): | ||
# Initialize hashes | ||
current_hashes = compute_hashes(self.curr_translation_files_dir) | ||
|
||
with open(self.metadata_file_dir, "r") as file: | ||
original_hashes = json.load(file) | ||
|
||
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(): | ||
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[DELETED].append(file_name) | ||
|
||
return changed_files | ||
|
||
""" | ||
Gets differences between old and new translations | ||
""" | ||
def get_changed_translations(self): | ||
changed_files = self.get_changed_files() | ||
changed_translations = {} | ||
|
||
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]) | ||
|
||
return changed_translations | ||
|
||
""" | ||
Gets differences between old and new translations for one language | ||
""" | ||
def compare_language(self, file_name, changed_translations): | ||
original_language_location = os.path.join(self.diff_state_files_dir, file_name) | ||
current_language_location = os.path.join(self.curr_translation_files_dir, file_name) | ||
|
||
original_language = read_json_file(original_language_location) | ||
current_language = read_json_file(current_language_location) | ||
|
||
# find modified and newly added translations | ||
for word, translation in current_language.items(): | ||
if word not in original_language: | ||
changed_translations[CREATED][word] = translation | ||
elif translation != original_language[word]: | ||
changed_translations[MODIFIED][word] = translation | ||
|
||
# find removed translations | ||
for word, translation in original_language.items(): | ||
if word not in current_language: | ||
changed_translations[DELETED][word] = translation | ||
|
||
return changed_translations | ||
|
||
def add_language(self, file_name, changed_translations): | ||
current_language_location = os.path.join(self.curr_translation_files_dir, file_name) | ||
current_language = read_json_file(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"thank you": "\u8c22\u8c22" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"thank you": "danke" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
{} | ||
{ | ||
"welcome": "\ud658\uc601\ud569\ub2c8\ub2e4" | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
40 changes: 40 additions & 0 deletions
40
i18nilize/tests/resources/diffing_algorithm/all_tests/all_added/expected_output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"italian": { | ||
"type": "created", | ||
"created": { | ||
"hello": "bonjourno", | ||
"thanks": "grazie", | ||
"welcome": "benvenuto" | ||
}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"spanish": { | ||
"type": "created", | ||
"created": { | ||
"hello": "hola", | ||
"thanks": "gracias" | ||
}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"french": { | ||
"type": "created", | ||
"created": { | ||
"hello": "bonjour", | ||
"thanks": "merci" | ||
}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"portugese": { | ||
"type": "created", | ||
"created": { | ||
"hello": "ola", | ||
"thanks": "obrigado", | ||
"welcome": "Bem-vindo" | ||
}, | ||
"modified": {}, | ||
"deleted": {} | ||
} | ||
} |
Empty file.
4 changes: 4 additions & 0 deletions
4
...e/tests/resources/diffing_algorithm/all_tests/all_added/modified_translations/french.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour", | ||
"thanks": "merci" | ||
} |
5 changes: 5 additions & 0 deletions
5
.../tests/resources/diffing_algorithm/all_tests/all_added/modified_translations/italian.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hello": "bonjourno", | ||
"thanks": "grazie", | ||
"welcome": "benvenuto" | ||
} |
5 changes: 5 additions & 0 deletions
5
...ests/resources/diffing_algorithm/all_tests/all_added/modified_translations/portugese.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hello": "ola", | ||
"thanks": "obrigado", | ||
"welcome": "Bem-vindo" | ||
} |
4 changes: 4 additions & 0 deletions
4
.../tests/resources/diffing_algorithm/all_tests/all_added/modified_translations/spanish.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "hola", | ||
"thanks": "gracias" | ||
} |
26 changes: 26 additions & 0 deletions
26
i18nilize/tests/resources/diffing_algorithm/all_tests/all_removed/expected_output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"italian": { | ||
"type": "deleted", | ||
"created": {}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"spanish": { | ||
"type": "deleted", | ||
"created": {}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"french": { | ||
"type": "deleted", | ||
"created": {}, | ||
"modified": {}, | ||
"deleted": {} | ||
}, | ||
"portugese": { | ||
"type": "deleted", | ||
"created": {}, | ||
"modified": {}, | ||
"deleted": {} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
.../tests/resources/diffing_algorithm/all_tests/all_removed/initial_translations/french.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour", | ||
"thanks": "merci" | ||
} |
5 changes: 5 additions & 0 deletions
5
...tests/resources/diffing_algorithm/all_tests/all_removed/initial_translations/italian.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hello": "bonjourno", | ||
"thanks": "grazie", | ||
"welcome": "benvenuto" | ||
} |
5 changes: 5 additions & 0 deletions
5
...sts/resources/diffing_algorithm/all_tests/all_removed/initial_translations/portugese.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hello": "ola", | ||
"thanks": "obrigado", | ||
"welcome": "Bem-vindo" | ||
} |
4 changes: 4 additions & 0 deletions
4
...tests/resources/diffing_algorithm/all_tests/all_removed/initial_translations/spanish.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "hola", | ||
"thanks": "gracias" | ||
} |
Empty file.
Oops, something went wrong.