Fix formatting of lib/duplicate_translations.py

pull/1062/head
Igor Chubin 1 month ago
parent 2f6c064908
commit 85ba61f37b

@ -1,33 +1,38 @@
import os import os
def remove_colon_and_strip_from_str(line): def remove_colon_and_strip_from_str(line):
""" """
Removes the colon from the line and strips the line. Removes the colon from the line and strips the line.
""" """
return line.replace(":", "").strip() return line.replace(":", "").strip()
def print_result_for_file(file_path, file_name, duplicate_entries): def print_result_for_file(file_path, file_name, duplicate_entries):
""" """
Prints the result for a given file. Prints the result for a given file.
""" """
print(f"-" * 50) print("-" * 50)
print(f"Processing file: {file_name} \n") print(f"Processing file: {file_name} \n")
# keep entries with more than one occurence # keep entries with more than one occurence
if len(duplicate_entries) > 0: if len(duplicate_entries) > 0:
for key, value in duplicate_entries.items(): for key, value in duplicate_entries.items():
# prints debug info for each duplicate found # prints debug info for each duplicate found
print(f"{file_path}: \"{key}\" appears in lines {", ".join(map(str, value))}") print(
f"{file_path}: \"{key}\" appears in lines {', '.join(map(str, value))}"
)
else: else:
# prints debug info, if no duplicates found 🥳 # prints debug info, if no duplicates found 🥳
print(f"No duplicates found!") print(f"No duplicates found!")
def find_duplicates(directory, debug=False): def find_duplicates(directory, debug=False):
""" """
Reads all .txt files in a given directory and tries to detect duplicate entries. Reads all .txt files in a given directory and tries to detect duplicate entries.
""" """
try: try:
language_lookup_table = {} language_lookup_table = {}
files = [f for f in os.listdir(directory) if f.endswith('.txt')] files = [f for f in os.listdir(directory) if f.endswith(".txt")]
files.sort() files.sort()
if not files: if not files:
@ -41,12 +46,14 @@ def find_duplicates(directory, debug=False):
file_path = os.path.join(directory, file_name) file_path = os.path.join(directory, file_name)
try: try:
with open(file_path, 'r', encoding='utf-8') as file: with open(file_path, "r", encoding="utf-8") as file:
lookup_table = {} lookup_table = {}
for line_number, line in enumerate(file, start=1): for line_number, line in enumerate(file, start=1):
stripped_line = line.strip() stripped_line = line.strip()
stripped_keywords = stripped_line.split(":") stripped_keywords = stripped_line.split(":")
stripped_keywords = list(map(remove_colon_and_strip_from_str, stripped_keywords)) stripped_keywords = list(
map(remove_colon_and_strip_from_str, stripped_keywords)
)
trimmed_keywords = list(map(str.strip, stripped_keywords)) trimmed_keywords = list(map(str.strip, stripped_keywords))
for tk in trimmed_keywords: for tk in trimmed_keywords:
@ -56,7 +63,9 @@ def find_duplicates(directory, debug=False):
lookup_table[tk].append(line_number) lookup_table[tk].append(line_number)
else: else:
lookup_table[tk] = [line_number] lookup_table[tk] = [line_number]
duplicate_entries = {k: v for k, v in lookup_table.items() if len(v) > 1} duplicate_entries = {
k: v for k, v in lookup_table.items() if len(v) > 1
}
print_result_for_file(file_path, file_name, duplicate_entries) print_result_for_file(file_path, file_name, duplicate_entries)
language_lookup_table[file_name] = duplicate_entries language_lookup_table[file_name] = duplicate_entries
except Exception as e: except Exception as e:
@ -67,6 +76,7 @@ def find_duplicates(directory, debug=False):
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
return None return None
# Example usage from the root directory: # Example usage from the root directory:
# python ./lib/duplicate_translations.py # python ./lib/duplicate_translations.py
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save