Skip to content

Commit 4b42960

Browse files
authored
Merge pull request #3 from pritam001/feature-lint
Feature lint and pre-commit
2 parents 8068d2d + a7d0003 commit 4b42960

28 files changed

+297
-38
lines changed

.bandit.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[bandit]
2+
targets: src,test,*.py
3+
exclude: dummy.py
4+
skips:
5+
tests:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,5 @@ DS_Store
138138
# logs
139139
logs/
140140

141+
# mypy
142+
.mypy_cache/

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/ambv/black
5+
rev: stable
6+
hooks:
7+
- id: black
8+
language_version: python3.8
9+
- repo: https://gitlab.com/pycqa/flake8
10+
rev: 3.8.3
11+
hooks:
12+
- id: flake8

Makefile

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
CONDA_ENV_NAME=pyflask-base
2+
APP_NAME=pyflask-service
3+
APP_DIR=src
4+
TEST_DIR=test
5+
HOME_DIR_PY_FILES=*.py
6+
7+
# echo _FormatCode_ guide:
8+
# 0 Reset all styles
9+
# 1 Bold
10+
# 32 Green
11+
# 34 Blue
12+
# 35 Magenta
13+
# 36 Cyan
14+
RESET_STYLES=\033[0m
15+
BOLD_BLUE=\033[1m\033[34m
16+
BOLD_CYAN=\033[1m\033[36m
17+
18+
# Signifies our desired python version
19+
# Makefile macros (or variables) are defined a little bit differently than traditional bash, keep in mind that in the Makefile there's top-level Makefile-only syntax, and everything else is bash script syntax.
20+
PYTHON = python3
21+
22+
# .PHONY defines parts of the makefile that are not dependant on any specific file
23+
# This is most often used to store functions
24+
.PHONY = help setup format lint test debug clean
25+
26+
# Defining an array variable
27+
FILES = input output
28+
29+
# Defines the default target that `make` will to try to make, or in the case of a phony target, execute the specified commands
30+
# This target is executed whenever we just type `make`
31+
.DEFAULT_GOAL = help
32+
33+
34+
# The @ makes sure that the command itself isn't echoed in the terminal
35+
help:
36+
@echo "$(BOLD_BLUE)-----------------------------MAKE GUIDE----------------------------$(RESET_STYLES)"
37+
@echo "$(BOLD_CYAN)make setup$(RESET_STYLES) : Setup pyflask-service"
38+
@echo "$(BOLD_CYAN)make format$(RESET_STYLES) : Format and fix python code pyflask-service"
39+
@echo "$(BOLD_CYAN)make lint$(RESET_STYLES) : Lint pyflask-service"
40+
@echo "$(BOLD_CYAN)make test$(RESET_STYLES) : Test pyflask-service"
41+
@echo "$(BOLD_CYAN)make debug$(RESET_STYLES) : Debug pyflask-service"
42+
@echo "$(BOLD_CYAN)make clean$(RESET_STYLES) : Clean pyflask-service"
43+
@echo "$(BOLD_CYAN)make dev-run$(RESET_STYLES) : Run pyflask-service in environment=development"
44+
@echo "$(BOLD_CYAN)make prod-run$(RESET_STYLES) : Run pyflask-service in environment=prod"
45+
@echo "$(BOLD_BLUE)-------------------------------------------------------------------$(RESET_STYLES)"
46+
47+
48+
setup: #: Use pip-tools, pip-compile, pip install
49+
@echo "$(BOLD_CYAN)Setting up pyflask base$(RESET_STYLES)"
50+
# Check for venv, conda else exit
51+
@echo "$(BOLD_CYAN)Installing pip-tools . . .$(RESET_STYLES)"
52+
pip install pip-tools
53+
@echo "$(BOLD_CYAN)Generating requirements$(RESET_STYLES)"
54+
pip-compile -q --build-isolation --output-file=requirements/requirements.txt requirements/requirements.in
55+
@echo "$(BOLD_CYAN)Generating dev requirements$(RESET_STYLES)"
56+
pip-compile -q --build-isolation --output-file=requirements/dev-requirements.txt requirements/dev-requirements.in
57+
@echo "$(BOLD_CYAN)Syncing requirements$(RESET_STYLES)"
58+
pip-sync -q requirements/requirements.txt requirements/dev-requirements.txt
59+
@echo "$(BOLD_CYAN)Installing requirements$(RESET_STYLES)"
60+
pip install -r requirements/requirements.txt
61+
@echo "$(BOLD_CYAN)Installing dev requirements$(RESET_STYLES)"
62+
pip install -r requirements/dev-requirements.txt
63+
@echo "$(BOLD_CYAN)Adding pre-commit hooks$(RESET_STYLES)"
64+
pre-commit install
65+
66+
67+
format: #: Format and fix python code with black, isort, autoflake
68+
@echo "$(BOLD_CYAN)Blackifying $(RESET_STYLES)🍳"
69+
black --version
70+
black $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
71+
@echo "$(BOLD_CYAN)ISorting 〽️$(RESET_STYLES)"
72+
isort --recursive $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
73+
@echo "$(BOLD_CYAN)Flaking️❄️$(RESET_STYLES)"
74+
flake8 --version
75+
autoflake --remove-all-unused-imports --remove-unused-variables --remove-duplicate-keys --ignore-init-module-imports -i -r $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
76+
77+
78+
lint: #: Run static analysis with flake8, mypy and bandit
79+
@echo "$(BOLD_CYAN)Flake linting ❄️$(RESET_STYLES)"
80+
flake8 --version
81+
flake8 $(APP_DIR) $(TEST_DIR) $(HOME_DIR_PY_FILES)
82+
@echo "$(BOLD_CYAN)Static typing️️$(RESET_STYLES)⌨️"
83+
mypy --version
84+
mypy $(APP_DIR) $(HOME_DIR_PY_FILES)
85+
@echo "$(BOLD_CYAN)Securing with bandit️🕵️️$(RESET_STYLES)"
86+
bandit --version
87+
bandit -l -i -r . --format=custom
88+
@echo "$(BOLD_CYAN)Running pre-commit hooks 🏁️️️$(RESET_STYLES)"
89+
pre-commit run --all-files
90+
@echo "$(BOLD_CYAN)All checks passed 🏳️️️️$(RESET_STYLES)"
91+

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,42 @@ An "optionally opinionated and structured" flask boilerplate microservice for id
55
![Built with](https://img.shields.io/badge/-Built%20with-073551?style=flat-square)
66
![Python](https://img.shields.io/badge/-Python-3776AB?style=flat-square&logo=Python&logoColor=white)
77
![Flask](https://img.shields.io/badge/-Flask-000000?style=flat-square&logo=flask&logoColor=white)
8+
![License](https://img.shields.io/github/license/pritam001/pyflask-microservice-base?style=flat-square&label=License)
89

9-
Minimum supporting Python version: 3.6
10+
Tools
11+
------------------------------------------------------------------------------
12+
[![Min Python Version 3.6+](https://img.shields.io/badge/python-3.6+-3776AB.svg)](https://www.python.org/download/releases/3.6.0/)
13+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
14+
[![Static Analysis: flake8](https://img.shields.io/badge/static%20analysis-flake8-white.svg)](https://www.python.org/dev/peps/pep-0008/)
15+
[![Static Typing: mypy](https://img.shields.io/badge/static%20typing-mypy-blue.svg)](https://www.python.org/dev/peps/pep-0008/)
16+
[![Security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit)
17+
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
1018

19+
Development Status
20+
------------------------------------------------------------------------------
1121
![WIP](https://img.shields.io/badge/%20%F0%9F%9A%A7%20-Work%20in%20progress-important)
1222

1323
Usage Guide
1424
------------------------------------------------------------------------------
1525
This is a template project hosted on GitHub which can be used to create new repositories.
1626

17-
[GitHub Guide: Creating a repository from a template](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template)
27+
Steps for creating boilerplate project in GitHub
28+
------------------------------------------------------------------------------
29+
0. Create a new repository named "my-pyflask-project" using this template repository *
30+
0. `git clone https://www.github.com/username/my-pyflask-project.git`
31+
0. `cd my-pyflask-project`
32+
0. Create and activate conda environment `conda activate my-conda-venv` **
33+
0. `make setup` : Use pip-tools, pip-compile, pip install to setup python packages
34+
35+
\* [GitHub Guide: Creating a repository from a template](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template)
36+
<br>
37+
\** [Guide to Conda environment](https://github.com/pritam001/pyflask-microservice-base/blob/master/documentation/conda.md)
38+
39+
40+
41+
Linting Guide
42+
------------------------------------------------------------------------------
43+
`make format` : Format and fix python code with black, isort, autoflake
1844

45+
`make lint` : Run static analysis with flake8, mypy and bandit
1946

base_constructor.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

documentation/bandit.md

Whitespace-only changes.

documentation/black.md

Whitespace-only changes.

documentation/conda.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Conda documentation
22

33
### 1. Install Pip
4+
https://www.shellhacks.com/python-install-pip-mac-ubuntu-centos/
45
### 2. Install Anaconda
56
##### Linux
67
`wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh`

documentation/flake8.md

Whitespace-only changes.

0 commit comments

Comments
 (0)