From 6c72a798a73e65c4b62da6f7fccbc641aa6b3975 Mon Sep 17 00:00:00 2001 From: Anton Bakker Date: Wed, 2 Aug 2023 09:38:13 +0200 Subject: [PATCH] add black formatting pre-commit hook --- .githooks/pre-commit | 38 ++++++++++++++++++++++++++ .gitignore | 2 ++ README.md | 18 +++++++++++- coordinates_transformation_api/main.py | 11 ++++++-- pyproject.toml | 4 ++- 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..0eee611 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +print_message() { + padding_char=">" + message=$1 + indent_level="${2:-1}" + level=${3:-NOCOLOR} + prepend_newline=${4:-false} + case "$level" in + "INFO") + color='\033[0;32m' + ;; + "WARNING") + color='\033[0;33m' + ;; + "ERROR") + color='\033[0;31m' + ;; + *) + color='\033[0;37m' + ;; + esac + if [[ $prepend_newline == true ]]; then echo ""; fi + printf "${color}%s %s\n\033[0;37m" $(printf "${padding_char}%.0s" $(seq 1 "$indent_level")) "$message" +} + +print_message "pre-commit - running Black formatting" 1 "" true + +if ! command -v black &> /dev/null +then + print_message "ERROR: black could not be found" 2 "ERROR" false + exit 1 +fi + +FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g' | grep -E "\.py$") +[ -z "$FILES" ] && exit 0 +echo "$FILES" | xargs black --stdin-filename "{}" +echo "$FILES" | xargs git add diff --git a/.gitignore b/.gitignore index 68bc17f..7fc4f9a 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +.gitconfig diff --git a/README.md b/README.md index 2bf5cc6..4113c3e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,22 @@ Design rues. Examples of these are: parameter. - with additional scenarios for other combinations. +## Develop + +To install from source requires minimum version of pip: `23.2.1`. + +Install dev dependencies with: + +```sh +pip install ".[dev]" +``` + +Install enable precommit hook with: + +```sh +git config -f .gitconfig core.hooksPath .githooks +``` + ## Install ```bash @@ -39,7 +55,7 @@ pip3 install . ct-api ``` -## Exanple operaties +## Example operaties ```bash # Landingpage diff --git a/coordinates_transformation_api/main.py b/coordinates_transformation_api/main.py index be43798..6e0c1c3 100644 --- a/coordinates_transformation_api/main.py +++ b/coordinates_transformation_api/main.py @@ -85,12 +85,19 @@ async def transform(): def custom_openapi(): - oas_file_resource = (impresources.files(assets) / "openapi.yaml") + oas_file_resource = impresources.files(assets) / "openapi.yaml" with oas_file_resource.open("rb") as oas_file: return yaml.load(oas_file, yaml.SafeLoader) app.openapi = custom_openapi + def start(): - uvicorn.run("coordinates_transformation_api.main:app", workers=2, host="0.0.0.0", port=8000, reload=True) + uvicorn.run( + "coordinates_transformation_api.main:app", + workers=2, + host="0.0.0.0", + port=8000, + reload=True, + ) diff --git a/pyproject.toml b/pyproject.toml index 1668147..829cf7e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,9 @@ requires-python = ">=3.11.4" dynamic = ["version"] [project.optional-dependencies] -dev = ["black", "mypy", "autoflake", "isort"] +dev = ["black"] +# TODO: add/enable other formatting/linting tools +# "mypy", "autoflake", "isort" [build-system] build-backend = "setuptools.build_meta"