Skip to content

Commit ad4d48e

Browse files
committed
Add linting
1 parent 94841da commit ad4d48e

File tree

13 files changed

+180
-71
lines changed

13 files changed

+180
-71
lines changed

.github/workflows/lint.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: lint
2+
3+
on:
4+
push:
5+
branches:
6+
- "*"
7+
pull_request:
8+
branches:
9+
- "*"
10+
11+
jobs:
12+
pre-commit:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: actions/setup-python@v3
17+
- uses: pre-commit/[email protected]

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ celerybeat-schedule
8686
.venv
8787
venv/
8888
ENV/
89+
.direnv/
90+
.envrc
8991

9092
# Spyder project settings
9193
.spyderproject

.pre-commit-config.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
default_language_version:
2+
python: python3.10
3+
repos:
4+
- repo: https://github.com/psf/black
5+
rev: 22.3.0
6+
hooks:
7+
- id: black
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v4.1.0
10+
hooks:
11+
- id: check-ast
12+
- id: check-merge-conflict
13+
- id: check-case-conflict
14+
- id: detect-private-key
15+
- id: check-added-large-files
16+
- id: check-symlinks
17+
- id: check-toml
18+
- id: end-of-file-fixer
19+
- id: trailing-whitespace
20+
- id: mixed-line-ending
21+
args: [--fix=lf]
22+
- repo: https://github.com/asottile/reorder_python_imports
23+
rev: v3.0.1
24+
hooks:
25+
- id: reorder-python-imports
26+
args:
27+
- --py3-plus
28+
- --application-directories=.:src
29+
exclude: migrations/
30+
- repo: https://github.com/pre-commit/pygrep-hooks
31+
rev: v1.9.0
32+
hooks:
33+
- id: python-check-blanket-noqa
34+
- id: python-check-mock-methods
35+
- id: python-no-eval
36+
- id: python-no-log-warn
37+
- id: rst-backticks
38+
- repo: https://github.com/asottile/pyupgrade
39+
rev: v2.31.1
40+
hooks:
41+
- id: pyupgrade
42+
args:
43+
- --py310-plus
44+
exclude: migrations/
45+
- repo: https://github.com/adamchainz/django-upgrade
46+
rev: 1.4.0
47+
hooks:
48+
- id: django-upgrade
49+
args:
50+
- --target-version=4.0
51+
- repo: https://github.com/asottile/yesqa
52+
rev: v1.3.0
53+
hooks:
54+
- id: yesqa
55+
- repo: https://github.com/asottile/add-trailing-comma
56+
rev: v2.2.2
57+
hooks:
58+
- id: add-trailing-comma
59+
args:
60+
- --py36-plus
61+
- repo: https://github.com/hadialqattan/pycln
62+
rev: v1.2.5
63+
hooks:
64+
- id: pycln
65+
- repo: https://github.com/pycqa/flake8
66+
rev: 4.0.1
67+
hooks:
68+
- id: flake8
69+
exclude: |
70+
(?x)^(
71+
.*/migrations/.*
72+
)$
73+
additional_dependencies:
74+
- flake8-bugbear
75+
- flake8-comprehensions
76+
- flake8-tidy-imports
77+
- flake8-print
78+
args: [--max-line-length=120]

npm_mjs/management/commands/collectstatic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from django.contrib.staticfiles.management.commands.collectstatic import (
22
Command as CollectStaticCommand,
33
)
4-
from django.apps import apps
54

65

76
class Command(CollectStaticCommand):
8-
97
def set_options(self, *args, **options):
10-
return_value = super(Command, self).set_options(*args, **options)
8+
return_value = super().set_options(*args, **options)
119
self.ignore_patterns += [
1210
"js/*.mjs",
1311
"js/modules/*",

npm_mjs/management/commands/create_package_json.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import os
21
import json
3-
from json_minify import json_minify
4-
from django.core.management.base import BaseCommand
2+
import os
3+
54
from django.apps import apps as django_apps
5+
from django.core.management.base import BaseCommand
6+
from json_minify import json_minify
67

78
from npm_mjs.paths import TRANSPILE_CACHE_PATH
89

@@ -37,7 +38,7 @@ def handle(self, *args, **options):
3738
try:
3839
with open(app_package_path) as data_file:
3940
data = json.loads(json_minify(data_file.read()))
40-
except IOError:
41+
except OSError:
4142
continue
4243
deep_merge_dicts(package, data)
4344
if not os.path.exists(TRANSPILE_CACHE_PATH):

npm_mjs/management/commands/makemessages.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import os
2-
import tempfile
32
import shutil
3+
import tempfile
44
import urllib
55

6-
from django.core.management.commands import makemessages
6+
from base.management import BaseCommand
77
from django.core.management import call_command
8+
from django.core.management.commands import makemessages
89
from django.core.management.utils import popen_wrapper
910

10-
from base.management import BaseCommand
11-
1211
# This makes makemessages create both translations for Python and JavaScript
1312
# code in one go.
1413
#
@@ -43,15 +42,11 @@ def process_locale_dir(self, locale_dir, files):
4342
# We need to copy the JS files first, as otherwise babel will
4443
# attempt to read package.json files in subdirs, such as
4544
# base/package.json
46-
in_path = urllib.parse.urljoin(
47-
self.temp_dir_in + "/", file.dirpath
48-
)
45+
in_path = urllib.parse.urljoin(self.temp_dir_in + "/", file.dirpath)
4946
os.makedirs(in_path, exist_ok=True)
5047
in_file = urllib.parse.urljoin(in_path + "/", file.file)
5148
shutil.copy2(file.path, in_file)
52-
out_path = urllib.parse.urljoin(
53-
self.temp_dir_out + "/", file.dirpath
54-
)
49+
out_path = urllib.parse.urljoin(self.temp_dir_out + "/", file.dirpath)
5550
file.dirpath = out_path
5651
os.chdir(".transpile/")
5752
out, err, status = popen_wrapper(
@@ -63,7 +58,7 @@ def process_locale_dir(self, locale_dir, files):
6358
"--out-dir",
6459
self.temp_dir_out,
6560
self.temp_dir_in,
66-
]
61+
],
6762
)
6863
os.chdir("../")
6964

npm_mjs/management/commands/npm_install.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
import re
33
import shutil
44
import time
5-
from subprocess import call, check_output
5+
from subprocess import call
6+
from subprocess import check_output
67

8+
from django.apps import apps as django_apps
79
from django.core.management import call_command
810
from django.core.management.base import BaseCommand
9-
from django.apps import apps as django_apps
1011

1112
from npm_mjs import signals
12-
from npm_mjs.tools import get_last_run, set_last_run
13-
from npm_mjs.paths import SETTINGS_PATHS, TRANSPILE_CACHE_PATH
13+
from npm_mjs.paths import SETTINGS_PATHS
14+
from npm_mjs.paths import TRANSPILE_CACHE_PATH
15+
from npm_mjs.tools import get_last_run
16+
from npm_mjs.tools import set_last_run
1417

1518

1619
def install_npm(force, stdout):
@@ -30,7 +33,8 @@ def install_npm(force, stdout):
3033
app_package_path = os.path.join(config.path, "package.json")
3134
if os.path.exists(app_package_path):
3235
app_package_change = max(
33-
os.path.getmtime(app_package_path), app_package_change
36+
os.path.getmtime(app_package_path),
37+
app_package_change,
3438
)
3539
npm_install = False
3640
if (
@@ -50,7 +54,7 @@ def install_npm(force, stdout):
5054
del os.environ["SUDO_UID"]
5155
os.environ["npm_config_unsafe_perm"] = "true"
5256
node_version = int(
53-
re.search(r'\d+',str(check_output(["node", "--version"]))).group()
57+
re.search(r"\d+", str(check_output(["node", "--version"]))).group(),
5458
)
5559
if node_version > 16:
5660
os.environ["NODE_OPTIONS"] = "--openssl-legacy-provider"

npm_mjs/management/commands/transpile.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
from io import open
2-
3-
import subprocess
4-
from subprocess import call
1+
import json
52
import os
63
import shutil
4+
import subprocess
75
import time
8-
import json
9-
from npm_mjs.paths import PROJECT_PATH, TRANSPILE_CACHE_PATH, STATIC_ROOT
10-
from npm_mjs.tools import set_last_run
11-
6+
from subprocess import call
127
from urllib.parse import urljoin
13-
from django.core.management.base import BaseCommand
14-
from django.contrib.staticfiles import finders
8+
9+
from django.apps import apps
1510
from django.conf import settings
11+
from django.contrib.staticfiles import finders
12+
from django.core.management.base import BaseCommand
1613
from django.templatetags.static import PrefixNode
17-
from django.apps import apps
1814

19-
from .npm_install import install_npm
2015
from .collectstatic import Command as CSCommand
16+
from .npm_install import install_npm
2117
from npm_mjs import signals
18+
from npm_mjs.paths import PROJECT_PATH
19+
from npm_mjs.paths import STATIC_ROOT
20+
from npm_mjs.paths import TRANSPILE_CACHE_PATH
21+
from npm_mjs.tools import set_last_run
2222

2323
# Run this script every time you update an *.mjs file or any of the
2424
# modules it loads.
@@ -28,9 +28,9 @@
2828
WEBPACK_CONFIG_JS_PATH = os.path.join(TRANSPILE_CACHE_PATH, "webpack.config.js")
2929

3030
try:
31-
with open(WEBPACK_CONFIG_JS_PATH, "r") as file:
31+
with open(WEBPACK_CONFIG_JS_PATH) as file:
3232
OLD_WEBPACK_CONFIG_JS = file.read()
33-
except IOError:
33+
except OSError:
3434
pass
3535

3636

@@ -67,7 +67,7 @@ def handle(self, *args, **options):
6767
if os.path.exists(transpile_path):
6868
files = []
6969
for js_path in js_paths:
70-
for root, dirnames, filenames in os.walk(js_path):
70+
for root, _dirnames, filenames in os.walk(js_path):
7171
for filename in filenames:
7272
files.append(os.path.join(root, filename))
7373
newest_file = max(files, key=os.path.getmtime)
@@ -90,12 +90,10 @@ def handle(self, *args, **options):
9090
os.makedirs(out_dir)
9191
with open(os.path.join(transpile_path, "README.txt"), "w") as f:
9292
f.write(
93-
(
94-
"These files have been automatically generated. "
95-
"DO NOT EDIT THEM! \n Changes will be overwritten. Edit "
96-
"the original files in one of the django apps, and run "
97-
"./manage.py transpile."
98-
)
93+
"These files have been automatically generated. "
94+
"DO NOT EDIT THEM! \n Changes will be overwritten. Edit "
95+
"the original files in one of the django apps, and run "
96+
"./manage.py transpile.",
9997
)
10098

10199
mainfiles = []
@@ -104,15 +102,15 @@ def handle(self, *args, **options):
104102
for path in js_paths:
105103
for mainfile in (
106104
subprocess.check_output(
107-
["find", path, "-type", "f", "-name", "*.mjs", "-print"]
105+
["find", path, "-type", "f", "-name", "*.mjs", "-print"],
108106
)
109107
.decode("utf-8")
110108
.split("\n")[:-1]
111109
):
112110
mainfiles.append(mainfile)
113111
for sourcefile in (
114112
subprocess.check_output(
115-
["find", path, "-type", "f", "-wholename", "*js"]
113+
["find", path, "-type", "f", "-wholename", "*js"],
116114
)
117115
.decode("utf-8")
118116
.split("\n")[:-1]
@@ -176,7 +174,7 @@ def handle(self, *args, **options):
176174
index_file.write(index_js)
177175
index_file.close()
178176
else:
179-
index_file = open(outfile, "r")
177+
index_file = open(outfile)
180178
old_index_js = index_file.read()
181179
index_file.close()
182180
if old_index_js != index_js:
@@ -227,7 +225,7 @@ def handle(self, *args, **options):
227225
"ignore_patterns": ["js/", "admin/"],
228226
"use_default_ignore_patterns": True,
229227
"post_process": True,
230-
}
228+
},
231229
)
232230
found_files = find_static.collect()
233231
static_frontend_files = (
@@ -240,11 +238,11 @@ def handle(self, *args, **options):
240238
"VERSION": start,
241239
"BASE_URL": transpile_base_url,
242240
"ENTRIES": entries,
243-
"STATIC_FRONTEND_FILES": list(
244-
map(lambda x: urljoin(static_base_url, x), static_frontend_files)
245-
),
241+
"STATIC_FRONTEND_FILES": [
242+
urljoin(static_base_url, x) for x in static_frontend_files
243+
],
246244
}
247-
with open(webpack_config_template_path, "r") as f:
245+
with open(webpack_config_template_path) as f:
248246
webpack_config_template = f.read()
249247
settings_dict = {}
250248
for var in dir(settings):
@@ -256,7 +254,8 @@ def handle(self, *args, **options):
256254
except AttributeError:
257255
pass
258256
webpack_config_js = webpack_config_template.replace(
259-
"window.transpile", json.dumps(transpile)
257+
"window.transpile",
258+
json.dumps(transpile),
260259
).replace("window.settings", json.dumps(settings_dict, default=lambda x: False))
261260

262261
if webpack_config_js is not OLD_WEBPACK_CONFIG_JS:

npm_mjs/paths.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from django.conf import settings
44

55
PROJECT_PATH = str(
6-
getattr(settings, "PROJECT_PATH", getattr(settings, "PROJECT_DIR", "./"))
6+
getattr(settings, "PROJECT_PATH", getattr(settings, "PROJECT_DIR", "./")),
77
)
88

99
TRANSPILE_CACHE_PATH = os.path.join(PROJECT_PATH, ".transpile/")
1010
TRANSPILE_TIME_PATH = os.path.join(TRANSPILE_CACHE_PATH, "time")
1111

12-
SETTINGS_PATHS = map(lambda x: str(x), getattr(settings, "SETTINGS_PATHS", []))
12+
SETTINGS_PATHS = [str(x) for x in getattr(settings, "SETTINGS_PATHS", [])]
1313

1414
STATIC_ROOT = str(getattr(settings, "STATIC_ROOT", "./static/"))

0 commit comments

Comments
 (0)