Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit 4742431

Browse files
author
staticdev
committed
Add pre-commit
1 parent 25b8e8c commit 4742431

File tree

28 files changed

+443
-214
lines changed

28 files changed

+443
-214
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,4 @@ dmypy.json
129129
.pyre/
130130

131131
# Pytype
132-
.pytype/
132+
.pytype/

.pre-commit-config.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.0.1
4+
hooks:
5+
- id: check-toml
6+
- id: check-yaml
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
- id: check-added-large-files
10+
- repo: https://github.com/prettier/prettier
11+
rev: 2.0.5
12+
hooks:
13+
- id: prettier
14+
- repo: https://github.com/psf/black
15+
rev: 19.10b0
16+
hooks:
17+
- id: black
18+
- repo: https://gitlab.com/pycqa/flake8
19+
rev: 3.7.9
20+
hooks:
21+
- id: flake8
22+
additional_dependencies:
23+
- flake8-bandit==2.1.2
24+
- flake8-bugbear==20.1.4
25+
- flake8-docstrings==1.5.0
26+
- pep8-naming==0.10.0
27+
- darglint==1.2.3
28+
- repo: https://github.com/asottile/reorder_python_imports
29+
rev: v2.3.0
30+
hooks:
31+
- id: reorder-python-imports
32+
args: [--application-directories=src]

MANIFEST.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
recursive-include sorting_bootstrap/templates *.html
1+
recursive-include sorting_bootstrap/templates *.html

README.md

+13-15
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@ pip install django-sorting-bootstrap
1818

1919
## Configuration
2020

21-
Include ``django_sorting_bootstrap`` in your ``INSTALLED_APPS``
21+
Include `django_sorting_bootstrap` in your `INSTALLED_APPS`
2222

23-
Put ``{% load sorting_tags %}`` at top of your templates.
23+
Put `{% load sorting_tags %}` at top of your templates.
2424

2525
Your templates have four tags available:
2626

27-
* ``auto_sort``
28-
* ``sort_link``
29-
* ``sort_th``
30-
* ``sort_headers``
27+
- `auto_sort`
28+
- `sort_link`
29+
- `sort_th`
30+
- `sort_headers`
3131

3232
## Basic usage
3333

3434
```html
35-
{% auto_sort queryset %}
36-
{% sort_link "link text" "field_name" %}
37-
{% sort_th "link text" "field_name" %}
38-
{% sort_headers simpleschangelist %}
35+
{% auto_sort queryset %} {% sort_link "link text" "field_name" %} {% sort_th
36+
"link text" "field_name" %} {% sort_headers simpleschangelist %}
3937
```
4038

4139
## Django views
@@ -104,15 +102,15 @@ Example usage:
104102
It may also be used as:
105103

106104
```html
107-
{% sort_link "link text" "field_name" "vis_name" %}
108-
{% sort_link "Name" "name" "what" %}
105+
{% sort_link "link text" "field_name" "vis_name" %} {% sort_link "Name" "name"
106+
"what" %}
109107
```
110108

111109
This is useful if you do not wnat to expose your database fields in urls.
112110

113111
## sort_th
114112

115-
It works the same way as sort_link, but the difference is the output template that renders a table header tag `<th>` using `Bootstrap`_ classes and Glyphicons.
113+
It works the same way as sort*link, but the difference is the output template that renders a table header tag `<th>` using `Bootstrap`* classes and Glyphicons.
116114

117115
Basic usage:
118116

@@ -148,9 +146,9 @@ You also need to call the function in your template:
148146

149147
```html
150148
<thead>
151-
<tr>
149+
<tr>
152150
{% sort_headers cl %}
153-
</tr>
151+
</tr>
154152
</thead>
155153
```
156154

noxfile.py

+106-48
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,135 @@
11
"""Nox sessions."""
2+
import contextlib
3+
import shutil
24
import tempfile
3-
from typing import Any
5+
from pathlib import Path
6+
from typing import cast
7+
from typing import Iterator
48

59
import nox
610
from nox.sessions import Session
711

12+
package = "django_sorting_bootstrap"
13+
python_versions = ["3.8", "3.7"]
814
nox.options.sessions = "lint", "safety", "mypy"
915
locations = "src", "noxfile.py"
1016

1117

12-
def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
13-
"""Install packages constrained by Poetry's lock file.
14-
This function is a wrapper for nox.sessions.Session.install. It
15-
invokes pip to install packages inside of the session's virtualenv.
16-
Additionally, pip is passed a constraints file generated from
17-
Poetry's lock file, to ensure that the packages are pinned to the
18-
versions specified in poetry.lock. This allows you to manage the
19-
packages as Poetry development dependencies.
20-
Arguments:
18+
class Poetry:
19+
"""Helper class for invoking Poetry inside a Nox session.
20+
21+
Attributes:
2122
session: The Session object.
22-
args: Command-line arguments for pip.
23-
kwargs: Additional keyword arguments for Session.install.
2423
"""
25-
with tempfile.NamedTemporaryFile() as requirements:
26-
session.run(
27-
"poetry",
28-
"export",
29-
"--dev",
30-
"--format=requirements.txt",
31-
f"--output={requirements.name}",
32-
external=True,
24+
25+
def __init__(self, session: Session) -> None:
26+
"""Constructor."""
27+
self.session = session
28+
29+
@contextlib.contextmanager
30+
def export(self, *args: str) -> Iterator[Path]:
31+
"""Export the lock file to requirements format.
32+
33+
Args:
34+
args: Command-line arguments for ``poetry export``.
35+
36+
Yields:
37+
The path to the requirements file.
38+
"""
39+
with tempfile.TemporaryDirectory() as directory:
40+
requirements = Path(directory) / "requirements.txt"
41+
self.session.run(
42+
"poetry",
43+
"export",
44+
*args,
45+
"--format=requirements.txt",
46+
f"--output={requirements}",
47+
external=True,
48+
)
49+
yield requirements
50+
51+
def version(self) -> str:
52+
"""Retrieve the package version.
53+
54+
Returns:
55+
The package version.
56+
"""
57+
output = self.session.run(
58+
"poetry", "version", external=True, silent=True, stderr=None
3359
)
34-
session.install(f"--constraint={requirements.name}", *args, **kwargs)
60+
return cast(str, output).split()[1]
3561

62+
def build(self, *args: str) -> None:
63+
"""Build the package.
3664
37-
@nox.session(python="3.8")
38-
def black(session: Session) -> None:
39-
"""Run black code formatter."""
40-
args = session.posargs or locations
41-
install_with_constraints(session, "black")
42-
session.run("black", *args)
65+
Args:
66+
args: Command-line arguments for ``poetry build``.
67+
"""
68+
self.session.run("poetry", "build", *args, external=True)
4369

4470

45-
@nox.session(python=["3.8", "3.7"])
46-
def lint(session: Session) -> None:
47-
"""Lint using flake8."""
48-
args = session.posargs or locations
49-
install_with_constraints(
50-
session, "flake8", "flake8-bandit", "flake8-black", "flake8-bugbear",
71+
def install_package(session: Session) -> None:
72+
"""Build and install the package.
73+
74+
Build a wheel from the package, and install it into the virtual environment
75+
of the specified Nox session.
76+
77+
The package requirements are installed using the versions specified in
78+
Poetry's lock file.
79+
80+
Args:
81+
session: The Session object.
82+
"""
83+
poetry = Poetry(session)
84+
85+
with poetry.export() as requirements:
86+
session.install(f"--requirement={requirements}")
87+
88+
poetry.build("--format=wheel")
89+
90+
version = poetry.version()
91+
session.install(
92+
"--no-deps", "--force-reinstall", f"dist/{package}-{version}-py3-none-any.whl"
5193
)
52-
session.run("flake8", *args)
94+
95+
96+
def install(session: Session, *args: str) -> None:
97+
"""Install development dependencies into the session's virtual environment.
98+
99+
This function is a wrapper for nox.sessions.Session.install.
100+
101+
The packages must be managed as development dependencies in Poetry.
102+
103+
Args:
104+
session: The Session object.
105+
args: Command-line arguments for ``pip install``.
106+
"""
107+
poetry = Poetry(session)
108+
with poetry.export("--dev") as requirements:
109+
session.install(f"--constraint={requirements}", *args)
110+
111+
112+
@nox.session(name="pre-commit", python="3.8")
113+
def precommit(session: Session) -> None:
114+
"""Lint using pre-commit."""
115+
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
116+
install(session, "pre-commit")
117+
session.run("pre-commit", *args)
53118

54119

55120
@nox.session(python="3.8")
56121
def safety(session: Session) -> None:
57122
"""Scan dependencies for insecure packages."""
58-
with tempfile.NamedTemporaryFile() as requirements:
59-
session.run(
60-
"poetry",
61-
"export",
62-
"--dev",
63-
"--format=requirements.txt",
64-
"--without-hashes",
65-
f"--output={requirements.name}",
66-
external=True,
67-
)
68-
install_with_constraints(session, "safety")
69-
session.run("safety", "check", f"--file={requirements.name}", "--full-report")
123+
poetry = Poetry(session)
124+
with poetry.export("--dev", "--without-hashes") as requirements:
125+
install(session, "safety")
126+
session.run("safety", "check", f"--file={requirements}", "--bare")
70127

71128

72-
@nox.session(python=["3.8", "3.7"])
129+
@nox.session(python=python_versions)
73130
def mypy(session: Session) -> None:
74131
"""Type-check using mypy."""
75132
args = session.posargs or locations
76-
install_with_constraints(session, "mypy")
133+
install_package(session)
134+
install(session, "mypy")
77135
session.run("mypy", *args)

0 commit comments

Comments
 (0)