Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanups, black, requirement versions + Add CI template + Add API boilerplate #3

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions templates/py_dummy/.github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Publish Python 🐍 distribution 📦 to PyPI

on:
push:
tags:
- '*'

jobs:
build:
name: Build distribution 📦
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install pypa/build
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: python3 -m build
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/

publish-to-pypi:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/{{ project.package_name }}
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

github-release:
name: >-
Sign the Python 🐍 distribution 📦 with Sigstore
and upload them to GitHub Release
needs:
- publish-to-pypi
runs-on: ubuntu-latest

permissions:
contents: write # IMPORTANT: mandatory for making GitHub Releases
id-token: write # IMPORTANT: mandatory for sigstore

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Sign the dists with Sigstore
uses: sigstore/[email protected]
with:
inputs: >-
./dist/*.tar.gz
./dist/*.whl
- name: Create GitHub Release
env:
GITHUB_TOKEN: {% raw %}${{ github.token }}{% endraw %}
run: >-
gh release create
"$GITHUB_REF_NAME"
--repo "$GITHUB_REPOSITORY"
--notes ""
- name: Upload artifact signatures to GitHub Release
env:
GITHUB_TOKEN: {% raw %}${{ github.token }}{% endraw %}
# Upload to GitHub Release using the `gh` CLI.
# `dist/` contains the built packages, and the
# sigstore-produced signatures and certificates.
run: >-
gh release upload
"$GITHUB_REF_NAME" dist/**
--repo "$GITHUB_REPOSITORY"
46 changes: 46 additions & 0 deletions templates/py_dummy/.github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: tests

on:
push:
pull_request:

jobs:
Lint:
runs-on: ubuntu-24.04
strategy:
fail-fast: true
matrix:
python-version: ["3.12"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
with:
python-version: {% raw %}${{ matrix.python-version }}{% endraw %}
- name: Install tox
run: pip install tox
- name: Black
run: |
tox -e black-check
tests:
runs-on: ubuntu-24.04
strategy:
fail-fast: true
matrix:
python-version: ["3.8", "3.10", "3.12", "3.13"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
with:
python-version: {% raw %}${{ matrix.python-version }}{% endraw %}
- name: Install required packages
run: |
sudo apt update
sudo apt install libev-dev -y
- name: Install tox
run: pip install tox
- name: Unit tests
run: |
tox -e {% raw %}${{ matrix.python-version }}{% endraw %}
- name: Functional tests
run: |
tox -e {% raw %}${{ matrix.python-version }}{% endraw %}-functional
33 changes: 33 additions & 0 deletions templates/py_dummy/etc/{{ project.package_name }}/logging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: 1
formatters:
aardvark:
format: '%(asctime)15s.%(msecs)03d %(processName)s pid:%(process)d tid:%(thread)d %(levelname)s %(name)s:%(lineno)d %(message)s'
datefmt: '%Y-%m-%dT%H:%M:%S'
# this one should only be used by handlers that support topology_id attribute
# and since it may be used by multiple modules, actual_module attribute
# should also be added (module will give a short name, pathname is too long)

handlers:
console:
class : logging.StreamHandler
formatter: aardvark
stream : ext://sys.stdout

loggers:
# by default all existing loggers are disabled upon the application
# of this config. To re-enable a logger and it's childer just add it
# to the loggers section with any even empty fields.
camel:
handlers: [console]
level: WARNING
propagate: False

{{ project.package_name }}: {}
bjoern: {}
gcl_looper: {}
restalchemy: {}
sentry_sdk: {}

root:
handlers: [console]
level: DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[db]
connection_url = postgresql://{{ project.package_name }}:[email protected]:5432/{{ project.package_name }}

[logging]
config = logging.yaml
4 changes: 3 additions & 1 deletion templates/py_dummy/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ name = {{ project.name }}
summary = {{ project.summary }}
description_file =
README.md
long_description_content_type = text/markdown
author = {{ author.name }}
author_email = {{ author.email }}
home_page = https://github.com/infraguys/{{ project.name}}/
home_page = https://github.com/infraguys/{{ project.name }}/
classifier =
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Expand All @@ -16,6 +17,7 @@ classifier =
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Programming Language :: Python :: 3.13

[files]
packages =
Expand Down
7 changes: 3 additions & 4 deletions templates/py_dummy/test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
coverage>=4.0
flake8>=6.1.0 # MIT License (MIT)
coverage>=4.0 # Apache-2.0
mock>=3.0.5,<4.0.0 # BSD
pytest==7.0.1,<8.0.0 # MIT License (MIT)
pytest-timer==0.0.11 # MIT License (MIT)
pytest>=8.0.0,<9.0.0 # MIT License (MIT)
pytest-timer>=1.0.0,<2.0.0 # MIT License (MIT)
30 changes: 18 additions & 12 deletions templates/py_dummy/tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[tox]
envlist = py3{8,10,11,12},pep8
envlist = black,py3{8,10,11,12,13},py3{8,10,12,13}-functional


[testenv]
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
setenv =
TEST_PATH={{ project.package_name }}/tests
PACKAGE_NAME={{ project.package_name }}
TEST_PATH={env:PACKAGE_NAME}/tests
functional: TEST_PATH={env:PACKAGE_NAME}/tests/functional
commands =
coverage run -p -m pytest {posargs} --timer-top-n=10 {env:TEST_PATH}

Expand All @@ -26,22 +28,15 @@ deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
coverage erase
coverage run -m pytest {posargs} --timer-top-n=10 {{ project.package_name }}/tests
coverage report --omit=.tox/*,{{ project.package_name }}/tests/*,memory:0x*
coverage html -d cover --omit=.tox/*,{{ project.package_name }}/tests/*,memory:0x*
coverage run -m pytest {posargs} --timer-top-n=10 {env:PACKAGE_NAME}/tests
coverage report --omit=.tox/*,{env:PACKAGE_NAME}/tests/*,memory:0x*
coverage html -d cover --omit=.tox/*,{env:PACKAGE_NAME}/tests/*,memory:0x*


[testenv:venv]
commands = {posargs}


[flake8]
show-source = true
builtins = _
exclude = .git,.tox,dist,doc,*lib/python*,*egg,build*
ignore = E203,W503


[testenv:doc]
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/doc-requirements.txt
Expand All @@ -57,3 +52,14 @@ deps = -r{toxinidir}/requirements.txt
usedevelop=true
allowlist_externals = echo
commands = echo "Done! Usage: source .tox/develop/bin/activate"


[testenv:black-check]
deps = black
commands = black -l 79 {env:PACKAGE_NAME} setup.py --check


[testenv:black]
line-length = 79
deps = black
commands = black -l 79 {env:PACKAGE_NAME} setup.py
Empty file.
68 changes: 68 additions & 0 deletions templates/py_dummy/{{ project.package_name }}/api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright {{ functions.now().strftime('%Y') }} {{ author.name }}.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from restalchemy.api import applications
from restalchemy.api.middlewares import contexts
from restalchemy.api.middlewares import logging
from restalchemy.api.middlewares import errors
from restalchemy.api.middlewares import retry_on_error
from restalchemy.api import routes
from restalchemy.storage import exceptions as storage_exc

from {{ project.package_name }}.api import controllers
from {{ project.package_name }}.api import routes as app_routes
from {{ project.package_name }}.api import versions


class UserApiApp(routes.Route):
__controller__ = controllers.RootController
__allow_methods__ = [routes.FILTER]


# Route to /1.0.0/ endpoint.
setattr(UserApiApp, versions.API_VERSION_1_0, routes.route(app_routes.ApiEndpointRoute))


def get_user_api_application():
return UserApiApp


def attach_middlewares(app, middlewares_list):
for middleware in middlewares_list:
app = middleware(application=app)
return app


def configure_middleware(middleware_class, *args, **kwargs):
def build_middleware(application):
return middleware_class(application=application, *args, **kwargs)

return build_middleware


def build_wsgi_application():
return attach_middlewares(
applications.Application(get_user_api_application()),
(
contexts.ContextMiddleware,
configure_middleware(
middleware_class=retry_on_error.RetryOnErrorsMiddleware,
exceptions=storage_exc.DeadLock,
),
errors.ErrorsHandlerMiddleware,
logging.LoggingMiddleware,
),
)
33 changes: 33 additions & 0 deletions templates/py_dummy/{{ project.package_name }}/api/controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright {{ functions.now().strftime('%Y') }} {{ author.name }}.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from restalchemy.api import controllers as ra_controllers

from {{ project.package_name }}.api import versions


class RootController(ra_controllers.Controller):
"""Controller for / endpoint"""

def filter(self, filters):
return (versions.API_VERSION_1_0,)


class ApiEndpointController(ra_controllers.Controller):
"""Controller for /v1.0/ endpoint"""

def filter(self, filters):
return []
Loading