Skip to content

Commit 5baa0fa

Browse files
Updating the base template (#1)
* Updated the template to add a basic linting config and optional pytest installation * Jinja-d the author name for the license * README updates and removed the lock file * Added post gen hook and cleaned up readme * Poetry-ize * Update README and spellcheck * Removed coverage blocks from toml file
1 parent dbade97 commit 5baa0fa

20 files changed

Lines changed: 413 additions & 93 deletions

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ repos:
3030
entry: poetry run flake8 .
3131
language: python
3232
files: ".*.py"
33+
exclude: "{{ cookiecutter.project_slug }}"
3334
- id: isort
3435
name: Running isort from local poetry env
3536
entry: poetry run isort .
3637
language: python
3738
files: ".*.py"
39+
exclude: "{{ cookiecutter.project_slug }}"
40+
- id: lizard
41+
name: Running lizard from local poetry env
42+
entry: poetry run lizard
43+
# Max 7 arguments allowed, show only warnings, sort by CCN, exclude .venv
44+
args: ["--CCN", "8", "-a", "7", "-w", "-s", "cyclomatic_complexity", "-x", ".\\.venv\\*"]
45+
language: python

README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1-
# Placeholder Project Name
1+
# Cookiecutter Python Package
22

3+
My take on a [Cookiecutter](https://github.com/cookiecutter/cookiecutter) template for a Python package with Poetry as the dependency manager.
34

4-
## Initial setup
5-
The bash script **setup.sh** is an executable. Devs should run
6-
```bash
7-
chmod +x setup.sh
8-
```
9-
(Powershell may have different syntax to set executable permissions) before running
10-
```
11-
bash setup.sh
12-
```
5+
**NOTE:** Only Python 3.8+ is supported.
136

14-
The **pyproject.toml** file contains project dependencies and can be updated to add more dependencies at any stage of the project by running `poetry add <packagename>`.
15-
Minimum Python version supported is 3.9.
7+
---
168

17-
The **.pre-commit-config.yaml** contains all the linters that runs before committing changes.
9+
## Features
10+
- Hooks: Pre-commit
11+
- Formatters and Linters: Black, Flake8, Flake8-bugbear, Isort, and Lizard
12+
- Testing Frameworks (Optional): Pytest, Coverage, and CovDefaults
13+
14+
## Usage
15+
16+
- Since this template uses Poetry as the dependancy manager, install poetry from `https://python-poetry.org/docs/#installation`
17+
18+
- Install the `cookiecutter` library.
19+
20+
```python
21+
python -m pip install cookiecutter
22+
```
23+
24+
- Run the command:
25+
```python
26+
python -m cookiecutter https://github.com/gurashish1singh/cookiecutter-python.git
27+
```
28+
- This template uses post-project generation hooks to:
29+
- Initialize a git repository (with default branch as main), IF the working directory is not already a git repository
30+
- Create a Poetry virtualenv
31+
- Install all dependencies
32+
- Install the pre-commit and pre-push hooks.

cookiecutter.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"full_name": "Gurashish Singh",
3+
"email": "gurashish1singh@gmail.com",
4+
"github_username": "gurashish1singh",
5+
"project_name": "Boilerplate Template",
6+
"github_repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",
7+
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
8+
"project_short_description": "Minimal python cookiecutter template with Poetry as the dependency manager and Black, Isort, Flake8, Flake8-bugbear as the linters.",
9+
"version": "0.1.0",
10+
"pytest": ["y", "n"]
11+
}

hooks/post_gen_project.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
from __future__ import annotations
3+
4+
import shlex
5+
import subprocess
6+
import sys
7+
from pathlib import Path
8+
9+
ERROR_MSG = "Error occured while running cmd"
10+
PRETTY_LINES = "*" * 80
11+
12+
13+
def main() -> int:
14+
working_dir = Path().resolve()
15+
git_dir = Path(working_dir, ".git")
16+
ret_code_one = 0
17+
if not git_dir.exists():
18+
ret_code_one = initialize_git()
19+
20+
if ret_code_one == 0:
21+
ret_code_two = setup_environment()
22+
return ret_code_one or ret_code_two or 0
23+
24+
25+
def initialize_git() -> int:
26+
COMMANDS_AND_MESSAGE = {
27+
"default_branch": (
28+
shlex.split("git config --global init.defaultBranch main"),
29+
PRETTY_LINES,
30+
),
31+
"init_git": (
32+
shlex.split("git init"),
33+
"Initializing an empty git repository\n",
34+
),
35+
}
36+
for cmds, message in COMMANDS_AND_MESSAGE.values():
37+
print(message)
38+
try:
39+
subprocess.run(cmds, check=True)
40+
except subprocess.CalledProcessError as e:
41+
print(ERROR_MSG, e)
42+
return e.returncode
43+
44+
return 0
45+
46+
47+
def setup_environment() -> int:
48+
49+
COMMANDS_AND_MESSAGE = {
50+
"install_poetry": (
51+
shlex.split("poetry install"),
52+
f"\n{PRETTY_LINES}\nInstalling poetry virtual environment",
53+
),
54+
"install_pre_commit": (
55+
shlex.split(
56+
"poetry run pre-commit install --hook-type pre-commit --hook-type pre-push"
57+
),
58+
f"\n{PRETTY_LINES}\nInstalling pre-commit hooks",
59+
),
60+
}
61+
for cmds, message in COMMANDS_AND_MESSAGE.values():
62+
print(message)
63+
try:
64+
subprocess.run(cmds, check=True)
65+
except subprocess.CalledProcessError as e:
66+
print(ERROR_MSG, e)
67+
return e.returncode
68+
69+
return 0
70+
71+
72+
if __name__ == "__main__":
73+
sys.exit(main())

poetry.lock

Lines changed: 64 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

poetry.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[virtualenvs]
2+
in-project = true

pyproject.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
[tool.poetry]
2-
name = "PLACEHOLDER PROJECT NAME"
2+
name = "cookiecutter-python"
33
version = "0.1.0"
4-
description = "PLACEHOLDER DESCRIPTION OF THE PROJECT"
4+
description = "A Python Cookiecutter template with Poetry as a dependency manager"
55
authors = ["Gurashish Singh <gurashish1singh@gmail.com>"]
6+
readme = "README.md"
7+
license = "MIT"
68

79
[tool.poetry.dependencies]
8-
python = "^3.9"
9-
PyYAML = "^6"
10-
python-json-logger = "^2"
10+
python = "^3.8"
1111

1212
[tool.poetry.dev-dependencies]
1313
flake8 = "^5"
1414
isort = "^5.5"
1515
black = "^22.9"
1616
pre-commit = "^2.20"
17+
flake8-bugbear = "^22"
18+
lizard = "^1.17"
1719

1820
[tool.black]
1921
line-length = 100

setup.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ exclude =
55
# There's no value in checking cache directories
66
__pycache__,
77
# Don't check virtualenv
8-
venv/
8+
venv/,
9+
.venv/
910
max_line_length = 100
11+
# B001 is already enabled for flake8-bugbear
12+
extend-ignore = E722

0 commit comments

Comments
 (0)