Skip to content

Commit ab2d039

Browse files
committed
Move check formatting into custom Formatter
1 parent dca1675 commit ab2d039

File tree

5 files changed

+42
-144
lines changed

5 files changed

+42
-144
lines changed

flake8_github_action/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from typing_extensions import NoReturn
3131

3232
# this package
33-
from flake8_github_action.annotation import Annotation
3433
from flake8_github_action.flake8_app import Application
3534

3635
__author__: str = "Dominic Davis-Foster"

flake8_github_action/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ def main(ctx: click.Context):
5353

5454
if __name__ == "__main__":
5555
sys.exit(main(obj={}))
56+
57+
58+
59+

flake8_github_action/annotation.py

Lines changed: 0 additions & 117 deletions
This file was deleted.

flake8_github_action/flake8_app.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,23 @@
3030
# Copyright (C) 2012-2016 Ian Cordasco <graffatcolmingov@gmail.com>
3131
# MIT Licensed
3232
#
33+
# "GitHubFormatter" based on https://gitlab.com/pycqa/flake8-json
34+
# Copyright (C) 2017-2018 Ian Stapleton Cordasco <graffatcolmingov@gmail.com>
35+
# MIT Licensed
36+
#
3337

3438
# stdlib
35-
import json
3639
from functools import partial
3740
from gettext import ngettext
38-
from io import StringIO
39-
from typing import List, Optional, Tuple, Type
41+
from typing import Optional, Tuple, Type
4042

4143
# 3rd party
4244
import click
4345
import flake8.main.application # type: ignore
4446
from flake8.formatting.base import BaseFormatter # type: ignore
45-
from flake8_json_reporter.reporters import DefaultJSON # type: ignore
4647
from typing_extensions import NoReturn
4748

48-
# this package
49-
from flake8_github_action.annotation import Annotation
50-
51-
__all__ = ["Application", "JsonFormatter"]
49+
__all__ = ["Application", "GitHubFormatter"]
5250

5351
_error = partial(ngettext, "error", "errors")
5452
_file = partial(ngettext, "file", "files")
@@ -125,31 +123,47 @@ def make_formatter(self, formatter_class: Optional[Type[BaseFormatter]] = None)
125123
Initialize a formatter based on the parsed options.
126124
"""
127125

128-
self.formatter = JsonFormatter(self.options)
126+
self.formatter = GitHubFormatter(self.options)
129127

130-
def report(self):
131-
"""
132-
Report errors, statistics, and benchmarks.
133-
"""
134128

135-
super().report()
129+
class GitHubFormatter(BaseFormatter):
136130

137-
json_annotations = json.loads(self.formatter.output_fd.getvalue()).items()
131+
def write_line(self, line):
132+
"""
133+
Override write for convenience.
134+
"""
135+
self.write(line, None)
138136

139-
for filename, raw_annotations in json_annotations:
140-
if raw_annotations:
141-
print(filename)
137+
def start(self):
138+
super().start()
139+
self.files_reported_count = 0
142140

143-
for annotation in raw_annotations:
144-
print(Annotation.from_flake8json(filename, annotation).to_str())
141+
def beginning(self, filename):
142+
"""
143+
We're starting a new file.
144+
"""
145145

146+
self.reported_errors_count = 0
146147

147-
class JsonFormatter(DefaultJSON):
148+
def finished(self, filename):
149+
"""
150+
We've finished processing a file.
151+
"""
148152

149-
def __init__(self, *args, **kwargs):
150-
super().__init__(*args, **kwargs)
153+
self.files_reported_count += 1
151154

152-
self.output_fd = StringIO()
155+
def format(self, violation):
156+
"""
157+
Format a violation.
158+
"""
153159

160+
if self.reported_errors_count == 0:
161+
self.write_line(violation.filename)
154162

163+
self.write_line(
164+
f"::warning "
165+
f"file={violation.filename},line={violation.line_number},col={violation.column_number}"
166+
f"::{violation.code}: {violation.text}"
167+
)
155168

169+
self.reported_errors_count += 1

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
attrs>=20.3.0
21
click>=7.1.2
32
consolekit>=0.6.0
43
flake8>=3.8.4
5-
flake8-json>=19.8.0
64
typing-extensions>=3.7.4.3

0 commit comments

Comments
 (0)