powrap: Add --diff option.

This commit is contained in:
Julien Palard 2020-12-03 13:00:02 +01:00
parent 8ab1522abc
commit a1a21ed2fd

View File

@ -6,6 +6,7 @@
import argparse import argparse
import sys import sys
from typing import Iterable from typing import Iterable
import difflib
from pathlib import Path from pathlib import Path
from subprocess import check_output, run, CalledProcessError from subprocess import check_output, run, CalledProcessError
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -15,9 +16,12 @@ from tqdm import tqdm
from powrap import __version__ from powrap import __version__
def check_style(po_files: Iterable[str], no_wrap=False, quiet=False): def check_style(po_files: Iterable[str], no_wrap=False, quiet=False, diff=False) -> int:
"""Check style of given po_files""" """Check style of given po_files.
to_fix = []
Prints errors on stderr and returns the number of errors found.
"""
errors = 0
for po_path in tqdm(po_files, desc="Checking wrapping of po files", disable=quiet): for po_path in tqdm(po_files, desc="Checking wrapping of po files", disable=quiet):
try: try:
with open(po_path, encoding="UTF-8") as po_file: with open(po_path, encoding="UTF-8") as po_file:
@ -39,8 +43,15 @@ def check_style(po_files: Iterable[str], no_wrap=False, quiet=False):
sys.exit(127) sys.exit(127)
new_po_content = tmpfile.read() new_po_content = tmpfile.read()
if po_content != new_po_content: if po_content != new_po_content:
to_fix.append(po_path) errors += 1
return to_fix print("Would rewrap:", po_path, file=sys.stderr)
if diff:
for line in difflib.unified_diff(
po_content.splitlines(keepends=True),
new_po_content.splitlines(keepends=True),
):
print(line, end="", file=sys.stderr)
return errors
def fix_style(po_files, no_wrap=False, quiet=False): def fix_style(po_files, no_wrap=False, quiet=False):
@ -93,6 +104,12 @@ def parse_args():
parser.add_argument( parser.add_argument(
"--quiet", "-q", action="store_true", help="Do not show progress bar." "--quiet", "-q", action="store_true", help="Do not show progress bar."
) )
parser.add_argument(
"--diff",
"-d",
action="store_true",
help="Don't write the files back, just output a diff for each file on stdout (implies --check).",
)
parser.add_argument( parser.add_argument(
"--check", "--check",
action="store_true", action="store_true",
@ -135,10 +152,8 @@ def main():
if not args.po_files: if not args.po_files:
print("Nothing to do, exiting.") print("Nothing to do, exiting.")
sys.exit(0) sys.exit(0)
if args.check: if args.check or args.diff:
to_fix = check_style(args.po_files, args.no_wrap, args.quiet) errors = check_style(args.po_files, args.no_wrap, args.quiet, args.diff)
if to_fix: sys.exit(errors > 0)
print("Would rewrap:", *to_fix, sep="\n- ")
sys.exit(1 if to_fix else 0)
else: else:
fix_style(args.po_files, args.no_wrap, args.quiet) fix_style(args.po_files, args.no_wrap, args.quiet)