Skip to content

Commit

Permalink
Add verbosity option add more logs (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmshn authored Aug 23, 2020
1 parent e14b5c3 commit 0df3f59
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import distutils.sysconfig
import fnmatch
import io
import logging
import os
import re
import signal
Expand All @@ -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+:$')
Expand Down Expand Up @@ -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(),
Expand All @@ -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',
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 0df3f59

Please sign in to comment.