|
| 1 | +"""Checks for missing in PO files error messages. |
| 2 | +
|
| 3 | +Returns an error code if a found an error message not included in PO file. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from pre_commit_po_hooks import utils |
| 11 | + |
| 12 | + |
| 13 | +class Check: |
| 14 | + |
| 15 | + quiet: bool |
| 16 | + po_dir: str |
| 17 | + repo_directory: str |
| 18 | + errors_patterns: list[str] |
| 19 | + |
| 20 | + def __init__(self, quiet: bool, po_dir: str, repo_directory: str, errors_patterns: list[str]): |
| 21 | + self.quiet = quiet |
| 22 | + self.po_dir = po_dir |
| 23 | + self.repo_directory = repo_directory |
| 24 | + self.errors_patterns = errors_patterns |
| 25 | + |
| 26 | + if not self.quiet: |
| 27 | + sys.stdout.write( |
| 28 | + f"Run with args: \n" |
| 29 | + f"`errors_patterns`: {self.errors_patterns},\n" |
| 30 | + f"`repo_directory`: {self.repo_directory},\n" |
| 31 | + f"`po_dir`: {self.po_dir}\n" |
| 32 | + ) |
| 33 | + |
| 34 | + def get_py_filenames(self) -> list[Path]: |
| 35 | + filenames = [] |
| 36 | + try: |
| 37 | + for pattern in self.errors_patterns: |
| 38 | + filenames += [ |
| 39 | + p for p in Path(self.repo_directory).rglob(pattern) |
| 40 | + if not any(x.startswith('.') or x.startswith('_') for x in p.parts) |
| 41 | + ] |
| 42 | + except Exception: |
| 43 | + raise Exception("Incorrect error filename pattern.\n") |
| 44 | + |
| 45 | + if not self.quiet: |
| 46 | + sys.stdout.write("Found error files: " + str(filenames) + '\n') |
| 47 | + |
| 48 | + return filenames |
| 49 | + |
| 50 | + def get_po_filenames(self, po_dir: str) -> list[Path]: |
| 51 | + return [p.parent / p.name for p in Path(po_dir).rglob("*.po") if os.path.isfile(p.parent / p.name)] |
| 52 | + |
| 53 | + def update_en_po(self, poes_data: dict, py_objects: set[str]) -> int: |
| 54 | + (filename, catalog), *_ = [ |
| 55 | + (filename, catalog) for filename, catalog in poes_data.items() if catalog.locale_identifier == 'en' |
| 56 | + ] |
| 57 | + po_objects = set(message.id for message in catalog if message.id) |
| 58 | + |
| 59 | + if sorted(list(py_objects)) != sorted(list(po_objects)): |
| 60 | + utils.update_po_file( |
| 61 | + errors=set((e, e) for e in py_objects), catalog=catalog, po_filepath=Path(self.po_dir) / filename |
| 62 | + ) |
| 63 | + return 1 |
| 64 | + |
| 65 | + return 0 |
| 66 | + |
| 67 | + def update_non_en_po(self, poes_data: dict, py_objects: set[str]) -> int: |
| 68 | + res = 0 |
| 69 | + |
| 70 | + for filename, catalog in poes_data.items(): |
| 71 | + if catalog.locale_identifier == 'en': |
| 72 | + continue |
| 73 | + |
| 74 | + messages = {message.id: message.string for message in catalog if message.id} |
| 75 | + |
| 76 | + if messages.keys() - py_objects: |
| 77 | + res = 1 |
| 78 | + utils.update_po_file( |
| 79 | + catalog=catalog, |
| 80 | + po_filepath=Path(self.po_dir) / filename, |
| 81 | + errors=set( |
| 82 | + (message_id, message_text) |
| 83 | + for message_id, message_text in messages.items() if message_id in py_objects |
| 84 | + ), |
| 85 | + ) |
| 86 | + |
| 87 | + return res |
| 88 | + |
| 89 | + def _execute(self): |
| 90 | + """Warns about all missed error messages found in filenames that not included in PO files. |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + errors_patterns : list[str] |
| 95 | + Pattern of errors.py filenames |
| 96 | +
|
| 97 | + repo_directory : str |
| 98 | + Path to repository containing errors.py files |
| 99 | +
|
| 100 | + po_dir : string |
| 101 | + Path to dir with .po files. |
| 102 | +
|
| 103 | + quiet : bool, optional |
| 104 | + Enabled, don't print output to stderr when an untranslated message is found. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | +
|
| 109 | + int: 0 if no missed messages found, 1 otherwise. |
| 110 | + """ |
| 111 | + py_objects = utils.load_py(filenames=self.get_py_filenames()) |
| 112 | + |
| 113 | + poes_data = {f.name: utils.load_po(po_filepath=f) for f in self.get_po_filenames(self.po_dir)} |
| 114 | + |
| 115 | + res1 = self.update_en_po(poes_data, py_objects=py_objects) |
| 116 | + res2 = 0 # self.update_non_en_po(poes_data, py_objects=py_objects) |
| 117 | + |
| 118 | + if res1 and not self.quiet: |
| 119 | + raise Exception("File .po was updated.\n") |
| 120 | + |
| 121 | + if res2 and not self.quiet: |
| 122 | + raise Exception("Non default .po files were updated.\n") |
| 123 | + |
| 124 | + return min(sum([res1, res2]), 1) |
| 125 | + |
| 126 | + def execute(self): |
| 127 | + try: |
| 128 | + return self._execute() |
| 129 | + except Exception as e: |
| 130 | + sys.stderr.write(str(e) or f"Hook error happened: {repr(e)}") |
| 131 | + return 1 |
| 132 | + |
| 133 | + |
| 134 | +def main(): |
| 135 | + parser = argparse.ArgumentParser() |
| 136 | + |
| 137 | + parser.add_argument("filenames", nargs="*", help="Changed files") |
| 138 | + parser.add_argument("-q", "--quiet", required=False, default=False, help="Supress output") |
| 139 | + parser.add_argument("--repo_directory", required=True, help="Path to repository containing errors.py files") |
| 140 | + parser.add_argument("--po_dir", required=True, help="Path to dir with .po files") |
| 141 | + parser.add_argument( |
| 142 | + "--errors_patterns", required=False, nargs='*', default=["errors.py"], help="Pattern of errors.py filenames" |
| 143 | + ) |
| 144 | + |
| 145 | + args = parser.parse_args() |
| 146 | + |
| 147 | + if not args.filenames: |
| 148 | + return 0 |
| 149 | + |
| 150 | + return Check( |
| 151 | + quiet=args.quiet, |
| 152 | + po_dir=args.po_dir, |
| 153 | + repo_directory=args.repo_directory, |
| 154 | + errors_patterns=args.errors_patterns, |
| 155 | + ).execute() |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + exit(main()) |
0 commit comments