Skip to content

Commit bf97018

Browse files
committed
Refactor to simplify code.
1 parent ab3f5d5 commit bf97018

File tree

5 files changed

+51
-219
lines changed

5 files changed

+51
-219
lines changed

flake8_github_action/__init__.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626
# OR OTHER DEALINGS IN THE SOFTWARE.
2727
#
2828

29-
# stdlib
30-
import json
31-
from typing import List
29+
# 3rd party
30+
from typing_extensions import NoReturn
3231

3332
# this package
3433
from flake8_github_action.annotation import Annotation
35-
from flake8_github_action.checks import Checks
3634
from flake8_github_action.flake8_app import Application
3735

3836
__author__: str = "Dominic Davis-Foster"
@@ -44,9 +42,7 @@
4442
__all__ = ["action"]
4543

4644

47-
def action(
48-
*args,
49-
) -> int:
45+
def action(*args, ) -> NoReturn:
5046
r"""
5147
Action!
5248
@@ -56,23 +52,3 @@ def action(
5652
flake8_app = Application()
5753
flake8_app.run(args)
5854
flake8_app.exit()
59-
60-
annotations: List[Annotation] = []
61-
62-
json_annotations = json.loads(flake8_app.formatter.output_fd.getvalue()).items()
63-
for filename, raw_annotations in json_annotations:
64-
annotations.extend(Annotation.from_flake8json(filename, ann) for ann in raw_annotations)
65-
66-
if flake8_app.result_count:
67-
ret = 1
68-
else:
69-
ret = 0
70-
71-
if annotations:
72-
for annotation in annotations:
73-
print(annotation.to_str())
74-
75-
return ret
76-
77-
78-

flake8_github_action/__main__.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,27 @@
2828

2929
# stdlib
3030
import sys
31-
from typing import Union
3231

3332
# 3rd party
3433
import click
35-
from apeye import URL
36-
37-
__all__ = ["main"]
38-
39-
# 3rd party
4034
from consolekit import CONTEXT_SETTINGS
4135

42-
token_var = "GITHUB_TOKEN"
36+
__all__ = ["main"]
4337

4438

45-
@click.option(
46-
"--annotate-only",
47-
is_flag=True,
48-
default=False,
49-
help="Only add the annotations (exit 0 regardless of flake8 output).",
50-
)
5139
@click.command(context_settings={"ignore_unknown_options": True, "allow_extra_args": True, **CONTEXT_SETTINGS})
5240
@click.pass_context
53-
def main(ctx: click.Context, annotate_only: bool = False,):
41+
def main(ctx: click.Context):
5442
"""
5543
Run flake8 and add the errors as annotations on GitHub.
44+
45+
All options and arguments are passed through to flake8.
5646
"""
5747

5848
# this package
5949
from flake8_github_action import action
6050

61-
response, ret = action(*ctx.args)
62-
63-
# if response.status_code == 200:
64-
# sys.exit(0)
65-
66-
if annotate_only:
67-
sys.exit(0)
68-
else:
69-
sys.exit(ret)
51+
action(*ctx.args)
7052

7153

7254
if __name__ == "__main__":

flake8_github_action/checks.py

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

flake8_github_action/flake8_app.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@
3232
#
3333

3434
# stdlib
35+
import json
3536
from functools import partial
3637
from gettext import ngettext
3738
from io import StringIO
38-
from typing import Optional, Tuple, Type
39+
from typing import List, Optional, Tuple, Type
3940

4041
# 3rd party
4142
import click
4243
import flake8.main.application # type: ignore
4344
from flake8.formatting.base import BaseFormatter # type: ignore
4445
from flake8_json_reporter.reporters import DefaultJSON # type: ignore
46+
from typing_extensions import NoReturn
47+
48+
# this package
49+
from flake8_github_action.annotation import Annotation
4550

4651
__all__ = ["Application", "JsonFormatter"]
4752

@@ -54,31 +59,31 @@ class Application(flake8.main.application.Application):
5459
Subclass of Flake8's ``Application``.
5560
"""
5661

57-
def exit(self) -> None:
62+
def exit(self) -> NoReturn:
5863
"""
5964
Handle finalization and exiting the program.
6065
6166
This should be the last thing called on the application instance.
6267
It will check certain options and exit appropriately.
6368
"""
6469

65-
# if self.options.count:
66-
if True:
67-
files_checked = self.file_checker_manager.statistics["files"]
68-
files_with_errors = self.file_checker_manager.statistics["files_with_errors"]
69-
if self.result_count:
70-
click.echo(
71-
f"Found {self.result_count} {_error(self.result_count)} "
72-
f"in {files_with_errors} {_file(files_with_errors)} "
73-
f"(checked {files_checked} source {_file(files_checked)})"
74-
)
75-
else:
76-
click.echo(f"Success: no issues found in {files_checked} source {_file(files_checked)}")
77-
78-
# if self.options.exit_zero:
79-
# raise SystemExit(self.catastrophic_failure)
80-
# else:
81-
# raise SystemExit((self.result_count > 0) or self.catastrophic_failure)
70+
if self.options.count:
71+
if True:
72+
files_checked = self.file_checker_manager.statistics["files"]
73+
files_with_errors = self.file_checker_manager.statistics["files_with_errors"]
74+
if self.result_count:
75+
click.echo(
76+
f"Found {self.result_count} {_error(self.result_count)} "
77+
f"in {files_with_errors} {_file(files_with_errors)} "
78+
f"(checked {files_checked} source {_file(files_checked)})"
79+
)
80+
else:
81+
click.echo(f"Success: no issues found in {files_checked} source {_file(files_checked)}")
82+
83+
if self.options.exit_zero:
84+
raise SystemExit(self.catastrophic_failure)
85+
else:
86+
raise SystemExit((self.result_count > 0) or self.catastrophic_failure)
8287

8388
def report_errors(self) -> None:
8489
"""
@@ -122,10 +127,29 @@ def make_formatter(self, formatter_class: Optional[Type[BaseFormatter]] = None)
122127

123128
self.formatter = JsonFormatter(self.options)
124129

130+
def report(self):
131+
"""
132+
Report errors, statistics, and benchmarks.
133+
"""
134+
135+
super().report()
136+
137+
annotations: List[Annotation] = []
138+
139+
json_annotations = json.loads(self.formatter.output_fd.getvalue()).items()
140+
for filename, raw_annotations in json_annotations:
141+
annotations.extend(Annotation.from_flake8json(filename, ann) for ann in raw_annotations)
142+
143+
for annotation in annotations:
144+
print(annotation.to_str())
145+
125146

126147
class JsonFormatter(DefaultJSON):
127148

128149
def __init__(self, *args, **kwargs):
129150
super().__init__(*args, **kwargs)
130151

131152
self.output_fd = StringIO()
153+
154+
155+

requirements.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
apeye>=0.4.0
21
attrs>=20.3.0
32
click>=7.1.2
43
consolekit>=0.6.0
5-
domdf-python-tools>=1.7.0
6-
dulwich>=0.20.13
74
flake8>=3.8.4
85
flake8-json>=19.8.0
9-
requests>=2.25.0
106
typing-extensions>=3.7.4.3

0 commit comments

Comments
 (0)