diff --git a/.scripts/check-colon.py b/.scripts/check-colon.py index f34b24f3..e3187af4 100644 --- a/.scripts/check-colon.py +++ b/.scripts/check-colon.py @@ -1,7 +1,10 @@ +#!/usr/bin/env python +"""Check consistency of colons at the end of entries. + +It returns 0 if nothing was done, 1 if files were modified. +""" import sys import pathlib -from itertools import chain - import polib @@ -15,7 +18,7 @@ def colon_is_visible(msg): raise ValueError("Don't know if msg ends with a visible or an invisible colon.") -def fix_double_colon(entry, check): +def fix_double_colon(filename, entry, check, verbose): fixed = entry.msgstr.rstrip(": \u202f\u00A0") if colon_is_visible(entry.msgid): fixed += "\u00A0::" @@ -25,44 +28,78 @@ def fix_double_colon(entry, check): message = "Expected translation to end with: ' ::'" if entry.msgstr != fixed: if check: - print(f"{file}:{entry.linenum}: {message}") + print(f"{filename}:{entry.linenum}: {message}") + if verbose: + print(entry) else: entry.msgstr = fixed return True return False -def fix_simple_colon(entry, check): +def fix_simple_colon(filename, entry, check, verbose): fixed = entry.msgstr.rstrip(": \u202f\u00A0") fixed += "\u00A0:" if entry.msgstr != fixed: if check: - print(fr"{file}:{entry.linenum}: Expected translation to end with: '\u00A0:'") + print( + rf"{filename}:{entry.linenum}: Expected translation to end with: '\u00A0:'" + ) + if verbose: + print(entry) else: entry.msgstr = fixed return True return False -def check(file): - check = '--check' in sys.argv - pofile = polib.pofile(file) +def check(filename, check, verbose): + try: + pofile = polib.pofile(filename) + except OSError: + print(f"{filename} doesn't seem to be a .po file", file=sys.stderr) + return True has_errors = False for entry in pofile: if not entry.msgstr: continue # No need to check untranslated entries. if entry.msgid.endswith("::"): - has_errors |= fix_double_colon(entry, check) + has_errors |= fix_double_colon(filename, entry, check, verbose) elif entry.msgid.endswith(":"): - has_errors |= fix_simple_colon(entry, check) + has_errors |= fix_simple_colon(filename, entry, check, verbose) if not check and has_errors: pofile.save() return has_errors -has_errors = False -for file in chain(pathlib.Path(".").glob("*.po"), pathlib.Path(".").glob("*/*.po")): - has_errors |= check(file) +def parse_args(): + import argparse + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--check", + action="store_true", + default=False, + help="only display suspected entries, do not fix.", + ) + parser.add_argument( + "--verbose", + action="store_true", + default=False, + help="display whole entry", + ) + parser.add_argument("path", nargs="*") + return parser.parse_args() + + +def main(): + has_errors = False + args = parse_args() + for filename in args.path: + has_errors |= check(filename, args.check, args.verbose) + sys.exit(has_errors) + -sys.exit(has_errors) +if __name__ == "__main__": + main()