Skip to content

Commit

Permalink
Add bolt-code with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 31, 2024
1 parent d150fab commit fad6c58
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bolt-code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Local development files
/.env
/.bolt
*.sqlite3

# Publishing
/dist

# Python
/.venv
__pycache__/
*.py[cod]
*$py.class

# OS files
.DS_Store
Empty file added bolt-code/bolt/code/__init__.py
Empty file.
85 changes: 85 additions & 0 deletions bolt-code/bolt/code/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import re
import subprocess
import sys
from pathlib import Path

import click

DEFAULT_RUFF_CONFIG = Path(__file__).parent / "ruff_defaults.toml"


@click.group()
def cli():
"""Standard code formatting and linting."""
pass


@cli.command()
@click.argument("path", default=".")
@click.option("--fix/--no-fix", "do_fix", default=False)
def lint(path, do_fix):
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

if do_fix:
ruff_args.append("--fix")

click.secho("Ruff check", bold=True)
result = subprocess.run(["ruff", "check", path, *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)


@cli.command()
@click.argument("path", default=".")
def format(path):
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

click.secho("Ruff format", bold=True)
result = subprocess.run(["ruff", "format", path, *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)


@cli.command()
@click.argument("path", default=".")
def fix(path):
"""Lint and format the given path."""
ruff_args = []

if not user_has_ruff_config():
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])

click.secho("Ruff check", bold=True)
result = subprocess.run(["ruff", "check", path, "--fix", *ruff_args])

if result.returncode != 0:
sys.exit(result.returncode)

click.secho("Ruff format", bold=True)
result = subprocess.run(["ruff", "format", path, *ruff_args])

if result.returncode != 0:
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
19 changes: 19 additions & 0 deletions bolt-code/bolt/code/ruff_defaults.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ignore = [
"E501", # Never enforce `E501` (line length violations)
"S101", # pytest use of assert
"ISC001", # Implicit string concatenation
]
extend-select = [
"I", # isort
# # "C90", # mccabe
# # "N", # pep8-naming
"UP", # pyupgrade
# "S", # bandit
# # "B", # bugbear
"C4", # flake8-comprehensions
# # "DTZ", # flake8-datetimez
"ISC", # flake8-implicit-str-concat
# # "G", # flake8-logging-format
# # "T20", # print
"PT", # pytest
]
29 changes: 29 additions & 0 deletions bolt-code/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[tool.poetry]
name = "bolt-code"
packages = [
{ include = "bolt" },
]

version = "0.1.0"
description = ""
authors = ["Dave Gaeddert <dave.gaeddert@dropseed.dev>"]

# Make the CLI available without adding to INSTALLED_APPS
[tool.poetry.plugins."bolt.cli"]
"code" = "bolt.code:cli"
"fix" = "bolt.code.cli:fix"

[tool.poetry.dependencies]
python = "^3.8"
ruff = "^0.1.0"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
ipdb = "^0.13.9"
isort = "^5.10.1"
black = "^23.1.0"
pytest-django = "^4.5.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit fad6c58

Please sign in to comment.