From 222292c4b2bb595c81d749bfd35ef9adc640bacb Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 25 Jul 2016 19:32:42 -0500 Subject: [PATCH] Handle repeated --quiet options again Fundamentally on Flake8 2.x using -q altered the format of the errors (and the behaviour a little) so it makes the most sense to implement this logic with formatters rather than messy logic spread throughout the project. The FilenameOnly formatter will keep track of filenames already reported and only print the name once while Nothing will print nothing. Closes #180 --- setup.py | 2 ++ src/flake8/formatting/base.py | 3 ++- src/flake8/formatting/default.py | 24 ++++++++++++++++++++++++ src/flake8/main/application.py | 9 ++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index cd002cd..680e68f 100644 --- a/setup.py +++ b/setup.py @@ -107,6 +107,8 @@ setuptools.setup( 'flake8.report': [ 'default = flake8.formatting.default:Default', 'pylint = flake8.formatting.default:Pylint', + 'quiet-filename = flake8.formatting.default:FilenameOnly', + 'quiet-nothing = flake8.formatting.default:Nothing', ], }, classifiers=[ diff --git a/src/flake8/formatting/base.py b/src/flake8/formatting/base.py index c4a626b..2ac0ed8 100644 --- a/src/flake8/formatting/base.py +++ b/src/flake8/formatting/base.py @@ -164,7 +164,8 @@ class BaseFormatter(object): The source code that has been formatted and associated with the line of output. """ - self._write(line) + if line: + self._write(line) if source: self._write(source) diff --git a/src/flake8/formatting/default.py b/src/flake8/formatting/default.py index bef8c88..f8e3a66 100644 --- a/src/flake8/formatting/default.py +++ b/src/flake8/formatting/default.py @@ -54,3 +54,27 @@ class Pylint(SimpleFormatter): """Pylint formatter for Flake8.""" error_format = '%(path)s:%(row)d: [%(code)s] %(text)s' + + +class FilenameOnly(SimpleFormatter): + """Only print filenames, e.g., flake8 -q.""" + + error_format = '%(path)s' + + def after_init(self): + """Initialize our set of filenames.""" + self.filenames_already_printed = set() + + def format(self, error): + """Ensure we only print each error once.""" + if error.filename not in self.filenames_already_printed: + self.filenames_already_printed.add(error.filename) + return super(FilenameOnly, self).format(error) + + +class Nothing(base.BaseFormatter): + """Print absolutely nothing.""" + + def format(self, error): + """Do nothing.""" + pass diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 464ff92..8a02fd6 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -181,10 +181,17 @@ class Application(object): # type: () -> NoneType """Initialize a formatter based on the parsed options.""" if self.formatter is None: + format_plugin = self.options.format + if 1 <= self.options.quiet < 2: + format_plugin = 'quiet-filename' + elif 2 <= self.options.quiet: + format_plugin = 'quiet-nothing' + if formatter_class is None: formatter_class = self.formatting_plugins.get( - self.options.format, self.formatting_plugins['default'] + format_plugin, self.formatting_plugins['default'] ).execute + self.formatter = formatter_class(self.options) def make_notifier(self):