|
| 1 | +import argparse |
| 2 | +import dataclasses |
| 3 | +import enum |
| 4 | +import glob |
| 5 | +import itertools |
| 6 | +import os |
| 7 | + |
| 8 | +import polib |
| 9 | +import tabulate |
| 10 | + |
| 11 | + |
| 12 | +class MissingReason(enum.StrEnum): |
| 13 | + FUZZY = "fuzzy" |
| 14 | + UNTRANSLATED = "untranslated" |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def from_poentry(poentry: polib.POEntry): |
| 18 | + if poentry.fuzzy: |
| 19 | + return MissingReason.FUZZY |
| 20 | + assert not poentry.translated() |
| 21 | + return MissingReason.UNTRANSLATED |
| 22 | + |
| 23 | +@dataclasses.dataclass |
| 24 | +class MissingEntry: |
| 25 | + reason: MissingReason |
| 26 | + file: str |
| 27 | + line: int |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def from_poentry(pofilename: str, poentry: polib.POEntry) -> "MissingEntry": |
| 31 | + return MissingEntry(MissingReason.from_poentry(poentry), pofilename, poentry.linenum) |
| 32 | + |
| 33 | + |
| 34 | +def find_missing_entries(filename: str) -> list[MissingEntry]: |
| 35 | + po = polib.pofile(filename) |
| 36 | + fuzzy = po.fuzzy_entries() |
| 37 | + untranslated = po.untranslated_entries() |
| 38 | + return [MissingEntry.from_poentry(filename, entry) for entry in fuzzy + untranslated] |
| 39 | + |
| 40 | +def main(): |
| 41 | + parser = argparse.ArgumentParser() |
| 42 | + parser.add_argument("files", nargs="+") |
| 43 | + parser.add_argument("-g", "--github-mode", help="Produce output as a GitHub comment", action='store_true') |
| 44 | + opts = parser.parse_args() |
| 45 | + missing_entries = list(itertools.chain.from_iterable(map(find_missing_entries, opts.files))) |
| 46 | + if not missing_entries: |
| 47 | + print(f"All entries translated, horray!{' :tada:' if opts.github_mode else ''}") |
| 48 | + else: |
| 49 | + missing_entries.sort(key = lambda entry: (entry.file, entry.line)) |
| 50 | + print("Entries missing translation, details follow:\n") |
| 51 | + print(tabulate.tabulate(missing_entries,headers=["Reason", "File", "Line"], tablefmt="github")) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + main() |
0 commit comments