From ea85e1736c6a0446e5c59d61de18d160cbe1d5fc Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2024 20:56:14 +0200 Subject: [PATCH] Create ruff_exclude.py --- scripts/ruff_exclude.py | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 scripts/ruff_exclude.py diff --git a/scripts/ruff_exclude.py b/scripts/ruff_exclude.py new file mode 100644 index 0000000..c2fef90 --- /dev/null +++ b/scripts/ruff_exclude.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +# ruff: noqa: T201 + +"""Usage: ruff check --select=ALL --statistics | ./ruff_exclude.py .""" + +from __future__ import annotations + +from string import digits +from subprocess import run +from sys import stdin + + +def quoted(s: str) -> str: + """Return a double-quoted string. + + >>> quoted("s") + '"s"' + >>> quoted('s') + '"s"' + >>> quoted(None) + '"None"' + """ + return f'"{s}"' + + +def ruff_linters() -> dict[str, str]: + """Call `ruff linter` and return a dict of {rule_family: description}.""" + lines = run( # noqa: S603 + ["ruff", "linter"], # noqa: S607 + capture_output=True, + check=True, + text=True, + ).stdout.splitlines() + linters = { + key: value for line in lines for key, value in [line.strip().split(" ", 1)] + } + e_w = linters.pop("E/W") + linters["E"] = f"{e_w} errors" + linters["W"] = f"{e_w} warnings" + pylint = linters.pop("PL") + linters["PLC"] = f"{pylint} conventions" + linters["PLE"] = f"{pylint} errors" + linters["PLR"] = f"{pylint} refactorings" + linters["PLW"] = f"{pylint} warnings" + return linters + + +if __name__ == "__main__": + violations = {line.split()[1].rstrip(digits) for line in stdin} + lines = [ + f' {"#" if key in violations else " "} {quoted(key):<7} # {value}' + for key, value in ruff_linters().items() + ] + print("[tools.ruff.lint]\nexclude = [") + print("\n".join(sorted(lines))) + print("]")