|
| 1 | +#!/usr/bin/env -S uv run --script |
| 2 | + |
| 3 | +# /// script |
| 4 | +# dependencies = ["ruff", "rich"] |
| 5 | +# /// |
| 6 | + |
| 7 | + |
| 8 | +import argparse |
| 9 | +import json |
| 10 | +import subprocess |
| 11 | +from collections.abc import Iterator |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import tomllib |
| 15 | +from rich import print |
| 16 | +from rich.columns import Columns |
| 17 | +from rich.panel import Panel |
| 18 | +from ruff.__main__ import find_ruff_bin |
| 19 | + |
| 20 | +libs = {"AIR", "ASYNC", "DJ", "FAST", "INT", "NPY", "PD"} |
| 21 | +specialty = { |
| 22 | + "CPY", # preview only |
| 23 | + "A", # Naming related |
| 24 | + "N", # Naming related |
| 25 | + "ANN", # Not all rules good |
| 26 | + "TID", # Not all rules good |
| 27 | + "C90", # Complexity |
| 28 | + "COM", # Trailing commas inform the formatter |
| 29 | + "D", # Requires everything documented |
| 30 | + "DOC", # Style-specific |
| 31 | + "ERA", # Check for commented code |
| 32 | + "FBT", # Can't be applied to old code very well |
| 33 | + "FIX", # TODO's are okay |
| 34 | + "TD", # Picky on todo rules |
| 35 | + "INP", # Namespace packages are correct sometimes |
| 36 | + "S", # Security (can be picky) |
| 37 | + "SLF", # Very picky |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def process_dir(path: Path) -> None: |
| 42 | + with path.joinpath("pyproject.toml").open("rb") as f: |
| 43 | + pyproject = tomllib.load(f) |
| 44 | + |
| 45 | + match pyproject: |
| 46 | + case {"tool": {"ruff": {"lint": {"extend-select": selection}}}}: |
| 47 | + selected = frozenset(selection) |
| 48 | + case _: |
| 49 | + raise SystemExit(1) |
| 50 | + |
| 51 | + linter_txt = subprocess.run( |
| 52 | + [find_ruff_bin(), "linter", "--output-format=json"], |
| 53 | + check=True, |
| 54 | + capture_output=True, |
| 55 | + text=True, |
| 56 | + cwd=path, |
| 57 | + ).stdout |
| 58 | + linter = json.loads(linter_txt) |
| 59 | + |
| 60 | + lint_info = {r["prefix"]: r["name"] for r in linter if r["prefix"] not in {"", "F"}} |
| 61 | + |
| 62 | + selected_items = {k: v for k, v in lint_info.items() if k in selected} |
| 63 | + all_uns_items = {k: v for k, v in lint_info.items() if k not in selected} |
| 64 | + unselected_items = { |
| 65 | + k: v for k, v in all_uns_items.items() if k not in libs | specialty |
| 66 | + } |
| 67 | + libs_items = {k: v for k, v in all_uns_items.items() if k in libs} |
| 68 | + spec_items = {k: v for k, v in all_uns_items.items() if k in specialty} |
| 69 | + |
| 70 | + def print_each(items: dict[str, str]) -> Iterator[str]: |
| 71 | + for k, v in items.items(): |
| 72 | + kk = f'[green]"{k}"[/green],' |
| 73 | + yield f" {kk:23} [dim]# {v}[/dim]" |
| 74 | + |
| 75 | + panel_sel = Panel( |
| 76 | + "\n".join(print_each(selected_items)), title="Selected", border_style="green" |
| 77 | + ) |
| 78 | + panel_lib = Panel( |
| 79 | + "\n".join(print_each(libs_items)), |
| 80 | + title="Library specific", |
| 81 | + border_style="yellow", |
| 82 | + ) |
| 83 | + panel_spec = Panel( |
| 84 | + "\n".join(print_each(spec_items)), title="Specialized", border_style="yellow" |
| 85 | + ) |
| 86 | + uns = "\n".join(print_each(unselected_items)) |
| 87 | + |
| 88 | + if uns: |
| 89 | + print(Columns([panel_sel, panel_lib, panel_spec])) |
| 90 | + print("[red]Unselected [dim](copy and paste ready)") |
| 91 | + print(uns) |
| 92 | + |
| 93 | + |
| 94 | +def main() -> None: |
| 95 | + parser = argparse.ArgumentParser(description="Look up Ruff rules in a directory") |
| 96 | + parser.add_argument( |
| 97 | + "path", |
| 98 | + nargs="?", |
| 99 | + type=Path, |
| 100 | + default=Path.cwd(), |
| 101 | + help="Directory to process (default: current working directory)", |
| 102 | + ) |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + process_dir(args.path) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments