From 0df3f59f3bd772d3982a0ad09408ba3e3ab5049f Mon Sep 17 00:00:00 2001 From: Shinichi TAMURA Date: Sun, 23 Aug 2020 10:18:49 +0900 Subject: [PATCH] Add verbosity option add more logs (#69) --- autoflake.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/autoflake.py b/autoflake.py index ecf1db2..0144e90 100755 --- a/autoflake.py +++ b/autoflake.py @@ -32,6 +32,7 @@ import distutils.sysconfig import fnmatch import io +import logging import os import re import signal @@ -47,6 +48,9 @@ __version__ = '1.3.1' +_LOGGER = logging.getLogger('autoflake') +_LOGGER.propagate = False + ATOMS = frozenset([tokenize.NAME, tokenize.NUMBER, tokenize.STRING]) EXCEPT_REGEX = re.compile(r'^\s*except [\s,()\w]+ as \w+:$') @@ -839,6 +843,7 @@ def fix_file(filename, args, standard_out): with open_with_encoding(filename, mode='w', encoding=encoding) as output_file: output_file.write(filtered_source) + _LOGGER.info('Fixed %s', filename) else: diff = get_diff_text( io.StringIO(original_source).readlines(), @@ -848,6 +853,8 @@ def fix_file(filename, args, standard_out): else: if args.check: standard_out.write('No issues detected!\n') + else: + _LOGGER.debug('Clean %s: nothing to fix', filename) def open_with_encoding(filename, encoding, mode='r', @@ -951,6 +958,7 @@ def is_exclude_file(filename, exclude): def match_file(filename, exclude): """Return True if file is okay for modifying/recursing.""" if is_exclude_file(filename, exclude): + _LOGGER.debug('Skipped %s: matched to exclude pattern', filename) return False if not os.path.isdir(filename) and not is_python_file(filename): @@ -974,6 +982,8 @@ def find_files(filenames, recursive, exclude): else: if not is_exclude_file(name, exclude): yield name + else: + _LOGGER.debug('Skipped %s: matched to exclude pattern', name) def _main(argv, standard_out, standard_error): @@ -1014,13 +1024,26 @@ def _main(argv, standard_out, standard_error): help='remove unused variables') parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) + parser.add_argument('-v', '--verbose', action='count', dest='verbosity', + default=0, help='print more verbose logs (you can ' + 'repeat `-v` to make it more verbose)') parser.add_argument('files', nargs='+', help='files to format') args = parser.parse_args(argv[1:]) + if standard_error is None: + _LOGGER.addHandler(logging.NullHandler()) + else: + _LOGGER.addHandler(logging.StreamHandler(standard_error)) + loglevels = [logging.WARNING, logging.INFO, logging.DEBUG] + try: + loglevel = loglevels[args.verbosity] + except IndexError: # Too much -v + loglevel = loglevels[-1] + _LOGGER.setLevel(loglevel) + if args.remove_all_unused_imports and args.imports: - print('Using both --remove-all and --imports is redundant', - file=standard_error) + _LOGGER.error('Using both --remove-all and --imports is redundant') return 1 if args.exclude: @@ -1034,7 +1057,7 @@ def _main(argv, standard_out, standard_error): try: fix_file(name, args=args, standard_out=standard_out) except IOError as exception: - print(unicode(exception), file=standard_error) + _LOGGER.error(unicode(exception)) failure = True return 1 if failure else 0