forked from wemake-services/flake8-eradicate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake8_eradicate.py
61 lines (43 loc) · 1.69 KB
/
flake8_eradicate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
from io import StringIO
from typing import Generator, Tuple
import attr
import pkg_resources
from eradicate import fix_file
pkg_name = 'flake8-eradicate'
#: We store the version number inside the `pyproject.toml`:
pkg_version: str = pkg_resources.get_distribution(pkg_name).version
STDIN = 'stdin'
@attr.attrs(frozen=True, auto_attribs=True, slots=True)
class _Options(object):
"""Represents ``eradicate`` option object."""
aggressive: bool = False
in_place: bool = False
class Checker(object):
"""Flake8 plugin to find commented out code."""
name = pkg_name
version = pkg_version
_error_template = 'E800: Found commented out code:\n{0}'
def __init__(self, tree, filename: str = STDIN) -> None:
"""
Creates new checker instance.
We only need a filename, since ``eradicate`` has its own logic to read
file contents. And it is unhandy to pass it.
But, without ``tree`` argument ``flake8`` does not call this plugin.
When performance will be an issue - we can refactor it.
"""
self.filename = filename
def _error(self, traceback: str) -> str:
return self._error_template.format(traceback)
def run(self) -> Generator[Tuple[int, int, str, type], None, None]:
"""
Runs the checker.
``fix_file()`` only mutates the buffer object.
It is the only way to find out if some error happened.
"""
if self.filename != STDIN:
buffer = StringIO()
fix_file(self.filename, _Options(), buffer)
traceback = buffer.getvalue()
if traceback:
yield 1, 0, self._error(traceback), type(self)