|
| 1 | +import argparse |
| 2 | +import os |
| 3 | + |
| 4 | +import sys |
| 5 | + |
| 6 | +sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))) |
| 7 | +from paradox_localization_utils.read_localization_file import ( |
| 8 | + file_to_keys_and_lines, |
| 9 | + get_key, |
| 10 | + BadLocalizationException, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def get_args(): |
| 15 | + parser = argparse.ArgumentParser(description="Keep edited lines only") |
| 16 | + parser.add_argument("source_dir", type=str, help="Directory with Paradox files to filter") |
| 17 | + parser.add_argument("original_dir", type=str, help="Directory with original Paradox files") |
| 18 | + parser.add_argument("target_dir", type=str, help="Directory where to write filtered lines") |
| 19 | + parser.add_argument("-language", type=str, help="Language to filter", default="english") |
| 20 | + parser.add_argument("-target_prefix", type=str, help="Prefix added to files in target_dir", default="replace ") |
| 21 | + return parser.parse_args() |
| 22 | + |
| 23 | + |
| 24 | +def write_kept_lines(source_file_path: str, output_file_path: str, original_lines_by_key: dict[str, str]): |
| 25 | + """Write in output_file_path the lines of source_file_path that are not in or are edited from original_lines_by_key |
| 26 | +
|
| 27 | + :param source_file_path: File to filter |
| 28 | + :param output_file_path: File where we write the filtered lines |
| 29 | + :param original_lines_by_key: Dictionary with the original lines by key |
| 30 | + """ |
| 31 | + with open(source_file_path, "r", encoding="utf8") as f: |
| 32 | + lines = f.readlines() |
| 33 | + |
| 34 | + with open(output_file_path, "w", encoding="utf8") as f: |
| 35 | + language_found = False |
| 36 | + for line in lines: |
| 37 | + # Keep the language definition line |
| 38 | + if not language_found and line.startswith("l_"): |
| 39 | + f.write(line) |
| 40 | + language_found = True |
| 41 | + continue |
| 42 | + try: |
| 43 | + key = get_key(line) |
| 44 | + if key not in original_lines_by_key or line != original_lines_by_key[key]: |
| 45 | + # It is a new or edited line, we keep it |
| 46 | + f.write(line) |
| 47 | + except BadLocalizationException: |
| 48 | + pass |
| 49 | + |
| 50 | + |
| 51 | +def keep_only_edited_lines( |
| 52 | + source_dir: str, |
| 53 | + original_dir: str, |
| 54 | + target_dir: str, |
| 55 | + language: str, |
| 56 | + target_prefix: str |
| 57 | +): |
| 58 | + # Store original file paths |
| 59 | + original_file_paths_by_file_name: dict[str, str] = dict() |
| 60 | + for root, _, files in os.walk(original_dir): |
| 61 | + for file in files: |
| 62 | + if file.endswith(language + ".yml"): |
| 63 | + original_file_paths_by_file_name[file] = os.path.join(root, file) |
| 64 | + |
| 65 | + # Analyze each file in source_dir to kepts only edited lines |
| 66 | + for root, _, files in os.walk(source_dir): |
| 67 | + for file in files: |
| 68 | + if file.endswith(language + ".yml"): |
| 69 | + print(f"Processing {file}...") |
| 70 | + # TODO Copy file when file is not in original_file_paths_by_file_name |
| 71 | + original_lines_by_key, _ = file_to_keys_and_lines(original_file_paths_by_file_name[file]) |
| 72 | + write_kept_lines( |
| 73 | + os.path.join(root, file), |
| 74 | + os.path.join(target_dir, target_prefix + file), |
| 75 | + original_lines_by_key, |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + args = get_args() |
| 81 | + keep_only_edited_lines( |
| 82 | + args.source_dir, |
| 83 | + args.original_dir, |
| 84 | + args.target_dir, |
| 85 | + args.language, |
| 86 | + args.target_prefix, |
| 87 | + ) |
0 commit comments