From 7a6e82e4403f0d350e9b77e2007029f173c4543d Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Sun, 3 Jul 2022 11:33:08 -0400 Subject: [PATCH] Initial commit --- README.md | 104 +++++++++++++++ cookiecutter.json | 16 +++ hooks/post_gen_project.sh | 2 + requirements.txt | 2 + {{cookiecutter.package_name}}/.flake8 | 5 + {{cookiecutter.package_name}}/.gitignore | 120 ++++++++++++++++++ .../.pre-commit-config.yaml | 9 ++ {{cookiecutter.package_name}}/README.md | 15 +++ {{cookiecutter.package_name}}/docs/Makefile | 20 +++ {{cookiecutter.package_name}}/docs/make.bat | 35 +++++ .../docs/source/conf.py | 103 +++++++++++++++ .../docs/source/dev/index.rst | 84 ++++++++++++ .../docs/source/index.rst | 30 +++++ .../docs/source/user/index.rst | 47 +++++++ {{cookiecutter.package_name}}/pyproject.toml | 36 ++++++ .../{{cookiecutter.package_name}}/__init__.py | 7 + .../{{cookiecutter.package_name}}/console.py | 26 ++++ .../{{cookiecutter.package_name}}/logger.py | 6 + .../tests/__init__.py | 0 .../tests/test_project.py | 5 + 20 files changed, 672 insertions(+) create mode 100644 README.md create mode 100644 cookiecutter.json create mode 100644 hooks/post_gen_project.sh create mode 100644 requirements.txt create mode 100644 {{cookiecutter.package_name}}/.flake8 create mode 100644 {{cookiecutter.package_name}}/.gitignore create mode 100644 {{cookiecutter.package_name}}/.pre-commit-config.yaml create mode 100644 {{cookiecutter.package_name}}/README.md create mode 100644 {{cookiecutter.package_name}}/docs/Makefile create mode 100644 {{cookiecutter.package_name}}/docs/make.bat create mode 100644 {{cookiecutter.package_name}}/docs/source/conf.py create mode 100644 {{cookiecutter.package_name}}/docs/source/dev/index.rst create mode 100644 {{cookiecutter.package_name}}/docs/source/index.rst create mode 100644 {{cookiecutter.package_name}}/docs/source/user/index.rst create mode 100644 {{cookiecutter.package_name}}/pyproject.toml create mode 100644 {{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/__init__.py create mode 100644 {{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/console.py create mode 100644 {{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/logger.py create mode 100644 {{cookiecutter.package_name}}/tests/__init__.py create mode 100644 {{cookiecutter.package_name}}/tests/test_project.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..461c9ad --- /dev/null +++ b/README.md @@ -0,0 +1,104 @@ +# Python Start Project with Cookiecutter + +This template allows you to easily setup new Python projects with the most important libraries and configuration files for managing dependencies and virtualenv, code formatting, debugging, testing, static code analysis and creating command-line interfaces. + +This project is based on [python-new-project](https://github.com/stribny/python-new-project), with a heavy amount of changes + + +## What is included in the template + +The template will: + +- install [Pytest](https://docs.pytest.org/en/stable/) and [Pytest-sugar](https://pypi.org/project/pytest-sugar/) for tests +- install [pydantic](https://github.com/samuelcolvin/pydantic) for data validation +- install [Black](https://black.readthedocs.io/en/stable/) and [Flake8](https://flake8.pycqa.org/en/latest/) for standardization +- install [pre-commit](https://pre-commit.com/) to run Black, Flake8, etc before every commit +- install [sphinx](https://pypi.org/project/Sphinx/) and [sphinx-autobuild](https://pypi.org/project/sphinx-autobuild/) for documentation +- create `pyproject.toml` configuration file for managing project dependencies and virtual environments using [Poetry](https://python-poetry.org/) +- create `README.md` README file +- create `.gitignore` file +- initialize new Git repository + + +## How to use the template + +This is a [cookiecutter](https://cookiecutter.readthedocs.io) template. + +To use it, make sure you have at least Python 3.7 and git installed. + +Then install `cookiecutter` and `poetry` via `pip`: + +``` +pip install poetry --user +pip install cookiecutter --user +``` + +You can now create a new Python project by running: + +``` +cookiecutter +``` + +This will take you to a project setup asking for: +- `package_display_name` - The name that will be used in README, documentation, etc +- `package_name` - The Python package name and name of the project folder (will generate automatically) +- `package_description` - The Python package description +- `package_command` - This will configure poetry to allow calling the package, `package_name/console:entry_point`, via this command. Generally, this would be the same value as `package_name` +- `author_name` - The author of the package +- `author_email` - The email for the author +- `github_repo_name` - For documentation, such as URLs to the issue tracker +- `github_user_name` - For documentation, such as URLs to the issue tracker +- `python_version`- The Python version that should be defined in `pyproject.toml` + +The generated structure looks like this: + +``` +└── package_name/ + ├── docs + │   ├── build + │   ├── make.bat + │   ├── Makefile + │   └── source + │   ├── conf.py + │   ├── dev + │   │   └── index.rst + │   ├── index.rst + │   └── user + │   └── index.rst + ├── poetry.lock + ├── pyproject.toml + ├── README.md + ├── src + │   └── package_name + │   ├── console.py + │   ├── __init__.py + │   └── logger.py + └── tests/ + ├── __init__.py + └── test_project.py +``` + +From here, you can install the current dependencies, if needed running `poetry install` from within `package_name`. We can run `poetry shell` and `package_name` to run the script. + + +## Default Configs + +The project includes [pydantic](https://github.com/samuelcolvin/pydantic) to help with configuration and settings for projects. + +The provided example `console.py` allows for the following object: + +``` +>>> from package_name.console import Config +>>> config = Config() +>>> config +Config(api_key='EXAMPLEKEY', more_settings=SubModel(foo='bar')) +>>> config.more_settings.foo +'bar' +``` + +The config settings can be defined either through environment variables, such as the following: + +``` +API_KEY='MY_ACTUAL_KEY' +MORE_SETTINGS__FOO='OTHER' +``` diff --git a/cookiecutter.json b/cookiecutter.json new file mode 100644 index 0000000..ff2098d --- /dev/null +++ b/cookiecutter.json @@ -0,0 +1,16 @@ +{ + "package_display_name": "Package Name", + "package_name": "package_name", + "package_description": "Package description", + "package_command": "package_name", + + "author_email": "author@website.com", + "author_name": "Project Author", + + "github_repo_user": "", + "github_repo_name": "", + + "python_version": "^3.7", + + "_extensions": ["jinja2_time.TimeExtension"] +} diff --git a/hooks/post_gen_project.sh b/hooks/post_gen_project.sh new file mode 100644 index 0000000..6248a7c --- /dev/null +++ b/hooks/post_gen_project.sh @@ -0,0 +1,2 @@ +#!/bin/bash +poetry install && git init && poetry run pre-commit install diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c6ebc55 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +poetry +cookiecutter diff --git a/{{cookiecutter.package_name}}/.flake8 b/{{cookiecutter.package_name}}/.flake8 new file mode 100644 index 0000000..7bf302c --- /dev/null +++ b/{{cookiecutter.package_name}}/.flake8 @@ -0,0 +1,5 @@ +[flake8] +ignore = E203, E266, E501, W503, F403, F401 +max-line-length = 120 +max-complexity = 18 +select = B,C,E,F,W,T4,B9 diff --git a/{{cookiecutter.package_name}}/.gitignore b/{{cookiecutter.package_name}}/.gitignore new file mode 100644 index 0000000..4779690 --- /dev/null +++ b/{{cookiecutter.package_name}}/.gitignore @@ -0,0 +1,120 @@ +# Swap files +*.sw[a-p] + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +poetry.lock + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ +.mypy_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.secrets +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/{{cookiecutter.package_name}}/.pre-commit-config.yaml b/{{cookiecutter.package_name}}/.pre-commit-config.yaml new file mode 100644 index 0000000..d692225 --- /dev/null +++ b/{{cookiecutter.package_name}}/.pre-commit-config.yaml @@ -0,0 +1,9 @@ +repos: +- repo: https://github.com/psf/black + rev: "22.6.0" + hooks: + - id: black +- repo: https://gitlab.com/pycqa/flake8 + rev: "3.8.4" + hooks: + - id: flake8 diff --git a/{{cookiecutter.package_name}}/README.md b/{{cookiecutter.package_name}}/README.md new file mode 100644 index 0000000..7bd89c2 --- /dev/null +++ b/{{cookiecutter.package_name}}/README.md @@ -0,0 +1,15 @@ +# {{cookiecutter.package_display_name}} + +{{cookiecutter.package_description}} + + +## Introduction + + +## Installation + + +## Configuration + + +## Usage diff --git a/{{cookiecutter.package_name}}/docs/Makefile b/{{cookiecutter.package_name}}/docs/Makefile new file mode 100644 index 0000000..d0c3cbf --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/{{cookiecutter.package_name}}/docs/make.bat b/{{cookiecutter.package_name}}/docs/make.bat new file mode 100644 index 0000000..747ffb7 --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=source +set BUILDDIR=build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/{{cookiecutter.package_name}}/docs/source/conf.py b/{{cookiecutter.package_name}}/docs/source/conf.py new file mode 100644 index 0000000..094d7d3 --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/source/conf.py @@ -0,0 +1,103 @@ +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import os +import re +import sys + +from datetime import datetime + +from sphinx.ext import apidoc + +# sys.path.insert(0, os.path.abspath('.')) + +# Get the version from the pyproject.toml configuration +regexp = re.compile(r'.*version = [\'\"](.*?)[\'\"]', re.S) +repo_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) +init_file = os.path.join(repo_root, 'pyproject.toml') +with open(init_file, 'r') as f: + module_content = f.read() + match = regexp.match(module_content) + if match: + version = match.group(1) + else: + raise RuntimeError( + 'Cannot find version in {}'.format(init_file)) + + +# -- Project information ----------------------------------------------------- + +project = '{{cookiecutter.package_display_name}}' +copyright = '{% now "utc", "%Y" %}, {{cookiecutter.author_name}}' +author = '{{cookiecutter.author_name}}' +release = version # The full version, including alpha/beta/rc tags. +version = version + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.viewcode', +] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The master toctree document. +master_doc = 'index' + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = [] + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + +# If true, links to the reST sources are added to the pages. +html_show_sourcelink = False + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +html_show_copyright = False + +# Suppress nonlocal image warnings +suppress_warnings = ['image.nonlocal_uri'] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +html_theme_options = { + 'description': '{{cookiecutter.package_description}}', + 'show_powered_by': False, + # 'logo': 'my-logo.png', + 'logo_name': False, + 'page_width': '80%', +} + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] diff --git a/{{cookiecutter.package_name}}/docs/source/dev/index.rst b/{{cookiecutter.package_name}}/docs/source/dev/index.rst new file mode 100644 index 0000000..a7b78e3 --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/source/dev/index.rst @@ -0,0 +1,84 @@ +Developers Guide +################ + +.. _testing-label: + +Testing +======= + +The {{cookiecutter.package_display_name}} project implements a regression +test suite that improves developer productivity by identifying capability +regressions early. + +Developers implementing fixes or enhancements must ensure that they have +not broken existing functionality. The {{cookiecutter.package_display_name}} +project provides some convenience tools so this testing step can be quickly +performed. + +.. code-block:: console + + ({{cookiecutter.package_name}}) $ pytest -v + +Individual unit tests can be run also. + +.. code-block:: console + + ({{cookiecutter.package_name}}) $ pytest -v test_name + + +Code Style +========== + +Adopting a consistent code style assists with maintenance. This project uses +Black to format code, flake8, and isort to sort imports + +.. _annotations-label: + +Type Annotations +---------------- + +The code base contains type annotations to provide helpful type information +that can improve code maintenance. + + +.. _documentation-label: + +Documentation +============= + +To simplify testing and generation, give `sphinx-autobuild` a shot, which will build and start a server for testing: + +.. code-block:: console + + ({{cookiecutter.package_name}}) $ sphinx-autobuild docs/source docs/build/html + +In the future, we may integrate and automate doc autobuild as well. + + +.. _release-label: + +Release Process +=============== + +The following steps are used to make a new software release. + +The steps assume they are executed from within a development virtual +environment. + +- Check that the package version label in ``pyproject.toml`` is correct. + +- Create and push a repo tag to Github. As a convention use the package + version number (e.g. YY.MM.MICRO) as the tag. + + .. code-block:: console + + $ git checkout master + $ git tag YY.MM.MICRO -m "A meaningful release tag comment" + $ git tag # check release tag is in list + $ git push --tags origin master + + - This will trigger Github to create a release at: + + :: + + https://github.com/{username}/{{cookiecutter.package_name}}/releases/{tag} diff --git a/{{cookiecutter.package_name}}/docs/source/index.rst b/{{cookiecutter.package_name}}/docs/source/index.rst new file mode 100644 index 0000000..9b14600 --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/source/index.rst @@ -0,0 +1,30 @@ +{{cookiecutter.package_display_name}} +{{cookiecutter.package_display_name|length * '#' }} + +{{cookiecutter.package_description}} + +.. toctree:: + :maxdepth: 2 + :numbered: + :hidden: + + user/index + dev/index + + +Quick Start +=========== + +{{cookiecutter.package_display_name}} is not yet available on PyPI. + +.. code-block:: console + + $ pip install . + +After installing {{cookiecutter.package_display_name}} you can use it like any other Python module. + +Here is a simple example: + +.. code-block:: python + + import {{cookiecutter.package_name}} diff --git a/{{cookiecutter.package_name}}/docs/source/user/index.rst b/{{cookiecutter.package_name}}/docs/source/user/index.rst new file mode 100644 index 0000000..bef5887 --- /dev/null +++ b/{{cookiecutter.package_name}}/docs/source/user/index.rst @@ -0,0 +1,47 @@ +User Guide +########## + +This section of the documentation provides user focused information such as +installing and quickly using this package. + +.. _install-guide-label: + +Install Guide +============= + +.. note:: + + It is best practice to install Python projects in a virtual environment, + which can be created and activated as follows using Python 3.6+. + + .. code-block:: console + + $ python -m venv venv --prompt myvenv + $ source venv/bin/activate + (myvenv) $ + +The simplest way to install {{cookiecutter.package_display_name}} is using Pip from the local directory's path. + +.. code-block:: console + + $ pip install . + +This will install ``{{cookiecutter.package_name}}`` and all of its dependencies. + +**Note:** This package is not yet on pypi + + +{% if cookiecutter.github_repo_user|length %} +.. _report-bugs-label: + +Report Bugs +=========== + +Report bugs at the `issue tracker `_. + +Please include: + + - Operating system name and version. + - Any details about your local setup that might be helpful in troubleshooting. + - Detailed steps to reproduce the bug. +{% endif %} diff --git a/{{cookiecutter.package_name}}/pyproject.toml b/{{cookiecutter.package_name}}/pyproject.toml new file mode 100644 index 0000000..3d9a1e3 --- /dev/null +++ b/{{cookiecutter.package_name}}/pyproject.toml @@ -0,0 +1,36 @@ +[tool.poetry] +name = "{{cookiecutter.package_name}}" +version = "0.0.1" +description = "{{cookiecutter.package_description}}" +authors = ["{{cookiecutter.author_name}} <{{cookiecutter.author_email}}>"] +#license = "GPLv3" +readme = "README.md" + +[tool.poetry.dependencies] +python = "{{cookiecutter.python_version}}" +pydantic = "*" +python-dotenv = "*" + +[tool.poetry.dev-dependencies] +black = "==22.6" +flake8 = "==3.8.4" +pre-commit = "*" +pytest = "*" +pytest-sugar = "*" +sphinx = "*" +sphinx-autobuild = "*" + +[tool.poetry.scripts] +{{cookiecutter.package_command}} = "{{cookiecutter.package_name}}.console:entry_point" + +[tool.semantic_release] +version_variable = "pyproject.toml:version" +branch = "master" +build_command = "pip install poetry && poetry build" +dist_path = "dist/" +upload_to_pypi = false +remove_dist = false + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/__init__.py b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/__init__.py new file mode 100644 index 0000000..92c0cf2 --- /dev/null +++ b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/__init__.py @@ -0,0 +1,7 @@ +from importlib.metadata import version + +from .logger import create_logger + +version = version("{{cookiecutter.package_name}}") + +logger = create_logger(__package__) diff --git a/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/console.py b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/console.py new file mode 100644 index 0000000..38c0a10 --- /dev/null +++ b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/console.py @@ -0,0 +1,26 @@ +from pydantic import ( + BaseModel, + BaseSettings, +) + + +class SubModel(BaseModel): + foo: str = "bar" + + +class Config(BaseSettings): + api_key: str = "EXAMPLEKEY" + more_settings: SubModel = SubModel() + + class Config: + env_file = ".env" + env_file_encoding = "utf-8" + env_nested_delimiter = "__" + + +def entry_point(): + config = Config() + + +if __name__ == "__main__": + entry_point() diff --git a/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/logger.py b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/logger.py new file mode 100644 index 0000000..47bc0a7 --- /dev/null +++ b/{{cookiecutter.package_name}}/src/{{cookiecutter.package_name}}/logger.py @@ -0,0 +1,6 @@ +import logging + + +def create_logger(logger_name: str) -> logging: + logger = logging.getLogger(logger_name) + return logger diff --git a/{{cookiecutter.package_name}}/tests/__init__.py b/{{cookiecutter.package_name}}/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/{{cookiecutter.package_name}}/tests/test_project.py b/{{cookiecutter.package_name}}/tests/test_project.py new file mode 100644 index 0000000..788b766 --- /dev/null +++ b/{{cookiecutter.package_name}}/tests/test_project.py @@ -0,0 +1,5 @@ +from {{cookiecutter.package_name}} import version + + +def test_version(): + assert version == '0.0.1'