Skip to content

Commit f083416

Browse files
committed
Add code.
1 parent d8da3a9 commit f083416

File tree

7 files changed

+99
-5
lines changed

7 files changed

+99
-5
lines changed

doc-source/api/flake8-helper.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ flake8_helper
33
==============
44

55
.. automodule:: flake8_helper
6+
:member-order: bysource

doc-source/contributing.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
==============
2-
Contributing
3-
==============
1+
Overview
2+
---------
43

54
.. This file based on https://github.com/PyGithub/PyGithub/blob/master/CONTRIBUTING.md
65

flake8_helper/__init__.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,99 @@
2626
# OR OTHER DEALINGS IN THE SOFTWARE.
2727
#
2828

29+
# stdlib
30+
import ast
31+
from abc import ABC, abstractmethod
32+
from typing import Iterator, List, Tuple, Type, TypeVar
33+
34+
__all__ = ["Visitor", "_P", "Plugin"]
35+
2936
__author__: str = "Dominic Davis-Foster"
3037
__copyright__: str = "2021 Dominic Davis-Foster"
3138
__license__: str = "MIT License"
3239
__version__: str = "0.0.0"
3340
__email__: str = "[email protected]"
41+
42+
_P = TypeVar("_P", bound="Plugin")
43+
44+
45+
class Visitor(ast.NodeVisitor):
46+
"""
47+
AST node visitor.
48+
"""
49+
50+
def __init__(self) -> None:
51+
#: The list of Flake8 errors identified by the visitor.
52+
self.errors: List[Tuple[int, int, str]] = []
53+
54+
def report_error(self, node: ast.AST, error: str):
55+
"""
56+
Report an error for the given node.
57+
58+
:param node:
59+
:param error:
60+
"""
61+
62+
self.errors.append((
63+
node.lineno,
64+
node.col_offset,
65+
error,
66+
))
67+
68+
69+
class Plugin(ABC):
70+
"""
71+
Abstract base class for Flake8 plugins.
72+
73+
:param tree: The abstract syntax tree (AST) to check.
74+
75+
**Minimum example:**
76+
77+
.. code=block:: python
78+
79+
class EncodingsPlugin(Plugin):
80+
'''
81+
A Flake8 plugin to identify incorrect use of encodings.
82+
83+
:param tree: The abstract syntax tree (AST) to check.
84+
'''
85+
86+
name: str = __name__
87+
version: str = __version__ #: The plugin version
88+
"""
89+
90+
def __init__(self, tree: ast.AST):
91+
92+
#: The abstract syntax tree (AST) being checked.
93+
self._tree = tree
94+
95+
@property
96+
@abstractmethod
97+
def name(self) -> str:
98+
"""
99+
The plugin name.
100+
"""
101+
102+
raise NotImplementedError
103+
104+
@property
105+
@abstractmethod
106+
def version(self) -> str:
107+
"""
108+
The plugin version.
109+
"""
110+
111+
raise NotImplementedError
112+
113+
def run(self: _P) -> Iterator[Tuple[int, int, str, Type[_P]]]:
114+
"""
115+
Traverse the Abstract Syntax Tree and identify errors.
116+
117+
Yields a tuple of (line number, column offset, error message, type(self))
118+
"""
119+
120+
visitor = Visitor()
121+
visitor.visit(self._tree)
122+
123+
for line, col, msg in visitor.errors:
124+
yield line, col, msg, type(self)

formate.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ lines_between_types = 0
3131
use_parentheses = true
3232
remove_redundant_aliases = true
3333
default_section = "THIRDPARTY"
34-
known_third_party = []
34+
known_third_party = [ "flake8",]
3535
known_first_party = "flake8_helper"

repo_helper.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ extra_sphinx_extensions:
3636
- sphinx_toolbox.flake8
3737

3838
sphinx_html_theme: furo
39-
standalone_contrib_guide: true
4039

4140
keywords:
4241
- flake8

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flake8>=3.8.4

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ package = flake8_helper
116116
[pytest]
117117
addopts = --color yes --durations 25
118118
timeout = 300
119+
120+
[dep_checker]
121+
allowed_unused = flake8

0 commit comments

Comments
 (0)