Skip to content

Commit 983d7f1

Browse files
committed
[WIP] pre-commit and ruff
1 parent 0564935 commit 983d7f1

File tree

751 files changed

+834
-1434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

751 files changed

+834
-1434
lines changed

.github/workflows/pre-commit.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Pre-commit Check
2+
3+
on: pull_request
4+
5+
jobs:
6+
pre-commit:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
with:
11+
fetch-depth: 0
12+
13+
- name: Set up Python
14+
uses: actions/setup-python@v5
15+
with:
16+
python-version: '3.x'
17+
18+
- name: Install pre-commit
19+
run: pip install pre-commit
20+
21+
- name: Get changed files
22+
id: changed-files
23+
run: |
24+
echo "files=$(git diff --name-only origin/main...HEAD | tr '\n' ' ')" >> $GITHUB_OUTPUT
25+
26+
- name: Run pre-commit
27+
continue-on-error: true
28+
run: |
29+
pre-commit run --files ${{ steps.changed-files.outputs.files }}

.pre-commit-config.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.8.4
5+
hooks:
6+
# Run the linter.
7+
- id: ruff
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format

bin/check_celery_worker_liveness.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
sys.exit(1)
6060

6161
print("Celery worker heartbeat found: OK.")
62-
sys.exit(0)
62+
sys.exit(0)

bootstrap.py

+97-69
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,36 @@
1313
Set up my development environment for me!
1414
"""
1515

16-
project_name = 'open_inwoner'
16+
project_name = "open_inwoner"
1717

1818
parser = argparse.ArgumentParser(description=description)
19-
parser.add_argument('target', choices=['production', 'staging', 'test', 'jenkins', 'dev'],
20-
help='production/staging/test/jenkins/dev')
21-
parser.add_argument('--project', default=project_name,
22-
help='Name of the project in your src directory, "%s" by default' % project_name)
23-
parser.add_argument('--init', action='store_true',
24-
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
25-
'Automatically done if requirements/base.txt does not yet exist.')
26-
parser.add_argument('--env', default='env',
27-
help='Directory name for virtualenv, "env" by default')
19+
parser.add_argument(
20+
"target",
21+
choices=["production", "staging", "test", "jenkins", "dev"],
22+
help="production/staging/test/jenkins/dev",
23+
)
24+
parser.add_argument(
25+
"--project",
26+
default=project_name,
27+
help='Name of the project in your src directory, "%s" by default' % project_name,
28+
)
29+
parser.add_argument(
30+
"--init",
31+
action="store_true",
32+
help='Initialize a fresh "startproject" by pinning the requirements using pip-tools compile. '
33+
"Automatically done if requirements/base.txt does not yet exist.",
34+
)
35+
parser.add_argument(
36+
"--env", default="env", help='Directory name for virtualenv, "env" by default'
37+
)
2838

2939
args = parser.parse_args()
3040

3141

3242
def replace_or_append(file_path, search_val, replace_val):
3343
file_handle, abs_path = mkstemp()
34-
new_file = open(abs_path, 'w')
35-
old_file = open(file_path, 'r')
44+
new_file = open(abs_path, "w")
45+
old_file = open(file_path)
3646
found = False
3747
for line in old_file:
3848
if line.startswith(search_val):
@@ -51,89 +61,107 @@ def replace_or_append(file_path, search_val, replace_val):
5161

5262

5363
def replace_wsgi_settings(target):
54-
path = os.path.join('src', project_name, 'wsgi.py')
64+
path = os.path.join("src", project_name, "wsgi.py")
5565
replace_or_append(
56-
path, 'os.environ.setdefault',
57-
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
66+
path,
67+
"os.environ.setdefault",
68+
'os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
69+
% (project_name, target),
5870
)
5971

6072

6173
def replace_manage_settings(target):
62-
path = os.path.join('src', project_name, 'manage.py')
74+
path = os.path.join("src", project_name, "manage.py")
6375
replace_or_append(
64-
path, ' os.environ.setdefault',
65-
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n' % (project_name, target)
76+
path,
77+
" os.environ.setdefault",
78+
' os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.conf.%s")\n'
79+
% (project_name, target),
6680
)
6781

6882

6983
def append_settings_activate(project, target, env):
70-
if os.name == 'posix':
71-
path = '%s/bin/activate' % env
72-
replace_or_append(path, 'export DJANGO_SETTINGS_MODULE=',
73-
'export DJANGO_SETTINGS_MODULE=\'%s.conf.%s\'\n' %
74-
(project, target))
75-
elif os.name == 'nt':
76-
path = '%s\\Scripts\\activate.bat' % env
77-
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
78-
'set DJANGO_SETTINGS_MODULE=%s.conf.%s\n' %
79-
(project, target))
80-
path = '%s\\Scripts\\deactivate.bat' % env
81-
replace_or_append(path, 'set DJANGO_SETTINGS_MODULE=',
82-
'set DJANGO_SETTINGS_MODULE=\n')
84+
if os.name == "posix":
85+
path = "%s/bin/activate" % env
86+
replace_or_append(
87+
path,
88+
"export DJANGO_SETTINGS_MODULE=",
89+
"export DJANGO_SETTINGS_MODULE='%s.conf.%s'\n" % (project, target),
90+
)
91+
elif os.name == "nt":
92+
path = "%s\\Scripts\\activate.bat" % env
93+
replace_or_append(
94+
path,
95+
"set DJANGO_SETTINGS_MODULE=",
96+
"set DJANGO_SETTINGS_MODULE=%s.conf.%s\n" % (project, target),
97+
)
98+
path = "%s\\Scripts\\deactivate.bat" % env
99+
replace_or_append(
100+
path, "set DJANGO_SETTINGS_MODULE=", "set DJANGO_SETTINGS_MODULE=\n"
101+
)
83102

84103

85104
def pip_compile_pin_requirements(virtualenv):
86-
print('\n== Compiling base requirements ==\n')
87-
if os.name == 'posix':
88-
pip_path = os.path.join(virtualenv, 'bin', 'pip')
89-
elif os.name == 'nt':
90-
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
91-
cmd_tpl = '{pip} install pip-tools'.format(pip=pip_path)
105+
print("\n== Compiling base requirements ==\n")
106+
if os.name == "posix":
107+
pip_path = os.path.join(virtualenv, "bin", "pip")
108+
elif os.name == "nt":
109+
pip_path = os.path.join(virtualenv, "Scripts", "pip")
110+
cmd_tpl = f"{pip_path} install pip-tools"
92111
call(cmd_tpl, shell=True)
93-
print('Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt')
94-
print('After that rerun bootstrap.py')
112+
print(
113+
"Error: Run `. env/bin/activate && ./bin/compile_dependencies.sh` to ensure you have requirements/base.txt and requirements/dev.txt"
114+
)
115+
print("After that rerun bootstrap.py")
95116
sys.exit(1)
96117

118+
97119
def main():
98120
virtualenv = args.env
99-
if not hasattr(sys, 'real_prefix'):
100-
print('\n== Creating virtual environment ==\n')
101-
call('virtualenv {0} --python=python3 --prompt="({1}-{2}) "'.format(
102-
virtualenv, args.project, args.target
103-
), shell=True)
104-
print('\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target))
121+
if not hasattr(sys, "real_prefix"):
122+
print("\n== Creating virtual environment ==\n")
123+
call(
124+
f'virtualenv {virtualenv} --python=python3 --prompt="({args.project}-{args.target}) "',
125+
shell=True,
126+
)
127+
print(
128+
'\n== Set "%s.conf.%s" as default settings ==\n' % (args.project, args.target)
129+
)
105130
append_settings_activate(args.project, args.target, args.env)
106131

107-
if os.name == 'posix':
132+
if os.name == "posix":
108133
# Make manage.py executable
109-
st = os.stat('src/manage.py')
110-
os.chmod('src/manage.py', st.st_mode | stat.S_IEXEC)
111-
django_admin_symlink = os.path.join(virtualenv, 'bin', 'django')
134+
st = os.stat("src/manage.py")
135+
os.chmod("src/manage.py", st.st_mode | stat.S_IEXEC)
136+
django_admin_symlink = os.path.join(virtualenv, "bin", "django")
112137
if not os.path.exists(django_admin_symlink):
113-
os.symlink('../../src/manage.py', django_admin_symlink)
114-
115-
print('\n== Upgrading Pip ==\n')
116-
if os.name == 'posix':
117-
pip_path = os.path.join(virtualenv, 'bin', 'pip')
118-
elif os.name == 'nt':
119-
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
120-
cmd_tpl = '{pip} install --upgrade pip'.format(pip=pip_path)
138+
os.symlink("../../src/manage.py", django_admin_symlink)
139+
140+
print("\n== Upgrading Pip ==\n")
141+
if os.name == "posix":
142+
pip_path = os.path.join(virtualenv, "bin", "pip")
143+
elif os.name == "nt":
144+
pip_path = os.path.join(virtualenv, "Scripts", "pip")
145+
cmd_tpl = f"{pip_path} install --upgrade pip"
121146
call(cmd_tpl, shell=True)
122147

123-
if args.init \
124-
or not os.path.exists(os.path.join('requirements', 'base.txt')) \
125-
or not os.path.exists(os.path.join('requirements', 'dev.txt')):
148+
if (
149+
args.init
150+
or not os.path.exists(os.path.join("requirements", "base.txt"))
151+
or not os.path.exists(os.path.join("requirements", "dev.txt"))
152+
):
126153
pip_compile_pin_requirements(virtualenv)
127154

128-
print('\n== Installing %s requirements ==\n' % args.target)
129-
if os.name == 'posix':
130-
os.environ['TMPDIR'] = '/var/tmp/'
131-
pip_path = os.path.join(virtualenv, 'bin', 'pip')
132-
cmd_tpl = '{pip} install -r requirements/{target}.txt'
133-
elif os.name == 'nt':
134-
pip_path = os.path.join(virtualenv, 'Scripts', 'pip')
135-
cmd_tpl = '{pip} install -r requirements\\{target}.txt'
155+
print("\n== Installing %s requirements ==\n" % args.target)
156+
if os.name == "posix":
157+
os.environ["TMPDIR"] = "/var/tmp/"
158+
pip_path = os.path.join(virtualenv, "bin", "pip")
159+
cmd_tpl = "{pip} install -r requirements/{target}.txt"
160+
elif os.name == "nt":
161+
pip_path = os.path.join(virtualenv, "Scripts", "pip")
162+
cmd_tpl = "{pip} install -r requirements\\{target}.txt"
136163
return call(cmd_tpl.format(pip=pip_path, target=args.target), shell=True)
137164

138-
if __name__ == '__main__':
165+
166+
if __name__ == "__main__":
139167
sys.exit(main())

ruff.toml

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Exclude a variety of commonly ignored directories.
2+
exclude = [
3+
".bzr",
4+
".direnv",
5+
".eggs",
6+
".git",
7+
".git-rewrite",
8+
".hg",
9+
".ipynb_checkpoints",
10+
".mypy_cache",
11+
".nox",
12+
".pants.d",
13+
".pyenv",
14+
".pytest_cache",
15+
".pytype",
16+
".ruff_cache",
17+
".svn",
18+
".tox",
19+
".venv",
20+
".vscode",
21+
"__pypackages__",
22+
"_build",
23+
"buck-out",
24+
"build",
25+
"dist",
26+
"node_modules",
27+
"site-packages",
28+
"venv",
29+
]
30+
31+
# Same as Black.
32+
line-length = 88
33+
indent-width = 4
34+
35+
# Assume Python 3.11
36+
target-version = "py311"
37+
38+
[lint]
39+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
40+
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
41+
# McCabe complexity (`C901`) by default.
42+
select = [
43+
# https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
44+
"B",
45+
"E",
46+
"F",
47+
"S",
48+
"I",
49+
"BLE",
50+
"C90",
51+
"S106",
52+
"UP",
53+
]
54+
ignore = ["E501"]
55+
56+
# Allow fix for all enabled rules (when `--fix`) is provided.
57+
fixable = ["ALL"]
58+
unfixable = []
59+
60+
# Allow unused variables when underscore-prefixed.
61+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
62+
63+
[format]
64+
# Like Black, use double quotes for strings.
65+
quote-style = "double"
66+
67+
# Like Black, indent with spaces, rather than tabs.
68+
indent-style = "space"
69+
70+
# Like Black, respect magic trailing commas.
71+
skip-magic-trailing-comma = false
72+
73+
# Like Black, automatically detect the appropriate line ending.
74+
line-ending = "auto"
75+
76+
# Enable auto-formatting of code examples in docstrings. Markdown,
77+
# reStructuredText code/literal blocks and doctests are all supported.
78+
#
79+
# This is currently disabled by default, but it is planned for this
80+
# to be opt-out in the future.
81+
docstring-code-format = false
82+
83+
# Set the line length limit used when formatting code snippets in
84+
# docstrings.
85+
#
86+
# This only has an effect when the `docstring-code-format` setting is
87+
# enabled.
88+
docstring-code-line-length = "dynamic"

src/custom_migrations/migrations/0001_delete_old_celery_tasks.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def delete_old_celery_tasks(apps, _):
2121

2222

2323
class Migration(migrations.Migration):
24-
2524
dependencies = [("django_celery_beat", "0018_improve_crontab_helptext")]
2625

2726
operations = [

src/eherkenning/backends.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from django.contrib.auth import get_user_model
2-
31
from digid_eherkenning.backends import eHerkenningBackend as _eHerkenningBackend
42
from digid_eherkenning.exceptions import eHerkenningError
53
from digid_eherkenning.utils import get_client_ip
4+
from django.contrib.auth import get_user_model
65

76
from open_inwoner.kvk.branches import KVK_BRANCH_SESSION_VARIABLE
87

src/eherkenning/mock/backends.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import logging
22
import re
33

4-
from django.contrib.auth import get_user_model
5-
64
from digid_eherkenning.backends import BaseBackend
75
from digid_eherkenning.utils import get_client_ip
6+
from django.contrib.auth import get_user_model
87

98
logger = logging.getLogger(__name__)
109

@@ -19,7 +18,7 @@ class eHerkenningBackend(BaseBackend):
1918
"eherkenning_no_kvk": "Login failed due to no KvK being returned by eHerkenning.",
2019
"eherkenning_len_kvk": "Login failed due to no KvK having more then 8 digits.",
2120
"eherkenning_num_kvk": "Login failed due to no KvK not being numerical.",
22-
}
21+
},
2322
)
2423

2524
def get_or_create_user(self, request, kvk):

0 commit comments

Comments
 (0)