Skip to content

Commit

Permalink
moved diff_processor helper functions to helper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-fenton committed Nov 20, 2024
1 parent 7282622 commit d7257af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
40 changes: 1 addition & 39 deletions i18nilize/src/internationalize/diffing_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import json
from dirsync import sync
from src.internationalize.helpers import compute_hash, compute_hashes, read_json_file

JSON_EXTENSION = ".json"

Expand Down Expand Up @@ -154,42 +155,3 @@ def __initialize_changed_template(self, type):
changed_translations[MODIFIED] = {}
changed_translations[DELETED] = {}
return changed_translations


"""
Helper functions
"""

def compute_hash(file_content):
hash = hashlib.sha256()
hash.update(file_content)
return hash.hexdigest()

def compute_hashes(directory):
hash_dict = {}
files = os.listdir(directory)
for file_name in files:
path = directory + "/" + file_name

# Read file as byte buffer for hashing
with open(path, "rb") as file:
file_name_no_ext = file_name.split(".")[0]
file_content = file.read()
file_hash = compute_hash(file_content)
hash_dict[file_name_no_ext] = file_hash

return hash_dict

def read_json_file(directory):
try:
with open(directory, "r") as file:
json_object = json.load(file)
return json_object
except FileNotFoundError:
print(f"File not found: {directory}")
raise
except IOError:
print(f"An error occurred while trying to read the file: {directory}")
raise
except Exception as e:
print(f"An exception occured: {e}")
44 changes: 44 additions & 0 deletions i18nilize/src/internationalize/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import sys
import os
import hashlib
import requests
from . import globals

Expand Down Expand Up @@ -138,3 +139,46 @@ def make_translation_map(data):
def get_translation(translations_map, language):
return translations_map.get(language, "Translation not found")

"""
Computes 256-bit hash for given content
"""
def compute_hash(file_content):
hash = hashlib.sha256()
hash.update(file_content)
return hash.hexdigest()

"""
Computes hashes for all files in a directory
"""
def compute_hashes(directory):
hash_dict = {}
files = os.listdir(directory)
for file_name in files:
path = directory + "/" + file_name

# Read file as byte buffer for hashing
with open(path, "rb") as file:
file_name_no_ext = file_name.split(".")[0]
file_content = file.read()
file_hash = compute_hash(file_content)
hash_dict[file_name_no_ext] = file_hash

return hash_dict

"""
Reads a file given the directory and returns json object
Expects file to be in json format
"""
def read_json_file(directory):
try:
with open(directory, "r") as file:
json_object = json.load(file)
return json_object
except FileNotFoundError:
print(f"File not found: {directory}")
raise
except IOError:
print(f"An error occurred while trying to read the file: {directory}")
raise
except Exception as e:
print(f"An exception occured: {e}")

0 comments on commit d7257af

Please sign in to comment.