Skip to content

Commit fad6c58

Browse files
committed
Add bolt-code with ruff
1 parent d150fab commit fad6c58

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

bolt-code/.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Local development files
2+
/.env
3+
/.bolt
4+
*.sqlite3
5+
6+
# Publishing
7+
/dist
8+
9+
# Python
10+
/.venv
11+
__pycache__/
12+
*.py[cod]
13+
*$py.class
14+
15+
# OS files
16+
.DS_Store

bolt-code/bolt/code/__init__.py

Whitespace-only changes.

bolt-code/bolt/code/cli.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import re
2+
import subprocess
3+
import sys
4+
from pathlib import Path
5+
6+
import click
7+
8+
DEFAULT_RUFF_CONFIG = Path(__file__).parent / "ruff_defaults.toml"
9+
10+
11+
@click.group()
12+
def cli():
13+
"""Standard code formatting and linting."""
14+
pass
15+
16+
17+
@cli.command()
18+
@click.argument("path", default=".")
19+
@click.option("--fix/--no-fix", "do_fix", default=False)
20+
def lint(path, do_fix):
21+
ruff_args = []
22+
23+
if not user_has_ruff_config():
24+
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
25+
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])
26+
27+
if do_fix:
28+
ruff_args.append("--fix")
29+
30+
click.secho("Ruff check", bold=True)
31+
result = subprocess.run(["ruff", "check", path, *ruff_args])
32+
33+
if result.returncode != 0:
34+
sys.exit(result.returncode)
35+
36+
37+
@cli.command()
38+
@click.argument("path", default=".")
39+
def format(path):
40+
ruff_args = []
41+
42+
if not user_has_ruff_config():
43+
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
44+
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])
45+
46+
click.secho("Ruff format", bold=True)
47+
result = subprocess.run(["ruff", "format", path, *ruff_args])
48+
49+
if result.returncode != 0:
50+
sys.exit(result.returncode)
51+
52+
53+
@cli.command()
54+
@click.argument("path", default=".")
55+
def fix(path):
56+
"""Lint and format the given path."""
57+
ruff_args = []
58+
59+
if not user_has_ruff_config():
60+
click.secho("Using default bolt.code ruff config", italic=True, bold=True)
61+
ruff_args.extend(["--config", str(DEFAULT_RUFF_CONFIG)])
62+
63+
click.secho("Ruff check", bold=True)
64+
result = subprocess.run(["ruff", "check", path, "--fix", *ruff_args])
65+
66+
if result.returncode != 0:
67+
sys.exit(result.returncode)
68+
69+
click.secho("Ruff format", bold=True)
70+
result = subprocess.run(["ruff", "format", path, *ruff_args])
71+
72+
if result.returncode != 0:
73+
sys.exit(result.returncode)
74+
75+
76+
def user_has_ruff_config():
77+
try:
78+
output = subprocess.check_output(["ruff", "check", ".", "--show-settings"])
79+
except subprocess.CalledProcessError:
80+
return False
81+
82+
if re.search("Settings path: (.+)", output.decode("utf-8")):
83+
return True
84+
else:
85+
return False
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ignore = [
2+
"E501", # Never enforce `E501` (line length violations)
3+
"S101", # pytest use of assert
4+
"ISC001", # Implicit string concatenation
5+
]
6+
extend-select = [
7+
"I", # isort
8+
# # "C90", # mccabe
9+
# # "N", # pep8-naming
10+
"UP", # pyupgrade
11+
# "S", # bandit
12+
# # "B", # bugbear
13+
"C4", # flake8-comprehensions
14+
# # "DTZ", # flake8-datetimez
15+
"ISC", # flake8-implicit-str-concat
16+
# # "G", # flake8-logging-format
17+
# # "T20", # print
18+
"PT", # pytest
19+
]

bolt-code/pyproject.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[tool.poetry]
2+
name = "bolt-code"
3+
packages = [
4+
{ include = "bolt" },
5+
]
6+
7+
version = "0.1.0"
8+
description = ""
9+
authors = ["Dave Gaeddert <[email protected]>"]
10+
11+
# Make the CLI available without adding to INSTALLED_APPS
12+
[tool.poetry.plugins."bolt.cli"]
13+
"code" = "bolt.code:cli"
14+
"fix" = "bolt.code.cli:fix"
15+
16+
[tool.poetry.dependencies]
17+
python = "^3.8"
18+
ruff = "^0.1.0"
19+
20+
[tool.poetry.dev-dependencies]
21+
pytest = "^7.1.2"
22+
ipdb = "^0.13.9"
23+
isort = "^5.10.1"
24+
black = "^23.1.0"
25+
pytest-django = "^4.5.2"
26+
27+
[build-system]
28+
requires = ["poetry-core>=1.0.0"]
29+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)