|
| 1 | +import os |
| 2 | +from argparse import ArgumentParser |
| 3 | +from functools import lru_cache |
| 4 | +from subprocess import check_output |
| 5 | + |
| 6 | +import polib |
| 7 | + |
| 8 | +parser = ArgumentParser() |
| 9 | +parser.add_argument( |
| 10 | + "path", |
| 11 | + nargs="?", |
| 12 | + default=None, |
| 13 | + help="Path to the .po file or directory containing .po files", |
| 14 | +) |
| 15 | +parser.add_argument( |
| 16 | + "--no-cache", action="store_true", default=False, help="Don't use cache" |
| 17 | +) |
| 18 | +args = parser.parse_args() |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + global git_root |
| 23 | + total_progress = False |
| 24 | + git_root = get_git_root() |
| 25 | + |
| 26 | + if args.no_cache: |
| 27 | + progress.cache_clear() |
| 28 | + get_git_root.cache_clear() |
| 29 | + |
| 30 | + if args.path is None: |
| 31 | + print("No path specified, showing total progress...") |
| 32 | + args.path = os.path.abspath(git_root).replace("\\", "/") |
| 33 | + total_progress = True |
| 34 | + |
| 35 | + else: |
| 36 | + args.path = os.path.abspath(args.path).replace("\\", "/") |
| 37 | + |
| 38 | + if os.path.isfile(args.path): |
| 39 | + paths = [args.path] |
| 40 | + |
| 41 | + elif os.path.isdir(args.path): |
| 42 | + paths = [] |
| 43 | + for root, _, files in os.walk(args.path): |
| 44 | + paths.extend( |
| 45 | + os.path.join(root, file) for file in files if file.endswith(".po") |
| 46 | + ) |
| 47 | + paths = map(lambda x: x.replace("\\", "/"), paths) |
| 48 | + |
| 49 | + else: |
| 50 | + print("Invalid path") |
| 51 | + return -1 |
| 52 | + |
| 53 | + try: |
| 54 | + progress(tuple(paths), total_progress) |
| 55 | + return 0 |
| 56 | + except Exception as e: |
| 57 | + print(f"Error: {e}") |
| 58 | + return -1 |
| 59 | + |
| 60 | + |
| 61 | +@lru_cache(maxsize=512) |
| 62 | +def progress(paths, total_progress=False): |
| 63 | + total = 0 |
| 64 | + translated = 0 |
| 65 | + previous = "/" |
| 66 | + is_root = True |
| 67 | + for path in paths: |
| 68 | + pofile = polib.pofile(path) |
| 69 | + total += len(pofile) - len(pofile.obsolete_entries()) |
| 70 | + translated += len(pofile.translated_entries()) |
| 71 | + path = path.replace(f"{git_root}", "") |
| 72 | + if is_root and len(path.split("/")) == 2: |
| 73 | + print() |
| 74 | + print("PYTHON-DOCS-TR") |
| 75 | + print("-" * 14) |
| 76 | + is_root = False |
| 77 | + if (previous.split("/")[1] != path.split("/")[1]) and len(path.split("/")) > 2: |
| 78 | + print() |
| 79 | + print(path.split("/")[1].upper()) |
| 80 | + print("-" * len(path.split("/")[1].upper())) |
| 81 | + previous = path |
| 82 | + print(f"{path}: {pofile.percent_translated()}%") |
| 83 | + |
| 84 | + dir_path = args.path.replace(f"{git_root}", "/").replace("//", "/") |
| 85 | + total_progress_of = "/" if total_progress else dir_path |
| 86 | + print() |
| 87 | + print( |
| 88 | + f"Total progress of {total_progress_of}: {round(translated / total * 100, 2)}%" |
| 89 | + ) if len(paths) > 1 else None |
| 90 | + |
| 91 | + |
| 92 | +@lru_cache(maxsize=1) |
| 93 | +def get_git_root(): |
| 94 | + return os.path.abspath( |
| 95 | + check_output(["git", "rev-parse", "--show-toplevel"]).decode("utf-8").strip() |
| 96 | + ).replace("\\", "/") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments