From 81495fd859192462ee738564c24fb14b51ebbfa1 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 6 Feb 2016 15:22:36 -0600 Subject: [PATCH] Add default formatting class --- flake8/formatting/default.py | 32 ++++++++++++++++++++++++++++++++ flake8/main/cli.py | 2 +- setup.py | 3 +++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 flake8/formatting/default.py diff --git a/flake8/formatting/default.py b/flake8/formatting/default.py new file mode 100644 index 0000000..0b69d7a --- /dev/null +++ b/flake8/formatting/default.py @@ -0,0 +1,32 @@ +"""Default formatting class for Flake8.""" +from flake8.formatting import base + + +class Default(base.BaseFormatter): + """Default formatter for Flake8. + + This also handles backwards compatibility for people specifying a custom + format string. + """ + + error_format = '%(path)s:%(row)d:%(col)d: %(code)s %(text)s' + + def after_init(self): + """Check for a custom format string.""" + self.output_fd = None + if self.options.format.lower() != 'default': + self.error_format = self.options.format + + def format(self, error): + """Format and write error out. + + If an output filename is specified, write formatted errors to that + file. Otherwise, print the formatted error to standard out. + """ + return self.error_format % { + "code": error.code, + "text": error.text, + "path": error.filename, + "row": error.line_number, + "col": error.column_number, + } diff --git a/flake8/main/cli.py b/flake8/main/cli.py index 51071b5..e77be84 100644 --- a/flake8/main/cli.py +++ b/flake8/main/cli.py @@ -54,7 +54,7 @@ def register_default_options(option_manager): # TODO(sigmavirus24): Figure out --first/--repeat add_option( - '--format', metavar='format', default='default', choices=['default'], + '--format', metavar='format', default='default', parse_from_config=True, help='Format errors according to the chosen formatter.', ) diff --git a/setup.py b/setup.py index 00b5a68..281ed00 100644 --- a/setup.py +++ b/setup.py @@ -70,6 +70,9 @@ setuptools.setup( 'flake8.extension': [ 'F = flake8.plugins.pyflakes:FlakesChecker', ], + 'flake8.format': [ + 'default = flake8.formatting.default.Default', + ], }, classifiers=[ "Environment :: Console",