Skip to content

Commit

Permalink
Preliminary work for applying black to the code base
Browse files Browse the repository at this point in the history
  • Loading branch information
tillbiskup committed Jan 14, 2024
1 parent d9b7bce commit ec9bbeb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Minimal Makefile for automating recurring tasks during development
#
# Copyright (c) 2023, Till Biskup
# 2023-12-06

.PHONY: docs tests help
.DEFAULT_GOAL := help

help:
@echo "This makefile automates different recurring tasks"
@echo ""
@echo "The following targets are available:"
@echo ""
@echo "docs - create documentation using Sphinx"
@echo "tests - run unittests"
@echo "check - check code using prospector"
@echo "black - format code using Black"

docs:
@echo "Create documentation using Sphinx"
$(MAKE) -C docs html

tests:
@echo "Run unittests"
cd tests/ && python -m unittest discover -s . -t .

check:
@echo "Check code using prospector... this may take a while"
prospector

black:
@echo "Automatically format code using Black"
black -l 78 . --extend-exclude templates
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.0.dev3
0.10.0.dev4
43 changes: 43 additions & 0 deletions bin/formatPythonCode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
#
# Autoformat Python files currently in git staging area.
#
# Intended for using in pre-commit hook
#
# Formatter, formatter options and exclude pattern can be set.
#
# Note that due to calling the formatter explicitly for files, exclude patterns
# via formatter options are likely to not work. Hence the list of files to be
# reformatted needs to be filtered beforehand.
#
# Existence of the formatter is checked, and if it is not present,
# the script silently exits.
#
# Only Python files in the staging area are reformatted and afterwards
# re-added to the staging area.
#
# Copyright (c) 2023, Till Biskup
# 2023-12-06

FORMATTER="black"
FORMATTER_OPTIONS="-l 78"
EXCLUDE_PATTERN="templates"

if ! command -v $FORMATTER &> /dev/null
then
exit
fi

if \[ -n "$EXCLUDE_PATTERN" \];
then
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.py' | grep -v $EXCLUDE_PATTERN)
else
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.py')
fi

for file in $CHANGED_FILES
do
echo "Reformat '$file' using '$FORMATTER $FORMATTER_OPTIONS'"
$FORMATTER $FORMATTER_OPTIONS "$file"
git add "$file"
done
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ def read(fname):
'bibrecord',
],
extras_require={
'dev': ['prospector'],
'docs': ['sphinx', 'sphinx-rtd-theme', 'sphinx-multiversion'],
'dev': ['prospector', "pyroma", "bandit", "black", ],
'docs': ['sphinx', 'sphinx-rtd-theme', 'sphinx-multiversion',],
"deployment": [
"build",
"twine",
],
},
python_requires='>=3.7',
entry_points={
Expand Down

0 comments on commit ec9bbeb

Please sign in to comment.