Skip to content

Commit

Permalink
Merge pull request #127 from at-gmbh/feature/random-improvements
Browse files Browse the repository at this point in the history
Random Improvements mostly about Poetry
  • Loading branch information
klamann authored Dec 22, 2023
2 parents 2e598bb + 095871d commit 2dfb503
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 47 deletions.
3 changes: 3 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
'requirements.txt',
'requirements-dev.txt',
'setup.py',
'tests/pytest.ini',
}

files_conda = {
'environment.yml',
'environment-dev.yml',
'setup.py',
'tests/pytest.ini',
}

files_poetry = {
Expand Down Expand Up @@ -165,6 +167,7 @@ def handle_editor_settings():
print(f"Error: unsupported editor {editor_settings}")
sys.exit(1)


def handle_ci():
ci_pipeline = '{{ cookiecutter.ci_pipeline }}'
if ci_pipeline == "gitlab":
Expand Down
30 changes: 18 additions & 12 deletions {{cookiecutter.project_slug}}/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ variables:
{%- if cookiecutter.package_manager == 'conda' %}
CONDA_PKGS_DIRS: $CI_PROJECT_DIR/.cache/conda
{%- elif cookiecutter.package_manager == 'poetry' %}
POETRY_VERSION: 1.0.5
POETRY_VERSION: 1.7.1
POETRY_CACHE_DIR: $CI_PROJECT_DIR/.cache/poetry
{%- endif %}

Expand All @@ -30,7 +30,7 @@ stages:

{% if cookiecutter.package_manager == 'poetry' -%}
build-wheel:
image: python:3-slim
image: python:3.11
stage: build
artifacts:
name: 'app-wheel'
Expand All @@ -39,35 +39,41 @@ build-wheel:
expire_in: 6 mos
script:
- pip install poetry==$POETRY_VERSION
- poetry build -f wheel {% else -%}
- poetry build -f wheel{% else -%}
build-wheel:
image: python:3-slim
image: python:3.11
stage: build
artifacts:
name: 'app-wheel'
paths:
- dist/{{ cookiecutter.module_name }}-*.whl
expire_in: 6 mos
script:
- python setup.py dist {%- endif %}
- python setup.py dist{%- endif %}

{% if cookiecutter.package_manager == 'poetry' -%}
test-unit:
stage: test
image: python:3-slim
image: python:3.11
cache:
key:
files:
- pyproject.toml
- poetry.lock
paths:
- $PIP_CACHE_DIR
- $POETRY_CACHE_DIR
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
before_script:
- pip install poetry==$POETRY_VERSION
- poetry install --no-root
- poetry install --only=main,test
- source `poetry env info --path`/bin/activate
script:
- pytest tests {% elif cookiecutter.package_manager == 'conda' -%}
- pytest tests --cov src --cov-report=term --cov-report xml:coverage.xml{% elif cookiecutter.package_manager == 'conda' -%}
test-unit:
stage: test
image: continuumio/miniconda3
Expand All @@ -86,10 +92,10 @@ test-unit:
- source activate .venv
- pip install dist/{{ cookiecutter.module_name }}-*.whl
script:
- pytest tests {% elif cookiecutter.package_manager == 'pip' -%}
- pytest tests{% elif cookiecutter.package_manager == 'pip' -%}
test-unit:
stage: test
image: python:3-slim
image: python:3.11
cache:
key:
files:
Expand All @@ -103,7 +109,7 @@ test-unit:
- pip install -r requirements.txt -r requirements-dev.txt
- pip install dist/{{ cookiecutter.module_name }}-*.whl
script:
- pytest tests {%- endif %}
- pytest tests{%- endif %}

# Nonprod deployments

Expand Down
20 changes: 14 additions & 6 deletions {{cookiecutter.project_slug}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: 'v4.4.0'
hooks:
- id: check-added-large-files
- id: check-ast
Expand All @@ -13,13 +13,21 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace{% if cookiecutter.code_formatter == 'black' %}
- repo: https://github.com/psf/black
rev: stable
rev: 'stable'
hooks:
- id: black
language_version: python3.8
exclude: ^notebooks{% else %}
- repo: https://github.com/PyCQA/flake8
rev: '5.0.4'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.7'
hooks:
- id: flake8
args: ['--max-line-length=100', '--ignore=F401,W503', '--exclude=tests/*']{% endif %}
- id: ruff{% endif %}
- repo: https://github.com/pycqa/isort
rev: '5.12.0'
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: 'v3.15.0'
hooks:
- id: pyupgrade
args: [ --py38-plus ]
5 changes: 2 additions & 3 deletions {{cookiecutter.project_slug}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ Note that we mainly use notebooks for experiments, visualizations and reports. E

To build a distribution package (wheel), please use

python setup.py bdist_wheel
{% if cookiecutter.package_manager == 'poetry' %}poetry build{% else %}python setup.py bdist_wheel{% endif %}

this will clean up the build folder and then run the `bdist_wheel` command.
You can find the build artifacts in the `dist` folder.

### Contributions

Before contributing, please set up the pre-commit hooks to reduce errors and ensure consistency

pip install -U pre-commit

pre-commit install

If you run into any issues, you can remove the hooks again with `pre-commit uninstall`.
Expand Down
39 changes: 30 additions & 9 deletions {{cookiecutter.project_slug}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,46 @@ name = "{{ cookiecutter.project_slug }}"
version = "0.1.0"
description = "{{ cookiecutter.project_short_description }}"
authors = ["{{ cookiecutter.full_name }} <{{ cookiecutter.email }}>"]
license = "Proprietary"
license = "proprietary"
packages = [{ include = "{{ cookiecutter.module_name }}", from = "src" }, ]
include = ["src/{{ cookiecutter.module_name }}/res/*"]
{% if cookiecutter.create_cli == "yes" %}
[tool.poetry.scripts]
{{ cookiecutter.project_slug }} = "{{ cookiecutter.module_name }}.main:app" {% endif %}
{{ cookiecutter.project_slug }} = "{{ cookiecutter.module_name }}.main:app"{% endif %}

[tool.poetry.dependencies]
python = "^3.10"{% if cookiecutter.config_file == 'hocon' %}
python = "^3.11"{% if cookiecutter.config_file == 'hocon' %}
pyhocon = "^0.3.59"{% elif cookiecutter.config_file == 'yaml' %}
PyYAML = "^6.0"{% endif %}{% if cookiecutter.create_cli == 'yes' %}
typer = {extras = ["all"], version = "^0.7.0"}{% endif %}
typer = {extras = ["all"], version = "^0.9.0"}{% endif %}

[tool.poetry.dev-dependencies]{% if cookiecutter.code_formatter == 'black' %}
black = "^22.10"{% endif %}
pre-commit = "^2.20"
pytest = "^7.2"
pytest-cov = "^4.0"{% if cookiecutter.use_notebooks == 'yes' %}
[tool.poetry.group.test.dependencies]
pytest = "^7.0"
pytest-cov = "^4.0"

[tool.poetry.group.linter.dependencies]{% if cookiecutter.code_formatter == 'black' %}
black = "^23.11"{% else %}
ruff = "^0.1.7"{% endif %}
isort = "^5.12.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.0"{% if cookiecutter.use_notebooks == 'yes' %}
jupyterlab = "^3.5"{% endif %}
{% if cookiecutter.code_formatter != 'black' %}
[tool.ruff]
line-length = 100
src = ["src", "tests"]
ignore = ["F401"]
{% endif %}
[tool.isort]{% if cookiecutter.code_formatter == 'black' %}
profile = "black"{% else %}
py_version = 310
line_length = 100
multi_line_output = 3{% endif %}

[tool.pytest.ini_options]
minversion = "7.0"
testpaths = ["tests"]

[build-system]
requires = ["poetry-core"]
Expand Down
4 changes: 0 additions & 4 deletions {{cookiecutter.project_slug}}/setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import os
import shutil
import subprocess
import sys
from distutils.cmd import Command
from runpy import run_path

from setuptools import find_packages, setup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

def main():
# TODO your journey starts here
print("hello :)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import typer

from {{ cookiecutter.module_name }} import __title__ , __version__{% if cookiecutter.config_file != 'none' %}, util{% endif %}
from {{ cookiecutter.module_name }} import __title__, __version__{% if cookiecutter.config_file != 'none' %}, util{% endif %}

logger = logging.getLogger('{{ cookiecutter.module_name }}')

app = typer.Typer(
name='{{ cookiecutter.module_name }}',
help="{{ cookiecutter.project_short_description }}"
)
app = typer.Typer(name='{{ cookiecutter.module_name }}')


def version_callback(version: bool):
Expand All @@ -25,8 +22,6 @@ def version_callback(version: bool):
metavar='PATH',
help="path to the program configuration"
)


VersionOption = typer.Option(
None,
'-v',
Expand All @@ -40,11 +35,13 @@ def version_callback(version: bool):
@app.command()
def main(config_file: str = ConfigOption, version: bool = VersionOption):
"""
This is the entry point of your command line application. The values of the CLI params that
are passed to this application will show up als parameters to this function.
{{ cookiecutter.project_short_description }}
Note: This is the entry point of your command line application. The values of the CLI params
that are passed to this application will show up als parameters to this function.
This docstring is where you describe what your command line application does.
Try running `python -m {{ cookiecutter.module_name }} --help` to see how this shows up in the command line.
Try running `python -m {{ cookiecutter.module_name }} --help` to see how this shows up in the
command line.
"""
{% if cookiecutter.config_file != 'none' %}config = util.load_config(config_file)
util.logging_setup(config){% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load_config(config_file: Union[str, Path]) -> Dict[str, Any]:
:param config_file: path of the config file to load
:return: the parsed config as dictionary
"""
with open(config_file, 'r') as fp:
with open(config_file) as fp:
return yaml.safe_load(fp)


Expand Down
1 change: 0 additions & 1 deletion {{cookiecutter.project_slug}}/tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

def test_the_universe():
assert 6 * 7 == 42

0 comments on commit 2dfb503

Please sign in to comment.