File: gen-pycodestyle-plugin

package info (click to toggle)
python-flake8 7.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,224 kB
  • sloc: python: 6,634; sh: 21; makefile: 19
file content (98 lines) | stat: -rwxr-xr-x 2,691 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
from __future__ import annotations

import inspect
import os.path
from collections.abc import Generator
from typing import Any
from typing import Callable
from typing import NamedTuple

import pycodestyle


def _too_long(s: str) -> str:
    if len(s) >= 80:
        return f"{s}  # noqa: E501"
    else:
        return s


class Call(NamedTuple):
    name: str
    is_generator: bool
    params: tuple[str, ...]

    def to_src(self) -> str:
        params_s = ", ".join(self.params)
        if self.is_generator:
            return _too_long(f"    yield from _{self.name}({params_s})")
        else:
            lines = (
                _too_long(f"    ret = _{self.name}({params_s})"),
                "    if ret is not None:",
                "        yield ret",
            )
            return "\n".join(lines)

    @classmethod
    def from_func(cls, func: Callable[..., Any]) -> Call:
        spec = inspect.getfullargspec(func)
        params = tuple(spec.args)
        return cls(func.__name__, inspect.isgeneratorfunction(func), params)


def lines() -> Generator[str]:
    logical = []
    physical = []

    logical = [
        Call.from_func(check) for check in pycodestyle._checks["logical_line"]
    ]
    physical = [
        Call.from_func(check) for check in pycodestyle._checks["physical_line"]
    ]
    assert not pycodestyle._checks["tree"]

    yield f'"""Generated using ./bin/{os.path.basename(__file__)}."""'
    yield "# fmt: off"
    yield "from __future__ import annotations"
    yield ""
    yield "from collections.abc import Generator"
    yield "from typing import Any"
    yield ""
    imports = sorted(call.name for call in logical + physical)
    for name in imports:
        yield _too_long(f"from pycodestyle import {name} as _{name}")
    yield ""
    yield ""

    yield "def pycodestyle_logical("
    logical_params = {param for call in logical for param in call.params}
    for param in sorted(logical_params):
        yield f"    {param}: Any,"
    yield ") -> Generator[tuple[int, str]]:"
    yield '    """Run pycodestyle logical checks."""'
    for call in sorted(logical):
        yield call.to_src()
    yield ""
    yield ""

    yield "def pycodestyle_physical("
    physical_params = {param for call in physical for param in call.params}
    for param in sorted(physical_params):
        yield f"    {param}: Any,"
    yield ") -> Generator[tuple[int, str]]:"
    yield '    """Run pycodestyle physical checks."""'
    for call in sorted(physical):
        yield call.to_src()


def main() -> int:
    for line in lines():
        print(line)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())