Skip to content

Commit

Permalink
Merge pull request #2 from francktrouillez/fix/consider-different-sch…
Browse files Browse the repository at this point in the history
…ema-between-source-and-target-files

[Fix] Automatically convert different schemas into the source one
  • Loading branch information
francktrouillez authored Jun 20, 2024
2 parents d57b683 + baf23b9 commit 0f1c922
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,22 @@ def find_missing_keys(source_dict: dict, target_dict: dict, current_key_path: li
:return: A list of keys that are missing in the target dictionary.
"""
missing_keys = []
if type(target_dict) != dict:
for nested_key in BaseFile.__get_nested_keys(source_dict):
missing_keys.append(current_key_path + nested_key)
return missing_keys
for key, value in source_dict.items():
if value is None:
continue

if type(value) == dict:
missing_keys.extend(BaseFile.find_missing_keys(value, target_dict.get(key, {}), current_key_path + [key]))
missing_keys.extend(BaseFile.find_missing_keys(value, target_dict.get(key, {}), current_key_path + [key]))
else:
if key not in target_dict:
missing_keys.append(current_key_path + [key])
else:
if type(value) != type(target_dict[key]):
missing_keys.append(current_key_path + [key])
return missing_keys

@staticmethod
Expand Down Expand Up @@ -110,3 +118,22 @@ def _ensure_directories_exist(file_path: str) -> None:
if directories:
directories = "/".join(directories)
os.makedirs(directories, exist_ok=True)

@staticmethod
def __get_nested_keys(object: dict) -> list[str]:
"""
Get the nested keys for a dictionary.
:param object: The dictionary to get the nested keys for.
:return: A list of nested keys.
"""
keys = []
if type(object) != dict:
return keys
for key, value in object.items():
if type(value) == dict:
for nested_key in BaseFile.__get_nested_keys(value):
keys.append([key, *nested_key])
else:
keys.append([key])
return keys
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
)
target_translation_data = target_data
for key in keys[:-1]:
if type(target_translation_data.get(key)) != dict:
target_translation_data[key] = {}
target_translation_data = target_translation_data.setdefault(key, {})
target_translation_data[keys[-1]] = target_translation
print(f"[{source_file} - {target_language}] Writing translations to '{target_file}'")
Expand Down

0 comments on commit 0f1c922

Please sign in to comment.