From 37500a3ad030392ff636c0c28385863cbff19d27 Mon Sep 17 00:00:00 2001 From: Dave Gaeddert Date: Tue, 6 Feb 2024 11:56:58 -0600 Subject: [PATCH] Always use specific config in bolt-code --- bolt-code/bolt/code/cli.py | 32 +++++++++++--------------- bolt-code/bolt/code/ruff_defaults.toml | 1 + pyproject.toml | 3 +++ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/bolt-code/bolt/code/cli.py b/bolt-code/bolt/code/cli.py index fe4bb4d07d..28d95600d7 100644 --- a/bolt-code/bolt/code/cli.py +++ b/bolt-code/bolt/code/cli.py @@ -1,9 +1,9 @@ -import re import subprocess import sys from pathlib import Path import click +import tomllib from bolt.cli.print import print_event @@ -20,11 +20,10 @@ def cli(): @click.argument("path", default=".") def check(path): """Check the given path for formatting or linting issues.""" - ruff_args = [] + ruff_args = ["--config", str(DEFAULT_RUFF_CONFIG)] - if not user_has_ruff_config(): - click.secho("Using default bolt.code ruff config", italic=True) - ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)]) + for e in get_code_config().get("exclude", []): + ruff_args.extend(["--exclude", e]) print_event("Ruff check") result = subprocess.run(["ruff", "check", path, *ruff_args]) @@ -43,11 +42,10 @@ def check(path): @click.argument("path", default=".") def fix(path): """Lint and format the given path.""" - ruff_args = [] + ruff_args = ["--config", str(DEFAULT_RUFF_CONFIG)] - if not user_has_ruff_config(): - click.secho("Using default bolt.code ruff config", italic=True) - ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)]) + for e in get_code_config().get("exclude", []): + ruff_args.extend(["--exclude", e]) print_event("Ruff check") result = subprocess.run(["ruff", "check", path, "--fix", *ruff_args]) @@ -62,13 +60,9 @@ def fix(path): sys.exit(result.returncode) -def user_has_ruff_config(): - try: - output = subprocess.check_output(["ruff", "check", ".", "--show-settings"]) - except subprocess.CalledProcessError: - return False - - if re.search("Settings path: (.+)", output.decode("utf-8")): - return True - else: - return False +def get_code_config(): + pyproject = Path("pyproject.toml") + if not pyproject.exists(): + return {} + with pyproject.open("rb") as f: + return tomllib.load(f).get("tool", {}).get("bolt", {}).get("code", {}) diff --git a/bolt-code/bolt/code/ruff_defaults.toml b/bolt-code/bolt/code/ruff_defaults.toml index 3720b19245..3d2de70d37 100644 --- a/bolt-code/bolt/code/ruff_defaults.toml +++ b/bolt-code/bolt/code/ruff_defaults.toml @@ -1,3 +1,4 @@ +target-version = "py310" ignore = [ "E501", # Never enforce `E501` (line length violations) "S101", # pytest use of assert diff --git a/pyproject.toml b/pyproject.toml index 2ee88d40c1..0f577a97eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,3 +32,6 @@ build-backend = "poetry.core.masonry.api" extend = "bolt-code/bolt/code/ruff_defaults.toml" target-version = "py310" extend-exclude = ["bolt-pytest/bolt/pytest/pytest"] + +[tool.bolt.code] +exclude = ["bolt-pytest/bolt/pytest/pytest"]