Skip to content

Commit

Permalink
added: initial commit0
Browse files Browse the repository at this point in the history
  • Loading branch information
juandisay committed Dec 6, 2023
2 parents faeb612 + d452cba commit 798f92e
Show file tree
Hide file tree
Showing 30 changed files with 713 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: tests

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
set -ex
pip install -r requirements.txt
- name: Install pre-commit
run: |
set -ex
pip install pre-commit
- name: Test with pytest
run: |
set -ex
pytest tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/env
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
default_language_version:
python: python3.9

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: trailing-whitespace
- id: check-merge-conflict
- id: check-case-conflict
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED

- Added Django flavor.
- Added post-generation hooks to initialize pre-commit and commit changes.
- Added pyupgrade to pre-commit hooks.
- Updated Python version to 3.11.
- Update Poetry to 1.2.x.

## [2.0.0] - 2020-01-16

The structure of the template is changed to target Python applications (and not
libraries) in the first place, and to use Poetry the base tool to work with the package.

## [1.0.0] - 2020-01-07

Created the first version of the template.
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Starter Template for Python projects

Use this cookiecutter template 🍪 to start every new Python project.

## System Dependencies

- Git
- Python 3.11
- [Poetry](https://python-poetry.org/docs/#installation) 1.2.x
- [Pre-commit hooks](https://pre-commit.com/)


## Getting Started

Create a new Python project

```
cookiecutter gh:imankulov/cookiecutter-python-project
```

For Windows, you might need to run `python -m cookiecutter` as the command might not work even though it's correctly configured on the PATH.


## What's inside

The template covers your back with the following elements:

- README.md with a pre-defined structure.
- CHANGELOG.md file with an initial message.
- Stub project template.
- Test directory with a sample test file.
- GitHub workflow configuration to run pytest automatically.
- Pre-configured mypy, Flake8, isort.
- A set of pre-commit hooks.
- pyproject.toml for Poetry.

## Django flavor

A branch [django](https://github.com/imankulov/cookiecutter-python-project/tree/django) contains an opinionated configuration for starting up a new Django project. The setup includes:

- Dependencies: django, django-environ, psycopg2-binary, sentry-sdk
- Dev dependencies: pytest-django
- Sample environment file: env.example
- A boilerplate project template. The settings.py file reads the environment from the .env file.

If you want to start a new Django project, switch to a Django branch before starting the cookiecutter.

```
git clone https://github.com/imankulov/cookiecutter-python-project.git
git checkout django
cookiecutter cookiecutter-python-project/
```

## How to use it

Before creating a project:

- Install [Poetry](https://python-poetry.org/docs/#installation).
- Install [pre-commit](https://pre-commit.com/).
- Choose a project name. Likely, in the format `foo-bar` and create a GitHub
repository for it. The root package of your project will have a default
name `foo_bar`. Make sure that you made the repository private if you plan to create
a private (non-open-source) project.
- Choose the license. For public projects, use MIT. For private projects, use "Proprietary."

The project generation runs these steps:

- Creates a new project from the template.
- Initializes a GitHub repository.
- Installs pre-commit hooks and updates all hooks to their latest versions.
- Create an initial commit.

After creating a project:

- Initialize the virtual environment and install all dependencies with `poetry install`.
- Deploy changes to GitHub.
- For Windows, you might need to change your access token to GitHub. See information [here](https://github.com/gitextensions/gitextensions/issues/4916#issuecomment-557509451)

Other hints:

- Use [How to Write Good Documentation](https://www.sohamkamani.com/blog/how-to-write-good-documentation/)
to fill in our README with the content.
- Use [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) guideline for your
changelog entries.

## Tests with GitHub actions

A file `.github/workflows/tests.yml` is responsible for running tests on GitHub and
upload coverage results to codecov.io.

To make it work with codecov.

- Go to https://app.codecov.io/ and find a repository upload token for your project.
- Go to your repository settings on GitHub, and define the actions secret
`CODECOV_TOKEN`: Settings → Secrets → New repository secret).

If you don't configure the token, the action quietly skips the upload step.

## How to configure VSCode

At the moment, VSCode doesn't automatically detect Poetry environments. You can set it
manually, though. For a newly created project, create a `.vscode/settings.json`

```
poetry install
mkdir -p .vscode
cat <<EOF > .vscode/settings.json
{
"python.pythonPath": "$(poetry env info -p)/bin/python"
}
EOF
```

Ref: https://github.com/microsoft/vscode-python/issues/8372

## Using .env with VSCode

If you use VSCode and the [vscode-dotenv](https://github.com/mikestead/vscode-dotenv)
extension, add the following lines to your `.vscode/settings.json` file:

```json
"files.associations": {
"env.example": "dotenv",
"env.github-actions": "dotenv"
}
```

## How to contribute

- If you have questions, ideas or suggestions, write them down in an issue.
- If you have a fix or an enhancement, create a pull request.
23 changes: 23 additions & 0 deletions cookiecutter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"author_name": "Your Name",
"author_email": "[email protected]",
"author_github": "youraccount",

"project_name": "Foo Bar",
"project_short_description": "Starter Template for Python projects",
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '-').replace('_', '-') }}",
"project_license": ["MIT", "Proprietary"],
"project_url": "https://github.com/{{ cookiecutter.author_github}}/{{ cookiecutter.project_slug }}",
"project_version": "0.1.0",

"database": ["postgresql", "sqlite"],

"postgresql_port": "5432",
"postgresql_version": "15",
"postgresql_user": "{{ cookiecutter.project_slug.replace('-', '_') }}",
"postgresql_password": "password",
"postgresql_database": "{{ cookiecutter.project_slug.replace('-', '_') }}",

"root_package": "{{ cookiecutter.project_slug.replace('-', '_') }}",
"python_version": "3.11"
}
34 changes: 34 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import subprocess as subp


DATABASE = "{{ cookiecutter.database }}"


def run():
"""Main entrypoint."""
if DATABASE == "sqlite":
# We don't need docker-compose.yml for sqlite
run_command("rm", "-f", "docker-compose.yml")

run_command("git", "init")
run_command("git", "add", ".")
run_command("pre-commit", "install")
run_command("pre-commit", "autoupdate")
run_command("git", "add", ".") # make sure we stage .pre-commit configuration
run_command("pre-commit", "run", "--all-files")
run_command("git", "add", ".") # absorb changes made by pre-commit
run_command(
"git",
"commit",
"-m",
"Generate project from imankulov/cookiecutter-python-project",
)


def run_command(*args):
"""Helper function to run a command."""
print(f"Running: {' '.join(args)}")
return subp.run(args)


run()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cookiecutter
pytest
pytest-cookies
Empty file added tests/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pathlib import Path
import subprocess as subp

import pytest

template_root = Path(__file__).parents[1].as_posix()


@pytest.fixture(scope="module")
def bake_result(cookies_session):
"""Reusable project fixture."""
# Set the module scope because creating a new project takes a while.
return cookies_session.bake(
extra_context={"project_name": "Test Project"}, template=template_root
)


def test_bake_creates_project(bake_result):
assert bake_result.exit_code == 0
assert bake_result.exception is None
assert bake_result.project_path.name == "test-project"
assert bake_result.project_path.is_dir()
assert (bake_result.project_path / "test_project").is_dir()


def test_bake_initializes_git(bake_result):
assert (bake_result.project_path / ".git").is_dir()


def test_bake_install_pre_commit_hooks(bake_result):
assert (bake_result.project_path / ".git/hooks/pre-commit").is_file()


def test_bake_commits_all_changes(bake_result):
ret = subp.run(["git", "diff", "--quiet"], cwd=bake_result.project_path)
assert ret.returncode == 0
48 changes: 48 additions & 0 deletions {{cookiecutter.project_slug}}/.github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: tests

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest
{% if cookiecutter.database == "postgresql" -%}
services:
postgres:
image: postgres:14
env:
POSTGRES_USER: "{{ cookiecutter.postgresql_user }}"
POSTGRES_PASSWORD: "{{ cookiecutter.postgresql_password }}"
POSTGRES_DB: "{{ cookiecutter.postgresql_database }}"
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
{%- endif %}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "{{ cookiecutter.python_version }}"
- name: Install dependencies
run: |
set -ex
curl -sSL https://install.python-poetry.org | python3 -
$HOME/.local/bin/poetry install
- name: Create environment file for testing
run: |
cp env.testing .env
- name: Test with pytest
run: |
set -ex
$HOME/.local/bin/poetry run coverage run -m pytest
- name: Convert coverage to XML
run: |
set -ex
$HOME/.local/bin/poetry run coverage xml
- name: Upload coverage to codecov
env:
CODECOV_TOKEN: {% raw %}${{ secrets.CODECOV_TOKEN }}{% endraw %}
run: |
bash <(curl -s https://codecov.io/bash)
8 changes: 8 additions & 0 deletions {{cookiecutter.project_slug}}/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vscode
.mypy_cache
.pytest_cache
.env
.coverage
coverage.xml
htmlcov/
dist
26 changes: 26 additions & 0 deletions {{cookiecutter.project_slug}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
default_language_version:
python: python3.11

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: check-merge-conflict
- id: check-case-conflict
- id: debug-statements

- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.282
hooks:
- id: ruff

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
- id: mypy
12 changes: 12 additions & 0 deletions {{cookiecutter.project_slug}}/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [{{ cookiecutter.project_version }}] - YYYY-MM-DD

### Added

- Created the project scaffold.
Loading

0 comments on commit 798f92e

Please sign in to comment.