Skip to content

Commit

Permalink
Add various lint check in pre-commit and Nox sessions (#22)
Browse files Browse the repository at this point in the history
To ensure code formatting consistencies and identify early potential bug or security issues. Participate to #18

* Add pre-commit in dev dependencies

* Migrate lint check in pre-commit

It will allow to use the pre-commit hook in the future.

* Fix typo in documentation

* Add pre-commit-hooks in dev dependencies

* Add balck in dev dependencies

* Add black in pre-commit conf

* Add black in lint Nox session

* Add pre-commit-hooks checks

* Fix end of files

* Add reorder_python_imports in dev dependencies

* Add reorder-python-imports check

* Reorder all imports with nox

* Add flake8-bugbear in dev dependencies

* Add flake8-bugbear in pre-commit nox session

* Add flake8-bandit in dev dependencies

* Add flake8-bandit in pre-commit nox session

* Add safety in dev dependencies

* Add safety in nox sessions

* Add pep8-naming in dev dependencies

* Add pep8-naming check in pre-commit nox session

* Remove old lint session in Nox

* Add prettier in pre-commit config

* Change var variable name to be compliant with pep8
R -> radius

* Fix formatting by prettier

* Update contributing documentation about pre-commit
  • Loading branch information
oncleben31 authored Jul 29, 2020
1 parent 89d9d30 commit 09eabbb
Show file tree
Hide file tree
Showing 23 changed files with 815 additions and 102 deletions.
15 changes: 8 additions & 7 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[flake8]
select = C,D,E,F,W
select = B,B9,C,D,E,F,N,S,W
max-complexity = 10
docstring-convention = google
exclude = .venv,.git,.tox,docs,venv,bin,lib,deps,build,.eggs
max-line-length = 80

# To work with Black
max-line-length = 88
# E501: line too long
# W503: Line break occurred before a binary operator
# E501: line too long => covered by B950
# W503: Line break occurred before a binary operator => Disabled for Black compatibility
# E203: Whitespace before ':'
# D202 No blank lines allowed after function docstring
# W504 line break after binary operator
ignore = W503
# E501,
ignore = W503, E501
# E203,
# D202,
# W504

# For avoiding issue with bandit
per-file-ignores = tests/*:S101
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
name: Feature request
about: Suggest an idea for this project

---

**Is your feature request related to a problem? Please describe.**
Expand All @@ -14,4 +13,4 @@ A clear and concise description of what you want to happen.
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
Add any other context or screenshots about the feature request here.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/issue.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: Issue
about: Create a report to help us improve

---

**Describe the bug**
A clear and concise description of what the bug is.

**log**

```
Add your logs here.
```
```
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
closeComment: false
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/

# Editor
/.vscode
/.vscode
51 changes: 51 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
repos:
- repo: local
hooks:
- id: black
name: black
entry: black
language: system
types: [python]
require_serial: true
- id: flake8
name: flake8
entry: flake8
language: system
types: [python]
require_serial: true
- id: check-added-large-files
name: Check for added large files
entry: check-added-large-files
language: system
- id: check-toml
name: Check Toml
entry: check-toml
language: system
types: [toml]
- id: check-yaml
name: Check Yaml
entry: check-yaml
language: system
types: [yaml]
- id: end-of-file-fixer
name: Fix End of Files
entry: end-of-file-fixer
language: system
types: [text]
stages: [commit, push, manual]
- id: trailing-whitespace
name: Trim Trailing Whitespace
entry: trailing-whitespace-fixer
language: system
types: [text]
stages: [commit, push, manual]
- id: reorder-python-imports
name: Reorder python imports
entry: reorder-python-imports
language: system
types: [python]
args: [--application-directories=src]
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
28 changes: 18 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ en participant à la documentation.

Vous aurez besoin de Python 3.6+ et des outils suivants:

- [Poetry](https://python-poetry.org)
- [Nox](https://nox.thea.codes/en/stable)
- [Poetry](https://python-poetry.org)
- [Nox](https://nox.thea.codes/en/stable)

Installez le package avec les dépendances de développement:

Expand All @@ -26,6 +26,10 @@ Vous pouvez maintenant utiliser une session interactive Python:

`$ poetry run python`

Je vous recommande d'installer un pre-commit-hook pour lancer automatiquement quelques vérfication avant de commit vos modifications.

`$ nox -s pre-commit -- install`

## Tester le projet

Dérouler la suite de tests complète:
Expand All @@ -45,12 +49,12 @@ Les tests unitaires sont dans le répertoire `tests` et utilisent le framework p

### Soumettre votre Pull Request (PR)

Oouvrez une [pull requet](https://github.com/hacf-fr/meteofrance-api/pulls) pour proposer des changements à ce projet.
Ouvrez une [pull request](https://github.com/hacf-fr/meteofrance-api/pulls) pour proposer des changements à ce projet.

Votre pull request doit vérifier les conditions suivantes pour être acceptée:

- La suite de tests Nox doit réussir sans erreurs ni warning.
- Doit inclure des tests unitaires. Ce projet maintien une couverture de code à 100%.
- La suite de tests Nox doit réussir sans erreurs ni warning.
- Doit inclure des tests unitaires. Ce projet maintien une couverture de code à 100%.

### Proposer une nouvelle fonctionnalité

Expand All @@ -67,8 +71,8 @@ improvements ideas, or contribute to the documentation.

You need Python 3.6+ and the following tools:

- [Poetry](https://python-poetry.org)
- [Nox](https://nox.thea.codes/en/stable)
- [Poetry](https://python-poetry.org)
- [Nox](https://nox.thea.codes/en/stable)

Install the package with development requirements:

Expand All @@ -78,6 +82,10 @@ You can now run an interactive Python session, or the command-line interface:

`$ poetry run python`

I recommand to install a pre-commit-hook to have some checks done automatically before you commit your changes.

`$ nox -s pre-commit -- install`

### How to test the project

Run the full test suite:
Expand All @@ -96,12 +104,12 @@ Unit tests are located in the tests directory, and are written using the pytest

### How to submit changes

Open a [pull requet](https://github.com/hacf-fr/meteofrance-api/pulls) to submit changes to this project.
Open a [pull request](https://github.com/hacf-fr/meteofrance-api/pulls) to submit changes to this project.

Your pull request needs to meet the following guidelines for acceptance:

- The Nox test suite must pass without errors and warnings.
- Include unit tests. This project maintains 100% code coverage.
- The Nox test suite must pass without errors and warnings.
- Include unit tests. This project maintains 100% code coverage.

### Feature suggestion

Expand Down
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Météo-France utilisée par les applications moblies officielles.

Le client permet:

- Rechercher des lieux de prévisions.
- Accéder aux prévisions météorologiques horraires ou quotidiennes.
- Accéder aux prévisions de pluie dans l'heure quand disponibles.
- Accéder aux alertes météo pour chaque département français et l'Andorre. Deux
bulletins sont disponibles : un synthétique et un second avec l'évolution des alertes
pour les prochaines 24 heures (exemple [ici](http://vigilance.meteofrance.com/Bulletin_sans.html?a=dept32&b=2&c=)).
- Rechercher des lieux de prévisions.
- Accéder aux prévisions météorologiques horraires ou quotidiennes.
- Accéder aux prévisions de pluie dans l'heure quand disponibles.
- Accéder aux alertes météo pour chaque département français et l'Andorre. Deux
bulletins sont disponibles : un synthétique et un second avec l'évolution des alertes
pour les prochaines 24 heures (exemple [ici](http://vigilance.meteofrance.com/Bulletin_sans.html?a=dept32&b=2&c=)).

Ce package a été développé avec l'intention d'être utilisé par [Home-Assistant](https://home-assistant.io/) mais il peut être utilsé dans d'autres contextes.

Expand All @@ -55,17 +55,17 @@ used by the official moblie applications.

The client allows:

- Search a forecast location.
- Fetch daily or hourly weather forecast.
- Fetch rain forecast within the next hour if available.
- Fetch the weather alerts or phenomenoms for each French department or Andorre.
Two bulletin are availabe: one basic and an other advanced with the timelaps evolution for the next 24 hours (example [here](http://vigilance.meteofrance.com/Bulletin_sans.html?a=dept32&b=2&c=)).
- Search a forecast location.
- Fetch daily or hourly weather forecast.
- Fetch rain forecast within the next hour if available.
- Fetch the weather alerts or phenomenoms for each French department or Andorre.
Two bulletin are availabe: one basic and an other advanced with the timelaps evolution for the next 24 hours (example [here](http://vigilance.meteofrance.com/Bulletin_sans.html?a=dept32&b=2&c=)).

This package have been developed to be used with [Home-Assistant](https://home-assistant.io/) but it can be used in other contexts.

### Installation

To use the `meteofrance` Python module, you have to install this package first:
To use the `meteofrance` Python module, you have to install this package first:

`pip install meteofrance-api`

Expand All @@ -76,23 +76,13 @@ You will find an example ot usage in a Python program in the [integration test](
Contributions are welcomed. Please check the guidelines in [`CONTRIBUTING.md`](CONTRIBUTING.md).

[commits-shield]: https://img.shields.io/github/commit-activity/y/hacf-fr/meteofrance-api.svg?style=for-the-badge

[commits]: https://github.com/hacf-fr/meteofrance-api/commits/master

[license-shield]: https://img.shields.io/github/license/hacf-fr/meteofrance-api.svg?style=for-the-badge

[releases-shield]: https://img.shields.io/github/release/hacf-fr/meteofrance-api.svg?style=for-the-badge

[releases]: https://github.com/hacf-fr/meteofrance-api/releases

[build-shield]: https://img.shields.io/github/workflow/status/hacf-fr/meteofrance-api/Python%20package?style=for-the-badge

[build]: https://github.com/hacf-fr/meteofrance-api/actions?query=workflow%3A%22Python+package%22

[codecov-shield]: https://img.shields.io/codecov/c/github/hacf-fr/meteofrance-api?style=for-the-badge

[codecov]: https://codecov.io/gh/hacf-fr/meteofrance-api

[pypi-shield]: https://img.shields.io/pypi/v/meteofrance-api?style=for-the-badge

[pypi]: https://pypi.org/project/meteofrance-api/
96 changes: 85 additions & 11 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import contextlib
import tempfile
from pathlib import Path
from typing import Iterator, cast
from textwrap import dedent
from typing import cast
from typing import Iterator

import nox
from nox.sessions import Session

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


Expand Down Expand Up @@ -107,6 +109,87 @@ def install(session: Session, *args: str) -> None:
session.install(f"--constraint={requirements}", *args)


def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
"""Activate virtualenv in hooks installed by pre-commit.
This function patches git hooks installed by pre-commit to activate the
session's virtual environment. This allows pre-commit to locate hooks in
that environment when invoked from git.
Args:
session: The Session object.
"""
if session.bin is None:
return

virtualenv = session.env.get("VIRTUAL_ENV")
if virtualenv is None:
return

hookdir = Path(".git") / "hooks"
if not hookdir.is_dir():
return

for hook in hookdir.iterdir():
if hook.name.endswith(".sample") or not hook.is_file():
continue

text = hook.read_text()
bindir = repr(session.bin)[1:-1] # strip quotes
if not (
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
):
continue

lines = text.splitlines()
if not (lines[0].startswith("#!") and "python" in lines[0].lower()):
continue

header = dedent(
f"""\
import os
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
os.environ["PATH"] = os.pathsep.join((
{session.bin!r},
os.environ.get("PATH", ""),
))
"""
)

lines.insert(1, header)
hook.write_text("\n".join(lines))


@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,
"black",
"flake8",
"flake8-bandit",
"flake8-bugbear",
"flake8-docstrings",
"pep8-naming",
"pre-commit",
"pre-commit-hooks",
"reorder-python-imports",
)
session.run("pre-commit", *args)
if args and args[0] == "install":
activate_virtualenv_in_precommit_hooks(session)


@nox.session(python="3.8")
def safety(session: Session) -> None:
"""Scan dependencies for insecure packages."""
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=python_versions)
def tests(session: Session) -> None:
"""Run the test suite."""
Expand All @@ -118,15 +201,6 @@ def tests(session: Session) -> None:
session.notify("coverage")


@nox.session(python=python_versions)
def lint(session):
"""Lint using flake8."""
args = session.posargs or locations
install_package(session)
install(session, "flake8", "flake8-docstrings")
session.run("flake8", *args)


@nox.session
def coverage(session: Session) -> None:
"""Produce the coverage report."""
Expand Down
Loading

0 comments on commit 09eabbb

Please sign in to comment.