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

Add pre-commit #39

Merged
merged 9 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ dmypy.json
.pyre/

# Pytype
.pytype/
.pytype/
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.0.1
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
# - repo: https://gitlab.com/pycqa/flake8
# rev: 3.7.9
# hooks:
# - id: flake8
# additional_dependencies:
# - flake8-bandit==2.1.2
# - flake8-bugbear==20.1.4
# - flake8-docstrings==1.5.0
# - pep8-naming==0.10.0
# - darglint==1.2.3
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.0
hooks:
- id: reorder-python-imports
args: [--application-directories=src]
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
recursive-include sorting_bootstrap/templates *.html
recursive-include sorting_bootstrap/templates *.html
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@ pip install django-sorting-bootstrap

## Configuration

Include ``django_sorting_bootstrap`` in your ``INSTALLED_APPS``
Include `django_sorting_bootstrap` in your `INSTALLED_APPS`

Put ``{% load sorting_tags %}`` at top of your templates.
Put `{% load sorting_tags %}` at top of your templates.

Your templates have four tags available:

* ``auto_sort``
* ``sort_link``
* ``sort_th``
* ``sort_headers``
- `auto_sort`
- `sort_link`
- `sort_th`
- `sort_headers`

## Basic usage

```html
{% auto_sort queryset %}
{% sort_link "link text" "field_name" %}
{% sort_th "link text" "field_name" %}
{% sort_headers simpleschangelist %}
{% auto_sort queryset %} {% sort_link "link text" "field_name" %} {% sort_th
"link text" "field_name" %} {% sort_headers simpleschangelist %}
```

## Django views
Expand Down Expand Up @@ -104,15 +102,15 @@ Example usage:
It may also be used as:

```html
{% sort_link "link text" "field_name" "vis_name" %}
{% sort_link "Name" "name" "what" %}
{% sort_link "link text" "field_name" "vis_name" %} {% sort_link "Name" "name"
"what" %}
```

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

## sort_th

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.
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.

Basic usage:

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

```html
<thead>
<tr>
<tr>
{% sort_headers cl %}
</tr>
</tr>
</thead>
```

Expand Down
175 changes: 125 additions & 50 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,152 @@
"""Nox sessions."""
import contextlib
import shutil
import tempfile
from typing import Any
from pathlib import Path
from typing import cast
from typing import Iterator

import nox
from nox.sessions import Session

nox.options.sessions = "lint", "safety", "mypy"
locations = "src", "noxfile.py"
package = "django_sorting_bootstrap"
python_versions = ["3.8", "3.7"]
nox.options.sessions = "pre-commit", "safety", "mypy" # , "tests"
locations = "src", "tests", "noxfile.py"


def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
"""Install packages constrained by Poetry's lock file.
This function is a wrapper for nox.sessions.Session.install. It
invokes pip to install packages inside of the session's virtualenv.
Additionally, pip is passed a constraints file generated from
Poetry's lock file, to ensure that the packages are pinned to the
versions specified in poetry.lock. This allows you to manage the
packages as Poetry development dependencies.
Arguments:
class Poetry:
"""Helper class for invoking Poetry inside a Nox session.

Attributes:
session: The Session object.
args: Command-line arguments for pip.
kwargs: Additional keyword arguments for Session.install.
"""
with tempfile.NamedTemporaryFile() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
f"--output={requirements.name}",
external=True,

def __init__(self, session: Session) -> None:
"""Constructor."""
self.session = session

@contextlib.contextmanager
def export(self, *args: str) -> Iterator[Path]:
"""Export the lock file to requirements format.

Args:
args: Command-line arguments for ``poetry export``.

Yields:
The path to the requirements file.
"""
with tempfile.TemporaryDirectory() as directory:
requirements = Path(directory) / "requirements.txt"
self.session.run(
"poetry",
"export",
*args,
"--format=requirements.txt",
f"--output={requirements}",
external=True,
)
yield requirements

def version(self) -> str:
"""Retrieve the package version.

Returns:
The package version.
"""
output = self.session.run(
"poetry", "version", external=True, silent=True, stderr=None
)
session.install(f"--constraint={requirements.name}", *args, **kwargs)
return cast(str, output).split()[1]

def build(self, *args: str) -> None:
"""Build the package.

@nox.session(python="3.8")
def black(session: Session) -> None:
"""Run black code formatter."""
args = session.posargs or locations
install_with_constraints(session, "black")
session.run("black", *args)
Args:
args: Command-line arguments for ``poetry build``.
"""
self.session.run("poetry", "build", *args, external=True)


@nox.session(python=["3.8", "3.7"])
def lint(session: Session) -> None:
"""Lint using flake8."""
args = session.posargs or locations
install_with_constraints(
session, "flake8", "flake8-bandit", "flake8-black", "flake8-bugbear",
def install_package(session: Session) -> None:
"""Build and install the package.

Build a wheel from the package, and install it into the virtual environment
of the specified Nox session.

The package requirements are installed using the versions specified in
Poetry's lock file.

Args:
session: The Session object.
"""
poetry = Poetry(session)

with poetry.export() as requirements:
session.install(f"--requirement={requirements}")

poetry.build("--format=wheel")

version = poetry.version()
session.install(
"--no-deps", "--force-reinstall", f"dist/{package}-{version}-py3-none-any.whl"
)
session.run("flake8", *args)


def install(session: Session, *args: str) -> None:
"""Install development dependencies into the session's virtual environment.

This function is a wrapper for nox.sessions.Session.install.

The packages must be managed as development dependencies in Poetry.

Args:
session: The Session object.
args: Command-line arguments for ``pip install``.
"""
poetry = Poetry(session)
with poetry.export("--dev") as requirements:
session.install(f"--constraint={requirements}", *args)


@nox.session(name="pre-commit", python="3.8")
def precommit(session: Session) -> None:
"""Lint using pre-commit."""
args = session.posargs or ["run", "--all-files", "--show-diff-on-failure"]
install(session, "pre-commit")
session.run("pre-commit", *args)


@nox.session(python="3.8")
def safety(session: Session) -> None:
"""Scan dependencies for insecure packages."""
with tempfile.NamedTemporaryFile() as requirements:
session.run(
"poetry",
"export",
"--dev",
"--format=requirements.txt",
"--without-hashes",
f"--output={requirements.name}",
external=True,
)
install_with_constraints(session, "safety")
session.run("safety", "check", f"--file={requirements.name}", "--full-report")
poetry = Poetry(session)
with poetry.export("--dev", "--without-hashes") as requirements:
install(session, "safety")
session.run("safety", "check", f"--file={requirements}", "--bare")


@nox.session(python=["3.8", "3.7"])
@nox.session(python=python_versions)
def mypy(session: Session) -> None:
"""Type-check using mypy."""
args = session.posargs or locations
install_with_constraints(session, "mypy")
install_package(session)
install(session, "mypy")
session.run("mypy", *args)


@nox.session(python=python_versions)
def tests(session: Session) -> None:
"""Run the test suite."""
install_package(session)
install(session, "coverage[toml]", "pytest")
session.run("coverage", "run", "-m", "pytest", *session.posargs)
session.run("coverage", "report")


@nox.session(python=python_versions)
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
install_package(session)
install(session, "pytest", "typeguard")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)
Loading