Skip to content

Commit

Permalink
python formatting configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Piezoid committed Sep 7, 2022
1 parent 2357221 commit 602cce6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
exclude = .git,.history,.github,config,docs,lib,scripts,src,test
ignore = E401,E203,W503,C901,E501,E722,E302,E303,E306,E741,F841,W605,E721

max-complexity=10
13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.black]
line-length = 80
include = '\.pyi?$'
exclude = '''
(
\.github/
| \.history/
| config/
| docs/
| lib/
| src/
)
'''
36 changes: 36 additions & 0 deletions scripts/python-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
# Format python files passed as arguments, or format dirty and uncached files.
# If GIT_COMMIT environment variable is set to a reference, format the files
# affected by this commit only.
# If AMEND is set, amend the HEAD commit.

set -e

if [ "$#" -eq 0 ]; then
if [ -z "$GIT_COMMIT" ]; then
# Take dirty and cached files
FILES=$(git diff-index HEAD -r --name-only)
else
# Take modified files by a commit (eg. for tree-filter)
FILES=$(git diff-tree --no-commit-id --name-only -r --diff-filter=AM "$GIT_COMMIT")
fi
readarray -t PYFILES < <(echo "$FILES" | grep '\.py$' || true)
else
PYFILES=("$@")
fi

if [ ${#PYFILES[@]} -eq 0 ]; then
exit 0
fi

set +e # Formatters are allowed to fail

black $BLACK_ARGS "${PYFILES[@]}"
autoflake --in-place "${PYFILES[@]}"
autopep8 --in-place -a -a "${PYFILES[@]}"
black $BLACK_ARGS "${PYFILES[@]}"

if [ "$AMEND" ]; then
git add -u "${PYFILES[@]}"
git commit --amend --no-edit -q --allow-empty
fi
7 changes: 7 additions & 0 deletions scripts/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file describes the Python virtualenv package requirements for
# klipper CI checks and local development.
black==22.6.0
flake8==5.0.4
autopep8==1.7.0
autoflake==1.4

0 comments on commit 602cce6

Please sign in to comment.