Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pre-commit hooks. #2

Merged
merged 4 commits into from
Apr 6, 2024
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
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# For more information, see:
# https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view

# Black code formatting of entire repository
da4cd7af618fea30ab54052f6ccaa137c5471d82
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-json
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0
hooks:
- id: black-jupyter
args: ["--skip-string-normalization"]
language_version: python3.11
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.0.1
### 2024-04-05

This version of the Swath Projector implements black code formatting across the
entire repository. There should be no functional changes to the service.

## v1.0.0
### 2023-11-16

Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,39 @@ newest release of the code (starting at the top of the file).
## vX.Y.Z
```

### pre-commit hooks:

This repository uses [pre-commit](https://pre-commit.com/) to enable pre-commit
checking the repository for some coding standard best practices. These include:

* Removing trailing whitespaces.
* Removing blank lines at the end of a file.
* JSON files have valid formats.
* [ruff](https://github.com/astral-sh/ruff) Python linting checks.
* [black](https://black.readthedocs.io/en/stable/index.html) Python code
formatting checks.

To enable these checks:

```bash
# Install pre-commit Python package as part of test requirements:
pip install -r tests/pip_test_requirements.txt

# Install the git hook scripts:
pre-commit install

# (Optional) Run against all files:
pre-commit run --all-files
```

When you try to make a new commit locally, `pre-commit` will automatically run.
If any of the hooks detect non-compliance (e.g., trailing whitespace), that
hook will state it failed, and also try to fix the issue. You will need to
review and `git add` the changes before you can make a commit.

It is planned to implement additional hooks, possibly including tools such as
`mypy`.

## Get in touch:

You can reach out to the maintainers of this repository via email:
Expand Down
68 changes: 39 additions & 29 deletions bin/project_local_granule.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
function below can be edited.

"""

from os import environ
from unittest.mock import patch

Expand All @@ -77,10 +78,10 @@


def set_environment_variables():
""" If the following environment variables are absent, the
`SwathProjectorAdapter` class will not allow the projector to run. Make
sure to run this script in a different environment (e.g. conda
environment) than any local instance of Harmony.
"""If the following environment variables are absent, the
`SwathProjectorAdapter` class will not allow the projector to run. Make
sure to run this script in a different environment (e.g. conda
environment) than any local instance of Harmony.

"""
environ['ENV'] = 'dev'
Expand All @@ -93,39 +94,48 @@ def set_environment_variables():


def rmtree_side_effect(workdir: str, ignore_errors=True) -> None:
""" A side effect for the `shutil.rmtree` mock that will print the
temporary working directory containing all output NetCDF-4 files.
"""A side effect for the `shutil.rmtree` mock that will print the
temporary working directory containing all output NetCDF-4 files.

"""
print(f'\n\n\n\033[92mOutput files saved to: {workdir}\033[0m\n\n\n')


def project_granule(local_file_path: str, target_crs: str = 'EPSG:4326',
interpolation_method: str = 'near') -> None:
""" The `local_file_path` will need to be absolute, and prepended with
`file:///` to ensure that the `harmony-service-lib-py` package can
recognise it as a local file.
def project_granule(
local_file_path: str,
target_crs: str = 'EPSG:4326',
interpolation_method: str = 'near',
) -> None:
"""The `local_file_path` will need to be absolute, and prepended with
`file:///` to ensure that the `harmony-service-lib-py` package can
recognise it as a local file.

The optional keyword arguments `target_crs` and `interpolation_method`
allow for a test that overrides the default message parameters of a
geographically projected output using nearest neighbour interpolation.
The optional keyword arguments `target_crs` and `interpolation_method`
allow for a test that overrides the default message parameters of a
geographically projected output using nearest neighbour interpolation.

"""
message = Message({
'callback': 'https://example.com/callback',
'stagingLocation': 's3://example-bucket/example-path',
'sources': [{
'granules': [{
'url': local_file_path,
'temporal': {
'start': '2021-01-03T23:45:00.000Z',
'end': '2020-01-04T00:00:00.000Z',
},
'bbox': [-180, -90, 180, 90],
}],
}],
'format': {'crs': target_crs, 'interpolation': interpolation_method},
})
message = Message(
{
'callback': 'https://example.com/callback',
'stagingLocation': 's3://example-bucket/example-path',
'sources': [
{
'granules': [
{
'url': local_file_path,
'temporal': {
'start': '2021-01-03T23:45:00.000Z',
'end': '2020-01-04T00:00:00.000Z',
},
'bbox': [-180, -90, 180, 90],
}
],
}
],
'format': {'crs': target_crs, 'interpolation': interpolation_method},
}
)

set_environment_variables()

Expand Down
2 changes: 1 addition & 1 deletion docker/service_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
Loading
Loading