prettify: print message about prettified file

This should make it much more user friendly, especially together
with the CI and pre-commit since with `--no-report-errors`
only a list of prettified files plus a summary will be reported,
instead of the list of INFO and WARNING lines for things the
user can't do anything about.
This commit is contained in:
Tiziano Müller 2020-02-12 17:48:52 +01:00 committed by Tiziano Müller
parent 58ebbcbc47
commit febe55d15a

View file

@ -8,7 +8,6 @@ from os import path
import logging
import argparse
import errno
import traceback
import contextlib
try:
@ -272,7 +271,9 @@ def prettifyFile(
def prettifyInplace(filename, backupdir=None, stdout=False, **kwargs):
"""Same as prettify, but inplace, replaces only if needed"""
"""Same as prettify, but inplace, replaces only if needed
:return: True if file was modified, False if otherwise. If stdin/stdout, always False.
"""
if filename == "stdin":
infile = tempfile.TemporaryFile(mode="r+")
@ -287,11 +288,11 @@ def prettifyInplace(filename, backupdir=None, stdout=False, **kwargs):
outfile.seek(0)
sys.stdout.write(outfile.read())
outfile.close()
return
return False
if infile == outfile:
infile.close()
return
return False
infile.seek(0)
outfile.seek(0)
@ -321,6 +322,8 @@ def prettifyInplace(filename, backupdir=None, stdout=False, **kwargs):
outfile.close()
return changed
def is_fypp(infile):
FYPP_SYMBOLS = r"(#|\$|@)"
@ -360,11 +363,6 @@ def mkdir_p(p):
raise
# from https://stackoverflow.com/a/14981125
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def abspath(p):
return path.abspath(path.expanduser(p))
@ -422,6 +420,9 @@ def main(argv):
False,
"store backups of original files in backup-dir (--backup-dir option)",
)
argparse_add_bool_arg(
parser, "quiet", False, "don't show summary or list of reformatted files"
)
argparse_add_bool_arg(parser, "report-errors", True, "report warnings and errors")
argparse_add_bool_arg(parser, "debug", False, "increase log level to debug")
@ -438,7 +439,7 @@ def main(argv):
if args.debug:
level = logging.DEBUG
else:
level = logging.INFO
level = logging.ERROR
# the fprettify logger provides filename and line number in case of errors
shandler = logging.StreamHandler()
@ -461,15 +462,16 @@ def main(argv):
prettify_logger.addHandler(shandler)
failure = 0
total_prettified = 0
for filename in args.files:
if not path.isfile(filename) and not filename == "stdin":
eprint("file '{}' does not exist!".format(filename))
logger.error(f"file '{filename}' does not exist")
failure += 1
continue
try:
prettifyInplace(
changed = prettifyInplace(
filename,
backupdir=args.backup_dir if args.do_backup else None,
stdout=args.stdout or filename == "stdin",
@ -483,10 +485,20 @@ def main(argv):
upcase_omp=args.omp_upcase,
replace=args.replace,
)
if changed and not args.quiet:
print(f"prettified {filename}")
total_prettified += 1
except:
logger.exception("processing file failed", extra={"ffilename": filename})
failure += 1
if not (args.stdout or filename == "stdin" or args.quiet):
print(
f"{total_prettified} file(s) prettified, {len(args.files) - total_prettified} file(s) left unchanged."
)
return failure > 0